How to Run a Code Automatically On Boot

The Raspberrypi is a low cost minicomputer board designed to be used as a PC for providing computer education for remote schools, where costly desktops are not available. Unlike the Desktop PCs, it is using ARM11 processor based microcontroller which can provide almost same processing power as low cost Desktop processors. Moreover the Raspberry pi board provides lot of pin outs like GPIO pins, serial communication pins etc. which enable them to be used in embedded system applications.

The Raspberrypi board uses Broadcom microcontroller which uses the ARM11 as its processor. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. For accessing the Broadcom chip peripherals like timers, interrupt controller, GPIO, I2C, SPI, PWM, UART etc. using C code there is a library file called “bcm2835”.

How to Run a Code Automatically On BootIn this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.

A simple LED blinking code is written using the functions from the library file <bcm2835> as shown in Code 1 Tab and is made executable.

The user can use vim editor to write the code and save into a file, say “blink.c”. From command line itself use the following command to compile the code with the new library file to generate the executable file “blink”

cc blink.c -lbcm2835 -o blink

The code can be executed using the following command;

./blink

As soon as the user enters the command the LED connected to the pin number 11 starts blinking. It can be noted that the no other process or commands can be executed within the same command line window until the process is terminated using CTRL+C. Such kind of process execution is called ‘foreground process’ execution which doesn’t allow other process to be executed till it terminates.

The ‘foreground process’ if it is started immediately after the booting process it will simply blocks all other process till it terminates. The process should be executed in such a way that it runs in the background of other process which can be executed later. Such kind of a process is called ‘background process’.

Any process can be executed as a background process using the ‘&’ symbol, for example the executable file ‘blink’ can be executed in background using the command;

./blink &

Now try to exit from the current folder to some other folder and try to enter same command ‘./blink’ and it won’t work. The reason is that all the commands entered at the command line are handled and executed by an OS program called ‘Shell’. For each and every command entered through the command line the shell will search for in some particular folders for the binary file having the same name as the command entered. For example if a command for listing the files ‘ls’ is entered the Shell will look for a binary file named ‘ls’ at folders like current folder, /bin, /sbin etc.

The program execution using ./blink was successful when it was done within the same folder where the executable file named ‘blink’ exist, because Shell will always search for the binary file at the current folder. But when entered another folder where there is no executable file the command fails because the Shell was not able to find the executable in the current folder or at /bin, /sbin etc.

To execute the program from anywhere in the file system simply copy paste the executable file in the folders where the Shell used to search for binary files having the name same as the command entered. When the binary file ‘blink’ is placed in the folder ‘/bin’ it can be executed from anywhere and to do this from the current folder where the binary file exists, all the user have to do is to enter the following ‘copy command;

cp blink /bin

After copying the executable file in the /bin directory, by adding the command to execute the same file in an OS specific file it can be made executable by the Shell which runs immediately after the system boots. The file to be edited is called “rc.local” which exist in the folder “/etc”. Open that file and using vim editor and add the required command for executing the ‘blink’ file.

//------------------------------------------ led blinking code --------------------------------------------------//
#include <bcm2835.h>
#define PIN RPI_GPIO_P1_11

int main() {if (!bcm2835_init()) // initialize the library
return 1;

bcm2835_gpio_fsel (PIN, BCM2835_GPIO_FSEL_OUTP); // Set the pin to be an output

while (1)
{bcm2835_gpio_write(PIN, HIGH); // Turn it on
bcm2835_delay(500); // wait a bit
bcm2835_gpio_write(PIN, LOW); // turn it off
bcm2835_delay(500); // wait a bit
}bcm2835_close(); // close the library

return 0;}

 

For more detail: How to Run a Code Automatically On Boot

Scroll to Top
Scroll to Top
Read previous post:
The RaspberryPI – Putting Fun Back Into Computing With A Small Price Tag

I recently acquired a Raspberry PI Model B. as a new gadget to toy with and I must say I...

Close