Technology

Off-The-Shelf Hacker: Let’s Talk Sensors

18 Jun 2016 3:43am, by

We’ve talked about the Arduino, the Raspberry Pi, the Pine 64 and several other devices. I’ve explained the differences between the various “microcontrollers” and which one to use for which project.

What about sensors?

Today, we’ll discuss “sensors” you can use with microcontrollers and nanocomputer systems in physical computing projects. I hear a lot of discussion about whether people should use an Arduino or Raspberry Pi, but sensors also have a huge influence on your decision.

Sensors 101

I categorize sensors into three types: analog, digital and other.

Analog

Analog sensors detect non-discrete physical behaviors. A common example is light falling on a photocell. The photo cell’s resistance changes with the amount of light striking its surface.

You need an analog-to-digital converter (ADC) to measure voltage, as a result of the varying resistance of the photocell and change it a series of stepped values, that a microcontroller can understand. This is all done through a voltage divider circuit.

One of my favorite Arduino modules, the Pro-Mini has built-in ADC circuits. The analog input pin measures from zero up to the power supply voltage, with ten-bit (2^10) resolution (1024 values). So, for example if you measure from zero to five volts, the value returned from an analog read function with zero volts, on the pin, would be zero. Likewise, when five volts is applied to the pin, the value would be 1024. If you had 2.5 volts, it would be 512. You get the picture.

The Pro-Mini has eight of these analog pins with analog-to-digital converters. Need more, the Arduino Mega has 16. The Raspberry Pi doesn’t have any analog pins.

Now, why is that?

Remember that the Arduino uses dedicated firmware that you upload for each program. It has no so-called “operating system.” That means it concentrates on tracking analog or digital sensors very, very quickly.

The Raspberry Pi runs Linux, so it is capable of multi-user and multitasking activities. That means it shares some of it’s computing cycles to possibly run a desktop, check for networking packets and run user applications, while watching its general purpose input/output pins. With all those other jobs going on, tracking analog signals is a challenge. The Pi only supports digital inputs.

Bottom line: Use an Arduino or similar microcontroller for analog-centric projects.

An example of an analog sensor might be a variable potentiometer. It’s a three terminal device where the middle terminal attaches to a little arm that wipes against a carbon element, as the shaft rotates. When the arm is close to one terminal, the resistance is low and increases as the shaft is turned.

Another analog device might be a pressure sensor, thermistor or even a strain gauge. The resistance in these devices all vary when some kind of external force (pressure, temperature, light, twisting, etc.) is applied.

Digital

OK, so the analog sensors change resistance, which in turn can be measured as a voltage by the analog Arduino pin, with it’s built-in ADC. That value is then used, as a ten-bit number, in an Arduino program.

Digital sensors are a lot simpler because they measure discrete physical behaviors. The are either on or off. A zero or one value is easy to detect by a microcontroller, and there is no need for an ADC.

Digital sensors might include an on/off switch, a reed switch, a push button, a Hall-effect sensor and so on. Digital inputs are generally so fast that something called “switch bounce” can be a problem. This occurs when some electrons jump across the gap between the contacts as they get close together but don’t quite touching each other, causing multiple digital “1’s” to be generated, during a button press. And, it can happen in the space of millisecond time frames. Building in a little time delay between readings or some filtering is usually a good idea to “debounce” the switch, so you only get one reading per press.

A Hall-effect sensor, registers closed when a magnet passes by and open when there is no magnet nearby. You might use one of these to measure the RPM of a spinning shaft.

The Raspberry Pi has a bunch of digital inputs, and you can measure quite a lot of things with a simple zero or one device. For example, instead of using an ADC with a potentiometer, to measure how far a shaft is turned, you could count pulses from a starting point with a photocell, an LED and slits cut into a little template attached to the shaft. It’s called an encoder, and it’s purely digital.

Other

I like to abstract the last group of sensors up one level and say they are “data supplying” devices, although they are digital in operation.

Instead of measuring a voltage (analog) or counting pulses (digital) these sensors supply data as input to the microcontroller.

A webcam falls under this category. It grabs a view with its array of light sensing material and converts it into a data stream that is sent through the USB port into the microcontroller or nanocomputer. It uses various protocols to communicate between the device (the camera) and the microcontroller. As that data flows into the microcontroller, it’s processed, and decisions can be made as to what actions to take, be it turning on a light, tripping a relay or sending data to another device, perhaps even over a network. This type of “data” sensor works well with sophisticated nanocomputers like the Raspberry Pi. You wouldn’t use a webcam sensor with an Arduino, simply because it doesn’t have enough processing power.

The Pro Mini can, however, use data from a Pixy image processing sensor. The Pixy detects colored objects, within its field of view and outputs a data stream that tells the X-Y coordinates and relative size of the object. That stream of data goes over some data link (USB, I2C, serial, etc.) to the microcontroller. An Arduino Pro Mini could interpret those X-Y coordinates to activate motors to steer a little robot toward a brightly colored ball, for example. The Raspberry Pi could easily do the same thing.

Another data-supplying sensor is the popular DS18B20 digital thermometer. To get a temperature from the device, you send a little pulse from one of the digital pins on your microcontroller and after a short time, the DS18B20 will return a unique serial number and the temperature reading, in digital form, that the microcontroller can interpret. The round trip reading takes about 750 milliseconds.

Other data-supplying devices include GPS modules, the BMP180 Barometric Pressure/Temperature/Altitude Sensor or even another microcontroller sending data over a data line. These all work with an Arduino or a Raspberry Pi.

Wrap Up

We’ve discussed three main categories of sensors. If you look at the major parts suppliers, you’ll find a nearly infinite number of devices that can measure almost anything you can imagine.

You may have a mix of analog, digital or data-supplying sensors in your projects. It just depends on what you are trying to measure, in the physical world.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.