The Raspberry pi is a mini computer which is designed in a single board with all the essential components required for running an operating system. The Raspberrypi board is powerful enough to run large operating systems like Linux, Mac and Windows. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board.
The Raspberrypi is a board actually designed for helping computer education for remote schools but it is a good platform for programmers especially beginners to explore various coding techniques. The Raspberry pi is a device which uses the Broadcom controller chip which is a SoC (System on Chip). This SoC has the ARM11 processor which runs on 700 MHz at its core. This powerful processor and the controller having the peripherals like timers, interrupt controller, GPIO, PCM / I2S, DMA controller, I2C, SPI slave, PWM, UART, USB etc. The immediate advantage of having an Operating System like Ubuntu running on such an embedded system device is multitasking.
The Multi-tasking Operating Systems can run several processes at a time creating and effect of parallel processing with the help of the high speed processor. The Linux Operating Systems provides Multi-User-Multitasking. Those who have worked on simple microcontrollers know how difficult it is to continuously check for an input pin status without affecting the other task of the microcontroller like displaying and SSD, playing a music etc. In a Multitasking environment of an Operating system a separate process can be created for checking the status of input pin and then notify the main process whenever their status changes.
In 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. To access the pins that coming out of the Broadcom controller the C library “bcm2835” has been downloaded and installed.
There are eight general purpose IO pins on the 13*2 pin connectors of the Raspberrypi board and among them four pins has been selected as input and then remaining four pins as output. The input pins are connected to push button and are pulled down using 1K resistors. The output pins are connected to the LEDs through another set of 1K resistors.
Four separate input reading processes has been created as Child process using the fork () function for each of the input pins. This Child processes continuously reads the particular input pin and sends a signal to the Parent process using kill () function whenever the status of the pin changes.
In this particular project a Parent creates 4 Childs processes which are then used to read the status of the four input pins independently. The Child processes are made to send a signal ‘SIGUSR1’ to the Parent process whenever the status of the input pin changes. As soon as the Parent process receives a signal, it reads the value of all the four input pins and writes the same to the corresponding output pins. This forms a Process System made up of 4 Child process and a Parent process where the Parent is free to do its work, but the Child can get the attention of the Parent by sending signal. The diagrammatic representation of the Process System is given below:
Fig. 2: Block Diagram of Process System for reading inputs using signal in Raspberry pi
For separate input reading and output writing the functions from the library <bcm2835.h> is used. The function ‘bcm2835_gpio_fsel ()’ is used to set the pins as input or output and the function ‘bcm2835_gpio_set_pud ()’ is used to turn off the pull up/down from the pin. The function ‘bcm2835_gpio_lev ()’ is used to read the value of the pin and ‘bcm2835_gpio_write ()’ is used to write the value to the output pin.
The code for a Child process which will send a signal to the Parent whenever the state of an input pin defined as IN_PIN1 changes is given as follows;
void signal_on_state_change_pin1 ( void )
{
while ( 1 ) // for continuous read
{
if ( bcm2835_gpio_lev ( IN_PIN1 ) ) // checking if pin is high
{
delay ( 50 );
if ( bcm2835_gpio_lev ( IN_PIN1 ) ) // confirming pin high after a delay
{
kill ( getppid(), SIGUSR1 ); // send SIGUSR1 signal to parent
do // loop until pin becomes low again
{
while ( bcm2835_gpio_lev ( IN_PIN1 ) ) // wait till pin low
delay ( 1 );
delay ( 50 ); // confirm low after this delay
}
while ( bcm2835_gpio_lev ( IN_PIN1 ) );
kill ( getppid(), SIGUSR1 ); // send SIGUSR1 signal to parent
}
else;
}else;
delay ( 1 );
}
}
The signal handler is installed in the Parent process which for handling the SIGUSR1 signals from the Child process. For each and every SIGUSR1 signal the signal handler reads the values of all the input pins where the buttons are connected and writes the same to the corresponding output pins where the LEDs are connected.
void button_signal_handler ( int signo )
{
bcm2835_gpio_write ( OUT_PIN1, bcm2835_gpio_lev ( IN_PIN1 ) );
bcm2835_gpio_write ( OUT_PIN2, bcm2835_gpio_lev ( IN_PIN2 ) );
bcm2835_gpio_write ( OUT_PIN3, bcm2835_gpio_lev ( IN_PIN3 ) );
bcm2835_gpio_write ( OUT_PIN4, bcm2835_gpio_lev ( IN_PIN4 ) );
}
*** Note that this project using the Raspberrypi board version 2 and hence in the code pin13 is defined as
#define OUT_PIN2 RPI_V2_GPIO_P1_13.
Those who have the version 1 board can use
#define OUT_PIN2 RPI_ GPIO_P1_13.
Project Source Code
###
#include <bcm2835.h> #include <unistd.h> #include <signal.h>
#define IN_PIN1 RPI_GPIO_P1_07
#define IN_PIN2 RPI_GPIO_P1_22
#define IN_PIN3 RPI_GPIO_P1_18
#define IN_PIN4 RPI_GPIO_P1_16#define OUT_PIN1 RPI_GPIO_P1_15
#define OUT_PIN2 RPI_V2_GPIO_P1_13
#define OUT_PIN3 RPI_GPIO_P1_12
#define OUT_PIN4 RPI_GPIO_P1_11void set_pins_input ( void );
void set_pins_output ( void );
void set_output_pins_low ( void );
void button_signal_handler ( int signo );
void signal_on_state_change_pin1 ( void );
void signal_on_state_change_pin2 ( void );
void signal_on_state_change_pin3 ( void );
void signal_on_state_change_pin4 ( void );pid_t child_id [ 4 ];
int main ( void )
{
int i;if (!bcm2835_init())
return 1;
set_pins_output ();
set_output_pins_low ();
set_pins_input ();
delay ( 100 );
signal ( SIGUSR1, button_signal_handler );if ( ! ( child_id [ 0 ] = fork () ) )
{
signal_on_state_change_pin1 ();
_exit ( 0 );
}
else;if ( ! ( child_id [ 1 ] = fork () ) )
{
signal_on_state_change_pin2 ();
_exit ( 0 );
}
else;if ( ! ( child_id [ 2 ] = fork () ) )
{
signal_on_state_change_pin3 ();
_exit ( 0 );
}
else;if ( ! ( child_id [ 3 ] = fork () ) )
{
signal_on_state_change_pin4 ();
_exit ( 0 );
}
else;while ( 1 )
delay ( 1 );bcm2835_close();
return 0;
}void signal_on_state_change_pin1 ( void )
{
while ( 1 )
{
if ( bcm2835_gpio_lev ( IN_PIN1 ) )
{
delay ( 50 );
if ( bcm2835_gpio_lev ( IN_PIN1 ) )
{
kill ( getppid(), SIGUSR1 );
do
{
while ( bcm2835_gpio_lev ( IN_PIN1 ) )
delay ( 1 );
delay ( 50 );
}
while ( bcm2835_gpio_lev ( IN_PIN1 ) );kill ( getppid(), SIGUSR1 );
}
else;
}else;
delay ( 1 );
}
}void signal_on_state_change_pin2 ( void )
{
while ( 1 )
{
if ( bcm2835_gpio_lev ( IN_PIN2 ) )
{
delay ( 50 );
if ( bcm2835_gpio_lev ( IN_PIN2 ) )
{
kill ( getppid(), SIGUSR1 );
do
{
while ( bcm2835_gpio_lev ( IN_PIN2 ) )
delay ( 1 );
delay ( 50 );
}
while ( bcm2835_gpio_lev ( IN_PIN2 ) );
kill ( getppid(), SIGUSR1 );
}
else;
}else;
delay ( 1 );
}
}void signal_on_state_change_pin3 ( void )
{
while ( 1 )
{
if ( bcm2835_gpio_lev ( IN_PIN3 ) )
{
delay ( 50 );
if ( bcm2835_gpio_lev ( IN_PIN3 ) )
{
kill ( getppid(), SIGUSR1 );
do
{
while ( bcm2835_gpio_lev ( IN_PIN3 ) )
delay ( 1 );
delay ( 50 );
}
while ( bcm2835_gpio_lev ( IN_PIN3 ) );kill ( getppid(), SIGUSR1 );
}
else;
}else;
delay ( 1 );
}
}void signal_on_state_change_pin4 ( void )
{
while ( 1 )
{
if ( bcm2835_gpio_lev ( IN_PIN4 ) )
{
delay ( 50 );
if ( bcm2835_gpio_lev ( IN_PIN4 ) )
{
kill ( getppid(), SIGUSR1 );
do
{
while ( bcm2835_gpio_lev ( IN_PIN4 ) )
delay ( 1 );
delay ( 50 );
}
while ( bcm2835_gpio_lev ( IN_PIN4 ) );kill ( getppid(), SIGUSR1 );
}
else;
}else;
delay ( 1 );
}
}void button_signal_handler ( int signo )
{
bcm2835_gpio_write ( OUT_PIN1, bcm2835_gpio_lev ( IN_PIN1 ) );
bcm2835_gpio_write ( OUT_PIN2, bcm2835_gpio_lev ( IN_PIN2 ) );
bcm2835_gpio_write ( OUT_PIN3, bcm2835_gpio_lev ( IN_PIN3 ) );
bcm2835_gpio_write ( OUT_PIN4, bcm2835_gpio_lev ( IN_PIN4 ) );
}void set_output_pins_low ( void )
{
bcm2835_gpio_write ( OUT_PIN1, LOW);
bcm2835_gpio_write ( OUT_PIN2, LOW);
bcm2835_gpio_write ( OUT_PIN3, LOW);
bcm2835_gpio_write ( OUT_PIN4, LOW);
}void set_pins_output ( void )
{
bcm2835_gpio_fsel ( OUT_PIN1, BCM2835_GPIO_FSEL_OUTP );
bcm2835_gpio_fsel ( OUT_PIN2, BCM2835_GPIO_FSEL_OUTP );
bcm2835_gpio_fsel ( OUT_PIN3, BCM2835_GPIO_FSEL_OUTP );
bcm2835_gpio_fsel ( OUT_PIN4, BCM2835_GPIO_FSEL_OUTP );
}void set_pins_input ( void )
{
bcm2835_gpio_fsel ( IN_PIN1, BCM2835_GPIO_FSEL_INPT );
bcm2835_gpio_set_pud ( IN_PIN1, BCM2835_GPIO_PUD_OFF );bcm2835_gpio_fsel ( IN_PIN2, BCM2835_GPIO_FSEL_INPT );
bcm2835_gpio_set_pud ( IN_PIN2, BCM2835_GPIO_PUD_OFF );bcm2835_gpio_fsel ( IN_PIN3, BCM2835_GPIO_FSEL_INPT );
bcm2835_gpio_fsel ( IN_PIN3, BCM2835_GPIO_FSEL_INPT );bcm2835_gpio_set_pud ( IN_PIN4, BCM2835_GPIO_PUD_OFF );
bcm2835_gpio_set_pud ( IN_PIN4, BCM2835_GPIO_PUD_OFF );
}###
Circuit Diagrams
Project Components
Project Video
Filed Under: Raspberry pi
Filed Under: Raspberry pi
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.