Published on

Controlling LEDs with Raspberry Pi

Authors
  • avatar
    Name
    UjjwalBgn
    Twitter

Note: It is recommended to turn off the Pi before connecting the wires to the GPIO pins. This will help to prevent damages to the Pi.

For this experiment, we will need the following:

  1. A Raspberry Pi (We will be using Raspberry Pi 3B)
  2. A LED light
  3. Jumpers Wire (also called DuPont wire)
  4. resistor

In this setup, we have used GPIO pin 26 (Board Pin 37) to connect the jumper wire to the positive terminal of the LED. Connect the ground pin (Board Pin 38) to the resistor. The resistor will decrease the current passing through and help to prevent the LED from burning out. Connect the other end of the resistor to the negative terminal of the LED. This setup will work for Raspberry Pi 3B and Raspberry Pi 4B.

(The longer leg of the LED is the positive terminals, also know as anode, and the shorter leg of the LED is the negative terminals, also know as cathode.)

Connecting Pi to the Led

Now let's write a simple python program to blink the LED light.

  1. Remote desktop into the Raspberry Pi. See Instructions
  2. Open Thony Python IDE and write the following python code:
from gpiozero import LED
from time import sleep
led = LED(26)
while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
  1. Save and Run the program. The LED light should now be blinking. Press Ctrl + C to stop the program.

Here are more example code:

Example 1: Using if-conditions to control the LED light

import time
from gpiozero import LED
light = LED(26)
while True:
    userInput = input("Enter a for On: ")
    # Ask user for input
    print(userInput)
    # Check if the user pressed 'a'
    if (userInput == 'a'):
        light.on()
        # Wait for 1 second
        time.sleep(1)
    else:
        # Turn the light off if anything except for'a' is pressed
        light.off()
        time.sleep(1)

Example 2: Using try/except along with GPIO.cleanup() to control the LED light

import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup (26,GPIO.OUT)
try:
    while True:
        GPIO.output(26,True)
        sleep(2)
        GPIO.output(26,False)
        sleep(2)
except KeyboardInterrupt:
# User pressed Ctrl + C
# Reset GPIO settings
    GPIO.cleanup()
    print("GPIO Clean up")

Example 3: Controlling multiple LEDs

Here is an example sketch with multiple LEDs and code for it.

Blinking Multiple LEDs with Pi
# Import
import RPi.GPIO as GPIO
from time import sleep
# GPIO setup
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set GPIO pins
red = 26
green = 19
blue = 13
yellow = 6
# Set GPIO pins as output
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
GPIO.setup(yellow, GPIO.OUT)
# Set the number of times the light will blink
n_blinks = 20
# Set the starting sleep time
blink_time = 0.5
x = 0
try:
    while x < n_blinks:
        GPIO.output(red, GPIO.HIGH)
        sleep(blink_time)
        GPIO.output(green, GPIO.HIGH)
        sleep(blink_time)
        GPIO.output(blue, GPIO.HIGH)
        sleep(blink_time)
        GPIO.output(yellow, GPIO.HIGH)
        sleep(blink_time)
        GPIO.output(red, GPIO.LOW)
        sleep(blink_time)
        GPIO.output(green, GPIO.LOW)
        sleep(blink_time)
        GPIO.output(blue, GPIO.LOW)
        sleep(blink_time)
        GPIO.output(yellow, GPIO.LOW)

        sleep(blink_time + blink_time)
        print("blink_time :" + str(blink_time))
        print("x :" + str(x))
        # Decrease the sleep time after every look
        if(blink_time > .1):
            blink_time = blink_time - 0.05

        x = x + 1

except KeyboardInterrupt:
# User pressed Ctrl + C
# Reset GPIO settings
    GPIO.cleanup()
    print("GPIO Clean up")