Friday 5 December 2014

Program to blink LED

STEP 1:

Open LXTerminal.

Create a file with .py extension

Ex: nano example.py

Nano is a type of editor used to write the code in that file named example.

Type the following code in that file by insert key or by typing letter I on keyboard.

Program:

#! /usr/bin/python
Import time                                         //importing library called time
Import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup (17, GPIO.OUT)          //configuring 17th pin as output
GPIO.setup (27, GPIO.OUT)         //configuring 27th pin as output
                                                           #Turn LED’S ON
GPIO.output (17, GPIO.HIGH)
GPIO.output (27, GPIO.HIGH)
Time.sleep (1)                                   //sleep for one second
                                                          #Turn LED’S OFF
GPIO.output (17, GPIO.LOW)
GPIO.output (27, GPIO.LOW)
Time.sleep (1)
                                                         # Turn LED’S ON
GPIO.output (17, GPIO.HIGH)
GPIO.output (27, GPIO.HIGH)
Time.sleep (1)
                                                       #Turn LED’S OFF
GPIO.output (17, GPIO.LOW)
GPIO.output (27, GPIO.LOW)
GPIO.cleanup

LED Blinking using WHILE Loop:

#! /usr/bin/python
Import time
Import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup (17, GPIO.OUT)    //configuring 17th pin as output
GPIO.setup (27, GPIO.OUT)    //configuring 27th pin as output
                                                     #while basically means loop foreverIt executes the code if some                                                               condition is true.
WHILE 1:                                   //give one tab in the while loop to overcome the indentation error                                                            while compiling
          GPIO.output (17, GPIO.HIGH)
          GPIO.output (27, GPIO.HIGH)
          Time.sleep (1)
          GPIO.output (17, GPIO.LOW)
          GPIO.output (27, GPIO.LOW)
          Time.sleep (1)


To check the output:Type python example.py in lx-terminal.


Output 1:

Output 2:

0 comments:

Post a Comment