The latest big news in the world of Raspberry Pi Python GPIO programming is that Ben Croston has released an update for RPi.GPIO. Why is that a big deal? Because this version has interrupts. âWhatâs an interrupt?â I hear you say. Itâs a way of waiting for something to happen without checking constantly whether or not itâs happening.
Imagine that youâre waiting for a delivery â something youâre really excited about â like a Pi camera.You spend far too much time looking down the street in eager anticipation of the postmanâs arrival. You canât fully focus on what youâre supposed to be doing because you know itâs imminent. Every time you hear something in the street, you jump up and look out of the window. Woe betide any door-knocking salesman who calls when youâre expecting a delivery.
What Iâve just described in human terms is a bit like polling. Polling is continually checking for something. For example, if you want to make sure your program reacts as quickly as possible to a button press, you can check the button status about ten thousand times per second. This is great if you need a quick reaction, but it uses quite a bit of the computerâs processing power. In the same way that you canât fully focus when youâre expecting a delivery, a large part of your CPU is used up with this polling.
There has to be a better way, right?
Yes. And there is. Itâs interrupts. This is the first in a series of articles which aim to show you how to use this new interrupt facility in Python.
Interrupts are a much more efficient way of handling the âwait for something to happen and react immediately when it doesâ situation. They free up the resources you would have wasted on polling, so that you can use them for something else. Then, when the event happens, the rest of your program is âinterruptedâ and your chosen outcome occurs.
So, to carry on our human exampleâŠ
An interrupt is like having an automatic postman detector that will tell you for sure when the postman arrives, so you can get on with something else. You now know you will not miss that knock on the door and end up with one of those âwe tried to deliver your item but you were out and the collection office is closed for the next two days, so enjoy the waitâ cards.
So interrupts are good, as you can set them up to wait for events to occur without wasting system resources.
So how do you code them?
Iâm going to show a simple âwait for a button pressâ example in this blog article and follow up with other examples in subsequent articles. But before you try this, you will quite likely need to update your RPi.GPIO package. You can check what version of RPi.GPIO you have in the command line withâŠ
sudo python
import RPi.GPIO as GPIO
GPIO.VERSION
This should show you what RPi.GPIO version you have. You need 0.5.1 or higher for this example.
You can exit the python environment with CTRL+Z
Install RPi.GPIO version 0.5.1 for simple interrupts
If you need to, you can install 0.5.1 or later with
sudo apt-get update
(This will update all your Raspbian packages and may take up to an hour)
sudo apt-get dist-upgrade
or, from the command line prompt (this will only update RPi.GPIO)âŠ
wget http://raspberry-gpio-python.googlecode.com/files/python-rpi.gpio_0.5.1a-1_armhf.deb
wget http://raspberry-gpio-python.googlecode.com/files/python3-rpi.gpio_0.5.1a-1_armhf.deb
sudo dpkg -i python-rpi.gpio_0.5.1a-1_armhf.deb
sudo dpkg -i python3-rpi.gpio_0.5.1a-1_armhf.deb
For more detail: How to use interrupts with Python on the Raspberry Pi and RPi.GPIO