Python code to control LED using Raspberry Pi
Using the Raspberry Pi to control an LED might seem like a basic or boring task. However, the same hardware and programming concepts used to control an LED can be used to control a wide variety of sensors and modules. Learning how to control an LED with the Raspberry Pi’s GPIO pins will open up a whole new variety of devices you can use with the Raspberry Pi.
Now here are the steps to build the circuit:
- Connect one wire between one GND (ground) pin of the Raspberry Pi and the blue line of the breadboard.
- Take the LED and check the 2 legs. You will see that one is shorter than the other. Plug the shorter leg to the blue line (now connected to GND), and the longer to any other connector. You can either directly connect the shorter leg to the blue line, or add an additional short male-to-male connector (like in the picture), the result is the same.
- Plug one leg of the resistor to the same line as the longer leg of the LED, and the other leg of the resistor to a different line.
- Finally, to close the circuit plug one wire between the same line as the other leg of the resistor, and the GPIO number 17 (more on Raspberry Pi pins and GPIOs). This is the 6th pin on the GPIO header, starting from the left, on the inside side.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(14,GPIO.OUT) # While loop while True: # set GPIO14 pin to HIGH GPIO.output(14,GPIO.HIGH) # show message to Terminal print "LED is ON" # pause for one second time.sleep(1) # set GPIO14 pin to HIGH GPIO.output(14,GPIO.LOW) # show message to Terminal print "LED is OFF" # pause for one second time.sleep(1)
Python Program:
Use below python code to control the LED.
import RPi.GPIO as GPIO import time PIN = 36 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN, GPIO.OUT) #LED output pin while True: GPIO.output(PIN, 0) #Turn OFF LED time.sleep(1) GPIO.output(PIN, 1) #Turn ON LED time.sleep(1)
On your Raspberry Pi, open IDLE (Menu > Programming > Python 2 (IDLE)).
Open a new project go to File > New File. Then type (or copy and paste) the following code:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(18, GPIO.OUT) GPIO.setup(22, GPIO.OUT) GPIO.setup(23, GPIO.OUT) GPIO.output(17, True) time.sleep(3) GPIO.output(17, False) time.sleep(1) GPIO.output(18, True) time.sleep(3) GPIO.output(18, False) time.sleep(1) GPIO.output(22, True) time.sleep(3) GPIO.output(22, False) time.sleep(1) GPIO.output(23, True) time.sleep(3) GPIO.output(23, False)
Save your project as multilights.py (File > Save As) in your Raspberry Pis Documents folder.
On your Raspberry Pi open Terminal (Menu > Accessories > Terminal) and navigate to your Documents folder by typing the following:
cd /home/pi/Documents
You can now run your new script by typing the following:
python multilights.py
The lights will take it, in turn, to switch on and off. The above script uses time. sleep command to create a pause between each step, making each light stay on for 3 seconds and to wait for 1 second before turning the next light on.
Recommended Articles
Sample Sheet Metal Quiz MCQ’s Questions and Answers
What is Machine Learning? A Primer for the Epidemiologist
Mechanical Engineering Notes