Flowing LED on RaspberryPi using Python
Flowing LED
raspberry pi
LED
python
In this article will make a flowing LED strip flowing in both directions using Python. If you need more info on LEDs and how can be connected please follow Blinking led on Raspberry Pi using Python tutorial. Also if you want to see the same flowing LED circuit in other programming languages, please check the references section.
Components
- 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
- 5 * 220Ω Resistor
- 5 * LED
- 1 * Breadboard
- 6 * Jumper wires
- software: Rpi.GPIO
Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).
Schematics
To do the schematics I used Fritzing software. For this tutorial, I will connect the cathode to the Raspberry Pi GPIO, also it is possible to connect the anode to the Raspberry Pi GPIO, check Blinking led on Raspberry Pi using Python tutorial for details.
Here is the schematic connecting all 5 LEDs using the cathode of the LED connected to the Raspberry Pi GPIO. Using the Pinout website for reference, we can check the numbering, starting from physical pin 11(BCM pin 17, Wiring Pi pin 0).
And the physical visualisation of the circuit.
Hardware
Now it's time to connect everything according to the diagram presented in the section above.
Software
Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.
Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:
$ sudo vim flowingLed.py
Copy/paste the following code.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
pins = [11, 12, 13, 15, 16]
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for pin in pins:
GPIO.setup(pin, GPIO.OUT) # Set all pins' mode is output
GPIO.output(pin, GPIO.HIGH) # Set all pins to high(+3.3V) to off led
def loop():
while True:
for pin in pins:
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
GPIO.output(pin, GPIO.HIGH)
for pin in reversed(pins):
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
GPIO.output(pin, GPIO.HIGH)
def destroy():
for pin in pins:
GPIO.output(pin, GPIO.HIGH) # turn off all leds
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
Test
To run the application execute the following command:
$ sudo python flowingLed.py
You should see the LEDs light flowing from one side to the other and back.
References
- Fritzing software used in this article to generate the schematic and all images.
- Pinout interactive website.
- Flowing LED on RaspberryPi using C
- Flowing LED on RaspberryPi using Java
- Flowing LED on RaspberryPi using Golang