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.

  1. Raspberry pi 4,
  2. microSD card,
  3. Power adapte
  4. 390 ohms Resistor,
  5. LED,
  6. Breadboard,
  7. Jumper Wire.

    Connect the circuit like this.

    blink LED with raspberry pi 4 circuit

    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.

    Find Pinout:

    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.

    pinout
    Raspberry pi 4 pinout
    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.

    Thonny vs Python 3 IDE

    1) gpiozero
    #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.

    while True:

    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.

    Sleep(1)

    Now we will run the code and Yes we can see blinking LED as well as output in shell pane.

    gpiozero blinking LED output

    2) Rpi.GPIO
    #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.

    board and BCM chart

    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.

    set warning line

    GPIO.setwarnings(False)

    This line is similar to arduino pinmode where we set input and output for pin. Here we are setting ledpin output.

    GPIO.setup(ledPin,GPIO.OUT)

    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.

    RPi.GPIO blinking LED output.png

    This two library mostly use. It is on you, which you want to prefer.

    Till then keep learning keep making.