How to blink an LED bulb and how to make an LED chaser using a Raspberry Pi board
Hello and welcome back. In this tutorial, we will learn how to blink an LED bulb and how to make an LED chaser using a Raspberry Pi board. I used the Raspberry Pi 2 Model B board for that. You can use any other board. The process is the same. Also, if you don’t have knowledge about Raspberry Pi OS and how to install Raspberry Pi OS on a Raspberry Pi board, please use this link for that. I explained step by step in that lesson. Ok now, you can engage in this tutorial. You cannot proceed without basic knowledge. Because all our upcoming tutorials and projects are based on the Raspberry Pi OS and the Thonny IDE. Also, I used the python language in this lesson series. Therefore first, get a good understanding of Raspberry Pi OS and Raspberry Pi boards.
Today in this tutorial we will blink an LED bulb and create an LED chaser using that knowledge. I think this is a basic example in the development board programming projects. So, I decided to do it. I will do advanced projects in future lessons. Also, use a GPIO ribbon cable and a GPIO extension board to work easily with the Raspberry Pi board and breadboard. It will be most important in your future projects. But, don’t worry you can make projects without these components. I also didn’t use these components.
Ok, let’s do it step by step. The required components are given below.
- Raspberry Pi board — Amazon
- LED bulb x 8 — Our Store / Amazon
- 180-ohm resistor x 8 — Our Store / Amazon
- Breadboard x 1 — Our Store / Amazon
- Male to Female jumper wires x 9 — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Now, identify these components.
1st Example — LED Blink
Step 1
Firstly, place the LED bulb and resistor on the breadboard.
Step 2
Secondly, connect these components to the Raspberry Pi board. Use the circuit diagram below for that.
Step 3
Thirdly, connect the mouse, keyboard, monitor and SD card to the Raspberry Pi board.
Step 4
Finally, connect the power source to the Raspberry Pi board.
Step 5
OK, now let’s create a program for this project. For that, I used the Thonny IDE and python language.
- First, click the Raspberry icon and go to Thonny IDE. Now, you can write your first LED Blink code. It is as follows. This program is described line by line.
- The complete program of this project — Download
#Include library files
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Set the LED pin as output pin
GPIO.setup(4,GPIO.OUT)
# Turn LED on and off
while True:
GPIO.output(4,GPIO.HIGH)# Turn on the LED
sleep(1)# Delay time
GPIO.output(4,GPIO.LOW)# Turn off the LED
sleep(1)# Delay time
- Now, save this program as you like. And then, click the run button.
- Good job, now you can enjoy your first Raspberry Pi project.
2nd Example — LED Chaser
Step 1
Firstly, place the LED bulbs and Resistors on the breadboard.
Step 2
Secondly, connect these LED bulbs to the Raspberry Pi board. For that, use the circuit diagram below.
Step 3
Thirdly, connect the mouse, keyboard, monitor, and SD card to the Raspberry Pi board.
Step 4
Now, connect the power supply to this board.
Step 5
And then, let’s create the program for this project. For that, open the Thonny IDE and paste the code below. It’s included six LED patterns. For that, I mainly used for loop functions.
- The complete program of this project — Download
#Include the library files
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Delay time
delay = 0.07
#Set the LED pins as output pins
for x in range(2,10):
GPIO.setup(x,GPIO.OUT)
#Pattern one
def patternOne(times):
for a in range (0,times):
for i in range(2,10):
GPIO.output(i,GPIO.HIGH)
sleep(delay)
GPIO.output(i,GPIO.LOW)
#Pattern two
def patternTwo(times):
for a in range(0,times):
for i in range(2,10):
GPIO.output(i,GPIO.HIGH)
sleep(delay)
GPIO.output(i,GPIO.LOW)
for x in range(8,2,-1):
GPIO.output(x,GPIO.HIGH)
sleep(delay)
GPIO.output(x,GPIO.LOW)
#Pattern three
def patternThree(times):
for a in range(0,times):
for i in range(2,10):
GPIO.output(i,GPIO.LOW)
sleep(delay)
GPIO.output(i,GPIO.HIGH)
for x in range(8,2,-1):
GPIO.output(x,GPIO.LOW)
sleep(delay)
GPIO.output(x,GPIO.HIGH)
#Pattern four
def patternFour(times):
for a in range(0,times):
for i in range(2,10):
GPIO.output(i,GPIO.HIGH)
sleep(delay)
for x in range(9,2,-1):
GPIO.output(x,GPIO.LOW)
sleep(delay)
#Pattern five
def patternFive(times):
for a in range(0,times):
for i in range(2,9):
GPIO.output(i,GPIO.LOW)
sleep(delay)
for x in range(9,1,-1):
GPIO.output(x,GPIO.HIGH)
sleep(delay)
#Pattern six
def patternSix(times):
for a in range(0,times):
for i in range(2,9):
GPIO.output(i,GPIO.HIGH)
GPIO.output(i+1,GPIO.HIGH)
sleep(delay)
GPIO.output(i,GPIO.LOW)
GPIO.output(i+1,GPIO.LOW)
for x in range(8,2,-1):
GPIO.output(x,GPIO.HIGH)
GPIO.output(x-1,GPIO.HIGH)
sleep(delay)
GPIO.output(x,GPIO.LOW)
GPIO.output(x-1,GPIO.LOW)
while True:
patternOne(3)
patternTwo(3)
patternThree(3)
patternFour(3)
patternFive(3)
patternSix(3)
- Next, save this program and click the run button. Now, you can see the LED patterns one by one. Try to create new patterns using this knowledge. For that, Study this code carefully.
OK, enjoy this project. The full video guide is below. So, see you in the next tutorial or project.
How to blink an LED bulb and how to make an LED chaser using a Raspberry Pi board