Saturday 29 April 2017

Interfacing of Raspberry pi with led and Switch

Interfacing of Raspberry pi with led and Switch:

Now ready to write code to switch and Led.  Turn on your Raspberry Pi and Open the terminal window.
Create a new text file “switch1.py” by typing the following:
Sudo nano switch1.py
After that write following code to the Text editer page:

import os
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) #board usage gpio pin , bcm usasge pin mnumber
GPIO.setup(3,GPIO.IN)
GPIO.setup(5,GPIO.OUT)
print(" interfacing of Switch and Led")

while True:
    if( GPIO.input(3)== False):
        print("Switch is Pressed")

            GPIO.output(5,GPIO.HIGH)
            sleep(1)
           
    else:
        GPIO.output(5,GPIO.LOW)
            sleep(1)
        sleep(2)
       
After writing the code we checked it, save and exit the text editor with
 “Ctrl + x” then “y” then “enter”.
To Running this Code Type:

sudo python switch1.py
then
You will see the LED turn on when switch is pressed.

Circuit Diagram:



0 comments:

Post a Comment

if you have any doubt please let me know