You already know how to run the python script in the command terminal as explained in the previous tutorial How to make the first python program on BBB . When you run the python script in SSH command prompt, you need to make BBB connection to PC because you are accessing BBB through SSH connection. If you want to run python script on BBB without connecting it to PC, external power is required.
You can run the python script when turning on BBB without any command prompt or command. This work is done by Crontab.
Make a simple blinking LED program
Let's run the LED blinking program on Beaglebone Black startup.
Required tools:
- Black Beaglebone
- LED
- 330Ω resistor
- Breadboard Female to Female Connectors
Software environment configuration
Install the latest version of python on BBB as explained in the tutorial How to make the first python program with Beaglebone Black. Install the adafruit python-GPIO library called adafruit_BBIO.
Working
It is a simple black Beaglebone learning tutorial. Here I connected two LEDs with black Beaglebone GPIO pins. When the script is running, both LEDs turn on and off for one second. After this run five times, the pin status and configuration become clear.
Description
Let's first prepare the circuit connection. Take a breadboard and supply VCC and ground from BBB to the breadboard line. Connect 3.3V power to pin number 3 of the P9 head and ground to pin number 2 of the P8 head. Connect the negative terminal of both LEDs to ground and the positive to the 330 Ohm resistor. Connect the other end of both resistors to pin numbers 8 and 9 of the P8 header. Supply power to the black Beaglebone by connecting it to the PC via a USB cable. Now your circuit is prepared.
Fig. 1: BeagleBone black boot prototype
Fig. 2: Image showing automatic execution of the Python script on BeagleBone Black Boot
Open the command terminal and access Beaglebone black through SSH as explained in introduction to Beaglebone black. Create a new file using the touch command with .py extension (i.e. led.py). Open the file with any text editor (i.e. nano, vim etc.) and write code in python language.
Auto-run script at startup
Crontab is a task management tool built into Debian. We can add new Cron Job in which a Python script will be executed on startup.
Follow these steps to run the python script on BBB startup:
Step 1: Connect to the internet and turn on BBB. Access BBB with SSH via PC.
Step 2: Install crontab from the debian repository following the command line:
sudo apt-get install crontab
Figure 3: Screenshot of the Linux console installing Crontab
Step 3: Open crontab and edit to run on startup. Enter the following instructions to open the crontab edit file:
crontab –e
Fig. 4: Linux console editing Crontab screenshot
Step 4: Scroll down and add the following line to run led.py on BBB startup:
@reboot sudo python /root/python_program/led.py &
Fig. 5: Screenshot of Python script written in Nano Editor on Linux console
Here, /root/python_program/led.py is the file path where led.py is located. Don't forget to add & to run the background process at the end of the line. Save the file and exit it.
Step 5: If you want to see it suddenly, restart your system:
restart
Wait for some time to restart. After rebooting, the led.py script runs automatically at startup and the LED starts blinking.
Step 6: If you don't want to restart, remove the USB cable and plug it back in. After initialization, led.py will run automatically.
Project source code
###
# LED blinking Tutorial
GPIO.cleanup
###
Project video