RaspberryPi inputs: to poll or to interrupt

One of the best features of the RaspberryPi is the GPIO (General Purpose Inputs & Outputs).

Reading the input levels on the GPIO is pretty easy, especially using Gordon's wiringPi library.

But you need to think carefully about how best to do this in the software for your chosen project.

The two basic options are either to poll an input, or to use an interrupt.

polling an input

RaspberryPi inputs can be checked periodically to determine if the input level is high or low.

In a functional programming language like Python, we would probably create a program loop like this:-

while True:
if GPIO.input(1)==True:
…input is HIGH, so do some stuff
else:
…input is LOW, so do something else
time.sleep(0.2)

This “while true” loop runs for as long as the program runs. The sleep time (200ms in this case) just ensures we don't paralyse the cpu by demanding its attention for 100% of the time.

In an event driven language like Gambas, we can use a timer (a periodic event) as the trigger to run an input checking routine;

inputTimer.delay=200 ‘a 200ms interval
inputTimer.start()

Public Sub inputTimer_Timer()

if digitalRead(PIN_1)=0 Then
…input is HIGH, so do some stuff
Else
…input is LOW, so do something else
EndIf
End

This timer routine only runs when the 200ms interval has elapsed. We don't need to create any program loops because Gambas takes care of that for us.

In both examples, our program will check the current condition of an input, do something appropriate, and then free-up the cpu to do other system tasks. So the interval between input checking is approximately 200ms plus the time it takes to execute our “some stuff” or “something else” code.

RaspberryPi inputs to poll or to interrupt

attention to detail

We need to look carefully at our requirements to determine whether this approach is suitable for our application.

For example, in my bird box project I'm detecting birds as they enter or leave the bird box. Small garden birds can get through the 28mm hole really quickly. But considering the length of the bird, I reckon the sensor will typically be blocked for at least 500ms, and as my counter only needs to provide a rough approximation, I've set my Gambas timer interval to 500ms.

However, your requirements may be more demanding. What if we wanted a more accurate count of birds entering and leaving the nest box?

You may find it useful to draw a simple sketch, something like this:

The second line shows that the input from the sensor may be high just long enough to be read by our software once, or the input may remain high long enough to be read high twice. The solution may be to count the first high input reading, but not count any further high readings until we have seen a low reading.

Now what happens if a bumble bee enters the nest and is briefly detected
The short pulse at the input may or may not be read as a high, it just a matter of timing.

You can reduce the influence of small critters upsetting your count by reducing the sample interval (i.e. increasing the frequency)

However you must now modify your code so that a minimum number of consecutive reads, where the input is high, are required to count as a bird (e.g. 10 consecutive high readings = 500ms = bird). While you still need to read a low input level (or maybe 2 or more) before counting the next high sequence as a possible bird.

Another consequence of increasing the input read frequency may be higher cpu utilisation, possibly resulting in a less responsive system, more heat and higher power consumption.

 

For more detail: RaspberryPi inputs: to poll or to interrupt


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