Hi guys, In this tutorial we are going to learn, how to make your first project blinking LED with raspberry pi. We will also learn how to use different libraries for blinking LED. Once you learn how to blink LED, you can literally control anything from LED to Air-Conditioner. So, Lets get started.
For this project you will need.
- Raspberry pi 4,
- Dual Fan Heat Sink Aluminium Armor Case. (Optional)
- microSD card,
- Power adapter,
- 390 ohms Resistor,
- Jumper Wire.
Connect the circuit like this.
Be careful when connecting wires to GPIO. Because it is directly connected CPU of raspberry pi. If you apply wrong voltage or shorts two pin, you can easily destroy your raspberry pi.
We will make LED blink with two libraries. First with gpiozero and second with Rpi.GPIO.
If you dont know what is BCM number and Board number and where it is on your rasperry pi. You can type this command in terminal to know more about pin number.
Script Writing IDE:
I am using Thonny python IDE. But if you want you can use python 3 IDE. At the time of recording this it is not available for latest version of raspbain. We can install it by this command.
sudo apt-get update sudo apt-get install idle3
As you can see Thonny python IDE works same as python 3 IDE.
#blinking with gpiozero library from gpiozero import LED from time import sleep led = LED(23) while True: led.on() print('LED ON') sleep(1) led.off() print('LED OFF') sleep(1)
This library is easiest way to blink LED. At the same time it is simple to understand and code.
First we will import LED from gpiozero library and sleep from time library.
from gpiozero import LED from time import sleep
create an object called led that refers to GPIO 23. You can see this number on pinout chart.
led = LED(23)
while loop will run forever. If you don’t interrupt.
It will turn ON LED for 1 second and print LED ON in shell pane.
led.on() print('LED ON') sleep(1)
This line will turn OFF LED for 1 second and print LED OFF to shell pane.
led.off() print('LED OFF') sleep(1)
You can change the ON OFF delay time in sleep. It is in seconds.
Now we will run the code and Yes we can see blinking LED as well as output in shell pane.
#blink with RPi.GPIO library import RPi.GPIO as GPIO import time ledPin = 16 GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(ledPin,GPIO.OUT) while True: GPIO.output(ledPin, GPIO.HIGH) print('LED ON') time.sleep(1) GPIO.output(ledPin, GPIO.LOW) print('LED OFF') time.sleep(1)
This is old library and similar to arduino.
we will import Rpi.GPIO as GPIO and import time for sleep.
import RPi.GPIO as GPIO import time
Now we will set pin number for LED. It is also depends on which boarding scheme you are using.
For example I am using here GPIO.BCM, On board pin number it is 16 but on BCM number it is 23. so our ledpin number is 23 for GPIO.BCM. If we are using GPIO.BOARD our pin number will be 16. it is on you, which scheme you prefer.
ledPin = 16 # if it is GPIO.BCM ledpin = 23 GPIO.setmode(GPIO.BOARD) # GPIO.BCM
if we don’t add this set warning line. It will give warning like this every time when we run the script.
This line is similar to arduino pinmode where we set input and output for pin. Here we are setting ledpin output.
If you don’t interrupt this, while loop will run forever.
This lines turn ON LED for 1 second and print LED ON,
GPIO.output(ledPin, GPIO.HIGH) print('LED ON') time.sleep(1)
This line OFF LED for 1 second and print LED OFF.
GPIO.output(ledPin, GPIO.LOW) print('LED OFF') time.sleep(1)
if you want to change delay time. You can change it from here time.sleep(1), it is in second.
Now we will run the code, we can see blinking LED as well as output in shell pane.
This two library mostly use. It is on you, which you want to prefer.
Till then keep learning keep making.