7.3 Blinking LED

Share for us

Next, let’s make the LED blink!

First, we need to save a copy of the code in the last chapter.

For Linux, Mac OS X Users or screen users

For users who have a display at hand, select File -> Save As, or just press Ctrl + Shift + S.

Select the directory /home/pi/RPi_Beginner, modify the File name to blink_led.py, and click Save.

For Windows Users

For remote login, type in the cp command:

cp led.py blink_led.py

And open blink_led.py by nano command.cp led.py blink_led.py

nano blink_led.py

Then continue.

Note: In the subsequent code, if you do not see the hint “For remote login” or “For users who have a display at hand” and alike, it means the code and the steps apply to both situations.

So, how to make an LED blink? Blinking, in fact, is turning the LED on and then off in a short time. No more delay, let’s start!

Now, go back to the code editing window. Change GPIO.HIGH back to GPIO.LOW, and add a line at the bottom:

import RPi.GPIO as GPIO

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT)

GPIO.output(17, GPIO.LOW)       # LED ON

GPIO.output(17, GPIO.HIGH)      # LED OFF 

Then the LED will blink!

Note: You can use # to comment out a line in Python, thus the contents behind can be seen as comments, but the Python would not run it. Therefore it would not take effect while providing some significant information as reminds.

To repeat the LED brightening and dimming cycle, we can use the while function in the code like this to include LED ON and LED OFF parts:

import RPi.GPIO as GPIO

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT)

 

while True:

    GPIO.output(17, GPIO.LOW)      # LED ON

    GPIO.output(17, GPIO.HIGH)      # LED OFF

True means to keep the while function circulating. Remember to add a colon: behind while True, and leave 4 spaces at the beginning of each line following (when you press Enter or Tab, the spaces will be automatically added). By the spaces, it will be obvious for the program that the subsequent controlling LED lines belong to the while function, and the Python will know which need to keep in circulating.

Don’t be hasty to run the code. If you run the code, the LED will be turned on and off in extremely high speed, and the change between them is barely visible for our eyes. All we see is that the LED seems constant on, so it’s very necessary to add a time module here.

The time module is a built-in one in Python which is used to read and implement time. We will just mention the simplest function time.sleep(second) for delay – it is used to slow the program running by letting it “sleep” for a while after each cycle.

First, add import time at the beginning.

Add time.sleep() behind the GPIO control lines, to let the Raspberry Pi “sleep” for a while:

import RPi.GPIO as GPIO

import time

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT)

 

while True:

    GPIO.output(17, GPIO.LOW)       # LED ON

    time.sleep(0.5)                  # Sleep 0.5 seconds

    GPIO.output(17, GPIO.HIGH)      # LED OFF

    time.sleep(0.5)                  # Sleep 0.5 seconds

Note: It’s not necessary to type in the label content behind # in your code.

Then the LED will light up and keep on for 0.5s, and then it will go out and keep off for 0.5s. The cycle repeats so that the LED blinking becomes clear. Press Ctrl + S to save the code. And now press F5 to run it!

Brilliant! The LED blinks now! As there is no stop code in the while loop, you can press Ctrl + C if you want to stop it.

You can also modify the value in time.sleep() to change the blinking speed. Have a try!

Have you found that you need to modify two places to change that speed? Actually you can use a variable to simplify this.

Add after the import line:

delay = 0.1

And change the two values 0.5 in the time.sleep() above to delay:

import RPi.GPIO as GPIO

import time

 

delay = 0.1

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT)

 

while True:

    GPIO.output(17, GPIO.LOW)       # LED ON

    time.sleep(delay)               # Sleep "delay" second

    GPIO.output(17, GPIO.HIGH)      # LED OFF

    time.sleep(delay)               # Sleep "delay" second

By doing this, you only need to adjust the variable to change the blinking rate. When the program becomes more complex, the modification will make it very convenient for you to change the values in long code.

You can also change the pin number, 17, to a variable, thus you don’t need to check which pin connects to which port when there are many I/O ports.

Add a variable led_pin before the delay line: led_pin= 17

And change the value 17 in all subsequent GPIO.output() to led_pin:


import RPi.GPIO as GPIO

import time

 

led_pin = 17

delay = 0.1

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(led_pin, GPIO.OUT)

 

while True:

    GPIO.output(led_pin, GPIO.LOW)       # LED ON

    time.sleep(delay)                    # Sleep "delay" second

    GPIO.output(led_pin, GPIO.HIGH)      # LED OFF

    time.sleep(delay)                    # Sleep "delay" second 

Save the code and run it to check.

If only a warning appears without any errors, then we can move on. But if there is an error, read the error information carefully which will prompt you in which line what goes wrong, so you can check the code accordingly.

Oh, the LED may be too bright or even dazzling? But then, how to adjust the brightness? Let’s check it out later!