Raspberry Pi LED Blueprints
上QQ阅读APP看书,第一时间看更新

Blinking LEDs

In this section, we will build a simple app that interacts with Raspberry Pi GPIO. We will use three LEDs, which are attached to the Raspberry Pi 2 board. Furthermore, we will turn the LEDs on/off sequentially.

The following hardware components are needed:

  • Raspberry Pi 2.(you can change this model)
  • Three LEDs of any color
  • Three resistors (330 Ω or 220 Ω)
    Blinking LEDs

The hardware wiring can be implemented as follows:

  • LED 1 is connected to Pi GPIO18
  • LED 2 is connected to Pi GPIO23
  • LED 3 is connected to Pi GPIO24

The following image shows the hardware connection for LED blinking:

Blinking LEDs

Now you can write a program using WiringPi with Python. The following is the complete Python code for blinking LEDs:

# ch01_01.py file

import wiringpi2 as wiringpi
import time

# initialize
wiringpi.wiringPiSetup()

# define GPIO mode
GPIO18 = 1
GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
wiringpi.pinMode(GPIO18, OUTPUT)
wiringpi.pinMode(GPIO23, OUTPUT)
wiringpi.pinMode(GPIO24, OUTPUT)


# make all LEDs off
def clear_all():
    wiringpi.digitalWrite(GPIO18, LOW)
    wiringpi.digitalWrite(GPIO23, LOW)
    wiringpi.digitalWrite(GPIO24, LOW)

# turn on LED sequentially
try:
    while 1:
        clear_all()
        print("turn on LED 1")
        wiringpi.digitalWrite(GPIO18, HIGH)
        time.sleep(2)
        clear_all()
        print("turn on LED 2")
        wiringpi.digitalWrite(GPIO23, HIGH)
        time.sleep(2)
        clear_all()
        print("turn on LED 3")
        wiringpi.digitalWrite(GPIO24, HIGH)
        time.sleep(2)

except KeyboardInterrupt:
    clear_all()

print("done")

Save this script in a file named Python ch01_01.py.

Moreover, you can run this file on the terminal. Type the following command:

sudo python ch01_01.py

You should see three LEDs blinking sequentially. To stop the program, you can press CTRL+C on the Pi terminal. The following is a sample of the program output:

Blinking LEDs

Based on our wiring, we connect three LEDs to GPIO18, GPIO23, and GPIO24 from the Raspberry Pi board. You can see these WiringPi GPIO values from the gpio readall command and find GPIO18, GPIO23, and GPIO24 recognized as (the wPi column) 1, 4, and 5, respectively.

First, we initialize WiringPi using wiringpi.wiringPiSetup(). Then, we define our GPIO values and set their modes on Raspberry Pi as follows:

GPIO18 = 1
GPIO23 = 4
GPIO24 = 5
LOW = 0
HIGH = 1
OUTPUT = 1
wiringpi.pinMode(GPIO18, OUTPUT)
wiringpi.pinMode(GPIO23, OUTPUT)
wiringpi.pinMode(GPIO24, OUTPUT)

Each LED will be turned on using wiringpi.digitalWrite(). time.sleep(n) is used to hold the program for n seconds. Let's set a delay time of two seconds as follows:

clear_all()
print("turn on LED 1")
wiringpi.digitalWrite(GPIO18, HIGH)
time.sleep(2)

The clear_all() function is designed to turn off all LEDs:

def clear_all():
    wiringpi.digitalWrite(GPIO18, LOW)
    wiringpi.digitalWrite(GPIO23, LOW)
    wiringpi.digitalWrite(GPIO24, LOW)