How to Read Multiple Inputs Using Raspberry Pi

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 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.

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. The Raspberrypi is a board actually designed for helping computer education for remote schools but it is a nice platform for programmers especially beginners to explore various coding techniques.

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. To access the peripherals of the Broadcom controller of the Raspberrypi board using C language, a C library is available called “bcm2835” which can be downloaded and installed on the Linux OS. This tutorial explains how to read the value of multiple input pins simultaneously with the help of the multitasking feature of the Operating System.

How to Read Multiple Inputs Using Raspberry Pi

n 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 codes has been written for each of the input pins which continuously reads the particular input pin and writes the value to a corresponding output pin. The function ‘bcm2835_gpio_fsel ()’ is used to set the pins as input and 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 codes are saved as .c files of names, say button1.c, button2.c, button3.c and button4.c. The codes can be separately compiled and made executable files of names, say button1, button2, button3 and button4. It is suggested to keep all the .c files and the executable files in a single folder for this particular project.

The user can run any of the input reading programs from the command line, for example to execute the file ‘button1’, the following command can be used:

./button1

The user can perform multi-tasking on them by running those executable files from the command line as background process using the following commands.

./button1 &

./button2 &

./button3 &

./button4 &

Once it is done the Operating System has four separate codes running parallel which reads four separate input pins and write the values to a corresponding output pins. Since the buttons are connected to the input pins and the LEDs are connected to the output pins, the user can press one or more button at a time and the corresponding LED glows.

All the above process can be generated as ‘Child processes’ of a single ‘Parent process’ with the help of fork () and execve () functions. As soon as the fork () function is called a new Child process is created which starts executing the following statements from the point where the fork () is being called. The process is called a Child process because it is identical to the Parent process in terms of memory like the variables, functions etc. The Child is but independent from the Parent as it has its own process id, and the memory is not shared with the Parent process.

The fork () function normally follows by a function call to the execve () function which will execute an executable file as a Child process. In this particular project the fork () function calls the execve () function which then executes one of the button reading code. The code uses fork () function four times with execve () function to create four input reading Child processes.

To execute the ‘button1’, ‘button2’, ‘button3’, and ‘button4’ as Child processes of a main process, simply open up a new .c file in vim editor  and write down the following simple code;

#include <unistd.h>

int main ( void )

{

if ( !fork () )

{

execve ( “./button1”, 0, 0 );

_exit ( 0 );

}

else;

 

if ( !fork () )

{

execve ( “./button2”, 0, 0 );

_exit ( 0 );

}

else;

if ( !fork () )

{

execve ( “./button3”, 0, 0 );

_exit ( 0 );

}

else;

if ( !fork () )

{

execve ( “./button4”, 0, 0 );

_exit ( 0 );

}

else;

}

 

Read more: How to Read Multiple Inputs Using Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top