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


while True:
    GPIO.output(18, False)

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


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

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

while True:
        if (GPIO.input(25) == True):
    expect KeyboardInterrupt:

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']
subject = 'hello'
message = 'Switch pressed on raspberry Pi'
username = ''
password = ''
server = ''

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.login (login,password)
    problems = server.sendmail (from_addr, to_addr_list, message)
while True:
        if (GPIO.input(25) ==true):
            sendmail(from_email, receipients_list, cc_list, subject, message, usernmae, password, server)
    expect KeyboardInterrupt:

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

You may like also:

Harshvardhan Mishra

i am an IoT Enthusiast and Part Time Tech Blogger.

One thought on “Controlling LED with Raspberry Pi

Leave a Reply

%d bloggers like this: