How to Use Timer in Raspberry Pi

The Raspberry Pi is a device involving the Broadcom controller chip that is a SoC (System on Chip). Their SoC has a strong ARM11 processor that is based on 700MHZ at its core. With the enhanced peripherals such as timers, interrupt controller, GPIO, PCM / I2S, DMA controller, I2C, SPI slave, PWM, UART, USB, graphical processing unit (GPU) which includes VideoCore, MPEG-2 and MPEG-4, 512 MB SDRAM this can be considered as a mini-computer.

Even the cheapest board like the Raspberry board can support big operating systems such as Linux, MAC, and WINDOWS. Above all, for any kind of programming and development, the Linux operating system especially Ubuntu is preferred. Operating systems such as the Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS, and the Rasbian and Ubuntu versions are obtainable for the Raspberrypi board. The apparent benefit of running an Operating System such as Ubuntu on an embedded system device such as Raspberrypi is the ability to have Multiple Users and Multiple Tasks.

How to Use Timer in Raspberry Pi

In this project, the Raspberry board is loaded with Ubuntu, and for ease of controlling remote VNC is used. Internet is also ably connected to the Raspberry board. By establishing an assessment of the connector port of the Raspberry board, it can be easily found that there are 26 connectors available. All the connector pins are removed by using female connectors which are 13*2 pins and at the other terminals of their wire, 26 pins Burg stick male connectors are connected. The Burg stick male connectors enable every pin out of the Raspberry board to be poked into the holes on a breadboard. To write to the pin or read from the pin that is coming out of the Broadcom controller of the Raspberry board using C language there is a C library available and that is “bcm2835” that has been downloaded and installed.

A signal is raised to provide information to the process that some event or condition has taken place that warranted its attention. The signals are utilized to announce different events and all the signals are characterized by their numbers. The complete list of all the signals available in the OS and their corresponding signal numbers can be known by using the following command.

kill -l

The following table gives a list of the most common signals that a process might encounter in an Operating System;

 NAME NUMBER DESCRIPTION
SIGHUP 1 Linux sends a process this signal when it becomes disconnected from a terminal.
SIGINT 2 Linux sends a process this signal when the user tries to end it by

pressing CTRL+C.

SIGILL 4 Linux sends a process this signal when it attempts to execute an illegal instruction.
SIGABRT 6 Linux sends a process this signal to the process when the process calls the ‘abort ()’ function
SIGFPE 8 Linux sends a process this signal when it has executed an invalid floating-point math instruction
SIGKILL 9 Linux sends a process this signal to end it immediately
SIGUSR1 10 User programs can send this signal to other process
SIGUSR2 12 User programs can send this signal to other process
SIGSEGV 11 Linux sends a process this signal when the program has attempted an invalid memory access
SIGPIPE 13 Linux sends a process this signal when the program has attempted to access a broken data stream, such as a socket connection that has been already closed
SIGALRM 14 A process can receive this signal from the Linux using the function alarm(), after a time period mentioned in its argument.
SIGTERM 15 Linux sends a process this signal requesting it to terminate
SIGCHLD 17 Linux sends a process this signal when a child process exits
SIGXCPU 24 Linux sends a process this signal when it exceeds the limit of

CPU time that it can consume.

SIGVTALRM 26 A process can receive this signal from the Linux using the function setitimer (), after a time period mentioned in its argument.

This particular project is based on continually being informed of and processing the signal number 14, the previously mentioned SIGALRM. A process gets the SIGALRM signal from the OS by using a function called ‘alarm ()’ and the function for its definition is found in the header file ‘<signal.’ h>.

Then the OS will send the SIGALRM to the process next to the period mentioned in the parameter that has been passed while calling the function. SINGLE will handle only a single call to the alarm () and will respond to it with a single SIGALRM signal only. For the reception of SIGALRM at a continuous interval of time, the same function has to be called after every reception of SIGALRM. The better method, therefore, would be to take advantage of setting a timer, which can proceed to generate the SIGALRM signals in intervals of time.

The ‘set interval timer’ function can be used to set a timer which can continuously generate the SIGALRM signals at specified intervals of time.

#include <bcm2835.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define PIN1 RPI_GPIO_P1_11
#define PIN2 RPI_GPIO_P1_12
#define PIN3 RPI_GPIO_P1_24
#define PIN4 RPI_GPIO_P1_15
#define PIN5 RPI_GPIO_P1_16
#define PIN6 RPI_GPIO_P1_18
#define PIN7 RPI_GPIO_P1_22
#define PIN8 RPI_GPIO_P1_07
void sig_handler ( int signo );
void set_pins_output ( void );
void set_all_pin_low ( void );
void timer_seconds ( long int seconds );
void timer_useconds ( long int useconds );
int main(){if (!bcm2835_init())
return 1;set_pins_output ();set_all_pin_low ();
 signal ( SIGALRM, sig_handler );timer_seconds ( 2 );while ( 1 );

bcm2835_close();return 0;}void timer_seconds ( long int seconds ){struct itimerval timer1;How to Use Timer in Raspberry Pi

timer1 . it_interval . tv_usec = 0;
timer1 . it_interval . tv_sec = seconds;
timer1 . it_value . tv_usec = 0;
timer1 . it_value . tv_sec = seconds;
setitimer ( ITIMER_REAL, &timer1, NULL );}
void timer_useconds ( long int useconds ){struct itimerval timer2;
timer2 . it_interval . tv_usec = useconds;
timer2 . it_interval . tv_sec = 0;
timer2 . it_value . tv_usec = useconds;
timer2 . it_value . tv_sec = 0;
setitimer ( ITIMER_REAL, &timer2, NULL );}
void set_all_pin_low ( void ){
bcm2835_gpio_write(PIN1, LOW);
bcm2835_gpio_write(PIN2, LOW);
bcm2835_gpio_write(PIN3, LOW);
bcm2835_gpio_write(PIN4, LOW);
bcm2835_gpio_write(PIN5, LOW);
bcm2835_gpio_write(PIN6, LOW);
bcm2835_gpio_write(PIN7, LOW);
bcm2835_gpio_write(PIN8, LOW);}

 

For mroe detail: How to Use Timer in 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 *