How to control LED with Raspberry Pi

By Anshul Pal Jun 15, 2023 #IoT #Programming #RPi #SBC
How to control LED with Raspberry Pi

In this tutorial you can find some basic example of controlling an LED or how to control an LED with Raspberry Pi and GPIO Pins(General Purpose Input/Output ) interface in Raspberry Pi.

Controlling LED From Raspberry Pi Console

Follow the given instructions to control LED from command line. In this example, the LED is connected to GPIO pin 17. As per your choice, you can connect the LED to any other GPIO pin.

#Switching LED on/off from Raspberry Pi console

$echo 17> /sys/class/gpio/export
$cd /sys/class/gpio/gpio17
#set pin 17 direction to out
$echo out > direction
#Turn LED on
$echo 1 > value
# Turn LED off
$echo 0> value

GPIO Pins Interface in Raspberry Pi

GPIO Pins stands for General Purpose input/output pins. You can find these pins along the top edge of the board. On current Raspberry Pi boards, you can see 40 GPIO pins on the header of the Raspberry Pi boards.

Raspberry Pi GPIO pinout

Note The numbering of GPIO Pins are not in numerical order, GPIO pins 0 and 1 are present on the board (physical pins 27 and 28) but are reserved for advanced use.

You can see GPIO pinout in the terminal window by using a command  pinout  . This tool is provided by GPIO Zero Python Library, which is installed by default in Raspberry Pi OS.

Python Program for blinking LED

By using this code, we can blink our LED for 1sec which is connected to our Raspberry Pi .This code is based on an RPI.GPIO module to control the GPIO on Raspberry Pi. In this Python Program we set pin 18 direction to output and then our code write True/False, alternatively after a delay of one second.

# python program for blinking LED

import RPi.GPIO as GPIO
import time
while True:
    GPIO.output(18, False)

Controlling the brightness of an LED

If you want to change the brightness or control the power of an LED with a Python Program. Then the  RPIO.GPIO has a PWM (pulse width modulation) feature. With the help of this feature, we can easily control the power of an LED and its brightness.

import RPi.GPIO as GPIO

led_pin = 18
GPIO.setup(led_pin, GPIO.OUT)
pwm_led = GPIO.PWM(led_pin, 500)
while True:
        duty_s = input("Enter brightness (0 to 100)")
        duty = int(duty_s)

PWM is a clever technique where you vary the length of pulse while keeping the overall number of pulse per second (the frequency in Hz) constant.
You can change the PWM frequency by modifying this line:

 pwm_led = GPIO.PWM(led_pin,500)

GPIO Pins in a Safe State

It is very important for GPIO pins to be set to input whenever your program exists  so that there is less of a chance of an accidental  short on the GPIO  header, which could damage your raspberry Pi. It is also necessary that you should clean GPIO pins with a GPIO.cleanup method.

Code for clean GPIO Pins.

import RPi.GPIO as GPIO
import time
GPIO.setup(18, GPIO.OUT)
    while (True)
        GPIO.output(18, True)
        GPIO.output(18, False)
    print("Cleaning up")

Now, when you press Ctrl+C to close the program, it will call the GPIO.cleanup before the program exists.

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

By Anshul Pal

Hey there, I'm Anshul Pal, a tech blogger and Computer Science graduate. I'm passionate about exploring tech-related topics and sharing the knowledge I've acquired. With two years of industry expertise in blogging and content writing, I'm also the co-founder of HVM Smart Solution. Thanks for reading my blog – Happy Learning!

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *