Thursday, April 18, 2024
How ToIoT HardwaresRaspberry PiTutorials/DIY

Controlling LED with Raspberry Pi

Here you can found basic example of controlling an LED from Raspberry Pi.

Switching LED on/off from Raspberry Pi console

Follow instructions for how to turn the LED on/off from command line. In this example the LED is connected to GPIO pin 18.
You can connect the LED to any other GPIO pin as well.

#Switching LED on/off from Raspberry Pi console

$echo 18> /sys/clas/gpio/export
$cd /sys/class/gpio/gpio18

#set pin 18 direction to out
$echo out > direction
#Turn LED on
$echo 1 > value

# Turn LED off
$echo 0> value

Python program for blinking LED

Given below python code for blinking an LED connected to raspberry Pi every second. The code uses th RPi.GPIO module to control the GPIO on raspberry Pi. In this program we set pin 18 direction to output and then write True/False alternatively after a delay of one second.

# python program for blinking LED

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)

while True:
    GPIO.output(18,True)
    time.sleep(1)
    GPIO.output(18, False)
    time.sleep(1)
    


You may like also: How To Use Raspberry pi in a truely headless mode


Interfacing an LED an Switch with Raspberry Pi

In this example the LED is connected to GPIO pin 18 and switch is connected to pin 25. In the infinite while loop the value of pin 25 is checked and the state of LED is toggled if the switch is pressed. This example shows how to get input from GPIO pins and process the input and take some action. The action in this example is toggling the state of an LED.

# Interfacing an LED and Switch with Raspberry Pi
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# Switch Pin
GPIO.setup(25, GPIO.IN)
#LED Pin
GPIO.setup(18, GPIO.OUT)

state=false
def toggleLED(pin):
    state = not state
    GPIO.output(pin, state)

while True:
    try:
        if (GPIO.input(25) == True):
            toggleLED(pin)
        sleep(.01)
    expect KeyboardInterrupt:
        exit()
        

Python program for sending an email on switch press

Let us look at another example, in which the action is an email alert.

#python program for sending an email on switch press

import smtplib
from time import sleep
import RPi.GPIO as GPIO
from sys import exit

from_email = ''
receipients_list = ['receipient-email']
cc_list=[]
subject = 'hello'
message = 'Switch pressed on raspberry Pi'
username = ''
password = ''
server = 'smtp.gmail.com:587'

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)

def sendmail (from_addr , to_addr_list, cc_addr_list, subject, message, login, password, smtpsever):
    header = 'from : %s \n' %from_addr
    header += 'To: %s \n' %',' .join(to_addr_list)
    header += 'Cc: %s \n' % ',' .join (cc_addr_list)
    header += 'subject: %s \n\n' % subject
    message = header +message
    
    server = smtplib.SMTP(smtpserver)
    sever.starttls()
    sever.login (login,password)
    problems = server.sendmail (from_addr, to_addr_list, message)
    server.quit()
    
while True:
    try:
        if (GPIO.input(25) ==true):
            sendmail(from_email, receipients_list, cc_list, subject, message, usernmae, password, server)
        sleep(.01)
    expect KeyboardInterrupt:
        exit()
        

I hope you like this example tutorial Controlling LED with Raspberry Pi.


You may like also:


Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin