Today we will learn to use GPIO pin to Blink three LED 5 times using using While loop in python scripts
Prerequisite
- Read my previous blog for basic understanding
- Basic understanding of Linux terminal commands
- Basic understanding python coding
- Basic knowledge of basic circuits
Have connected 3 LED with resistor on Pin 17, 22,27. LED will always have resistor in path.
Note : Now we have started to intend commands in while loop.Using this intend, Python know when to end while loop.
#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
count = 0
while count < 5:
GPIO.output(17, 1)
GPIO.output(27, 1)
GPIO.output(22, 1)
print “LED off”
sleep(1)
GPIO.output(17, 0)
GPIO.output(27, 0)
GPIO.output(22, 0)
print “LED off”
sleep(1)
count += 1
GPIO.cleanup()
Explanation
Only New command is explained in detail below, Other are already explained in previous Blog
#!/usr/bin/python |
Make Python script executable . Not Mandatory at this stage of programs. You may also run script with command. python scriptname.py |
count = 0 |
Python Variable with value zero. Python has five standard data types −
When we declared count = 0.Python understands its Number variable. |
while count < 5 |
Keep running below program till while condition is reached, in above program value of we reach value 5. Note we have count += 1 in While loop, this increment counter each time loops is executed while 1: >> Means Infinite loop |
GPIO.cleanup() |
RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports you’ve used. Not Mandatory at basic level of programs |