IoT and the Physical World
Ted Malone has been working on this great Raspberry Pi 3 IoT journey...
In the first two posts of this series (See Post One and Post Two) I detailed the process of configuring a Raspberry Pi 3 to enable development and testing of Internet of Things (IoT) projects. The first post covered the basics of the Raspberry Pi, and the second detailed the basics of the GPIO interface and demonstrated how to turn an LED on and off with code. In this post, we will extend the project that we created earlier and add interaction with the physical world through the use of a mechanical button press.
When interacting with sensors and devices, one of the more important things to understand is how interrupts work, and how you code interrupt service routines.
Understanding Hardware Interrupts
This is a topic that you could spend many hours on and not understand all of the nuances, yet it’s very important to understand the basics of how interrupts work if you’re going to develop IoT applications that utilize sensors or other electronic components that work with the Raspberry Pi.
Step One – Construct the Simple Circuit
In the previous post, we created a simple circuit that included an LED and a resistor that connected to the GPIO interface and allowed us to control the LED in code. The circuit diagram looks like:
and the resulting breadboard configuration looks like:
Step Two – Develop the Code
For this exercise, we will create a new solution in Visual Studio that uses interrupts to detect when the button is pressed. We will then toggle the LED every time the button is pressed. In Visual Studio, create a new C# Windows Console application named ISR.
Deploying and Executing the ISR Application
Use a file transfer program (I use FileZilla) to copy the ISR.exe and WiringPi.dll files to a folder on the Raspberry Pi. (I used ~/DevOps/ISR)
The LED will light, and when you press the button it should toggle state. You will likely very quickly see the result of our not de-bouncing the switch, as some presses will likely result in multiple toggles of the LED. There are many patterns for de-bouncing in software, but the most reliable solutions are hardware-based. You will also note that when you press a key, the application does not exit until you press the button as well. This is a byproduct of the ISR Routine needing to execute in order to capture the keypress.
Congrats! You’ve now developed an application that bridges the gap between code and the physical world. This is an extremely important concept for developing any IoT Application.
In the next post in this series, we will discuss the concept of digital signaling and will develop an application that interacts with an RGB LED that requires Pulse Width Modulation in order to work properly.