Turning on & off an LED is a right of passage for all embedded programming. This guide will go through writing your first python script for EdgePi to control the indicator lights on the EdgePi.
Start by looking at the top of your EdgePi. You'll see to the left a 2 by 4 array of 8 indicator LEDs, labeled O1 - O8. These are the LEDs we'll be controlling.
Before we setup the environment and program anything, please note that there are several daemons running on the EdgePi that help connect it to the EdgePi Portal. These daemons are not designed to work together with any local projects running on your EdgePi, and can result in reading invalid inputs. For our cases, we don't have to worry about them, but to temporarily disable the daemons you can do the following
# first, ssh into your EdgePi
sudo systemctl stop edgepi-device-manager.service
sudo systemctl stop edgepi-data-collector.service
sudo systemctl stop edgepi-data-aggregator.service
sudo systemctl stop edgepi-aws-iot.service
Now that's out of the way, we need to make sure our editor is configured correctly so we can start coding. See the VS Code setup page for more info if you plan on using VS Code.
Make a new folder for the project, then open the VS Code explorer in that folder
If needed, enter the password pi
again and accept the permissions prompt. Now you should be ready to edit files and run commands on the EdgePi
Install the virtual environment (venv
) package
a. It's best to have projects and the associated packages, only installed with-in the confines of the virtual environment
b. Open a terminal by going to the top left corner of the screen, and pressing New Terminal
, under Terminal
. Make sure that this terminal is on the EdgePi
c. Type $ sudo apt-get install python3-venv
in the terminal to install the venv
package
Create a new virtual environment for this project. This will ensure you don't overwrite the dependencies for other projects on your machine
a. Do $ python -m venv led_proj
to create a new virtual environment
b. Activate this virtual environment with $ source led_proj/bin/activate
c. You should now be within your virtual environment workspace
The EdgePi SDK is a layer of code installed on the EdgePi that makes it much easier to write programs that interface with the inputs and outputs. The EdgePi SDK documentation can be found on Py Pi here.
From within your virtual environment, enter $ python3 -m pip install edgepi-python-sdk
to install the SDK.
The EdgePi SDK is separated into different modules, such as the Thermocouple Module or the LED module. We're interested in the latter.
Let's start by turing all the LEDs off, except for O1.
To do this, we need to create a new file. Let's call it leds.py
. In leds.py
, type the following code:
from edgepi.led.edgepi_leds import EdgePiLED
from edgepi.led.led_constants import LEDPins
# this python object lets us control all the LEDs
led_array = EdgePiLED()
# create a list of all the LED identifiers
led_names = [
LEDPins.LED1, LEDPins.LED2, LEDPins.LED3, LEDPins.LED4,
LEDPins.LED5, LEDPins.LED6, LEDPins.LED7, LEDPins.LED8,
]
# loop through and turn each LED off
for led in led_names:
led_array.turn_led_off(led)
# enable only the first LED
led_array.turn_led_on(LEDPins.LED1)
Finally, we can run this program by typing python leds.py
in the terminal on our EdgePi. You should see now that only the top left LED is enabled.
We aren't limited to just the EdgePi SDK, you can use any function from the python standard library, or any other library that supports the Rasberry Pi.
Next, let's play with timing a little and make our LEDs blink! Type the following into leds.py
:
import time, random
from edgepi.led.edgepi_leds import EdgePiLED
from edgepi.led.led_constants import LEDPins
# create a list of all the LED identifiers, like before
led_array = EdgePiLED()
led_names = [
LEDPins.LED1, LEDPins.LED2, LEDPins.LED3, LEDPins.LED4,
LEDPins.LED5, LEDPins.LED6, LEDPins.LED7, LEDPins.LED8,
]
# do 200 iterations (10s)
for i in range(200):
for led in led_names:
# randomly toggle each LED
if random.random() < 0.1:
led_array.toggle_led(led)
# wait 50 ms
time.sleep(0.05)
You should see the LEDs randomly blink for 10s.
Congrats! Now you know how to run python code on the EdgePi, control the LEDs, and use functions from the python standard library.