Interfacing a sensor with an Arduino
Arduinos have grown in popularity over the past few years, to the point where they are ubiquitous. Learn how you can use them from Linux to create a sensor system
The world of hobby electronics is one that has seen explosive growth and massive changes over the past few years. The biggest changes have come from the level of computing hardware that is available to the hobbyist. From very simple microcontrollers to full PCs on a USB stick, all sorts of options are available. One of the more popular options is the Arduino, which falls between these two extremes.
The entire Arduino project has been a proof of the power of open source. All of the hardware designs are open source, meaning anyone
can take them and manufacture Arduinos. So you can pick one up with various capabilities. There are also tons of extra modules that are available to add functionality to your project.
In this tutorial, you will get a development environment set up to work with your Arduino. Once that is done, we will cover the basics of programming it, specifically in order to get it to read the temperature. Then you will learn how easy it is to write a Python program to get your Linux box to interact with the newly programmed Arduino sensor.
Step by Step
Get your hardware
The first step is to select your hardware. There are several different versions available from several different manufacturers. These manufacturers deliver to different areas of the globe, with a full list given on the main Arduino site. The example code in this tutorial assumes that you are using one of the models that includes a built-in temperature sensor (like the Arduino Leonardo, for example).
Most Arduino boards interface over USB with the development computer. For those which don’t, there are extra modules, called shields, available to provide USB communication. When you plug it into your Linux box, it should show up as a serial device under /dev/ttyUSBX or /dev/ttyACMX. You can check which by executing dmesg and looking for the messages generated when you plug it in.
Installing the required software has gotten much easier recently. You should be able to find packages in most distributions that will handle everything for you. For example, Debian- based distros use ‘sudo apt-get install arduino’ to install the IDE, compiler and other required packages for Arduino development.
Start a new program
Now that you have your Arduino plugged in, you can start up your IDE and begin programming your first project. Programs in Arduino speak are called sketches, and they are stored in a sketchbook.
Verify your board
Since there are so many variations of the Arduino available, you need to tell the IDE which one you will be generating code for. Clicking on Tools>Board will pull up a list of all of the boards that it understands. We selected Leonardo for this example.
Verify serial port
The Arduino IDE scans all of the available serial port devices on your Linux box and presents them as options. Clicking on Tools>Serial Port will pull up this list so that you can select the correct one. Remember to check dmesg to see which one is associated to your Arduino.
Using a programmer
Normally, you upload your code using the built-in bootloader. You can use an external programmer instead. If you decide to do this, then you can select the programmer you are using by clicking Tools>Programmer.
Clicking on File>Preferences will pop up another dialog window where you can set preferences for the IDE. The ones you will likely want to set are the verbose outputs for verifying and uploading. This will be helpful when you are first starting out.
Start with an example
The Arduino IDE includes a very complete set of examples to get you started. They are broken down into groupings based on what they do. So there are examples for string processing, controlling servos etc. Selecting one of these examples will open the referenced sketch in a new window.
There are two necessary parts of every program: a setup function and a loop function. When power is applied to the Arduino, your program starts up immediately. The setup function runs once and is used for initialisation steps. Then the loop function starts up and continues for ever.
We are going to want to see what this temperature sensor is doing, so we will need to set up a serial connection. ‘Serial.begin(9600)’ opens the serial connection and sets the speed to 9600. Then you can print text out on this new serial connection.
The main loop
The main loop is the part of your program that does all of the work. In this case we will be calling a function to get the temperature and print it out. Then we sleep for 1000ms with the function ‘delay’. At the end of this delay we circle back up to the top of the loop function.
You can define your own functions in your code. In this example, we create the function GetTemp which takes no arguments and returns a double. This code is very C-like, so if you have any experience with C you should be able to read and write code for the Arduino.
Putting it all together
Once you have your program finished, you need to compile it. In the Arduino IDE, this step is called verification. When you verify your program, it gets run through the compiler, allowing you to check for any errors. The compiler output goes to the console and to the lower pane in the main window. If you set verbose output for verification in the preferences, then more complete output gets displayed in the lower pane. It is like setting verbose flags on GCC when you compile directly on the command line. You can then go and fix any errors and check on any warnings until your code compiles cleanly.
Uploading to your Arduino
Once you have a program that compiles and you are confident is correct, you can then upload it to your Arduino. Clicking on File> Upload will recompile your program and then attempt to send it over the serial port you selected earlier.
Interfacing to other languages
Now that you have a working program on your Arduino, you can interact with it over the serial port that gets created when you plug it into the USB port. There are examples all over the internet showing how to use Python, Ruby, Perl etc to talk with your Arduino.
pySerial is a package that provides a wrapper to make serial communications easier. If you are running a Debian-based distribution, you can install the package python-serial. Otherwise, you can download the tarball from SourceForge and install from source. It uses the usual ‘setup.py’ routines to handle installation.
Reading with Python
Now that pySerial is installed, you can import the serial module and use it to open the serial port to your Arduino and interact with it. In this example, we can just use ‘readline’ to read the temperatures being printed out by your Arduino program.
There are lots of other modules, called shields, available that allow you to communicate in other ways. These include shields for Ethernet, Bluetooth and Wi-Fi, among others. In one last quick example, we’ll look at creating a Twitter client that prints out tweets that are collected over an Ethernet shield.
Setup a network connection
The first step is to open a network connection and get an IP address. This is code that would be in your ‘setup’ function. It will try and use DHCP to get an address; but if that fails, you can hardcode some default values into your code as a backup.
Connecting to Twitter
Once your Arduino is on the network, you can now try and establish a connection to http://twitter.com. You need to explicitly make a connection to port 80, then you need to actually make a manual HTTP request. This is done in its own function so that you can reuse it to make repeated connections to Twitter.
Now that we have a connection, we can start reading tweets and printing them out. This is handled in the main loop of your Arduino program. Reading data off of the network connection is done one byte at a time, so you need code that will put all of the bytes together.
At this point, you have an environment on your Linux box that should allow you develop Arduino code. With all of the pre-built sensor shields available, plus the ability to build your own sensors and feed them as inputs to your Arduino, you can turn your Linux machine into a fully fledged science station. You can then write programs to monitor these sensors and handle pretty sophisticated tasks. The sky is the limit.