Microcontrollers react to their environment through input from sensors. Thousands of different sensors are available from dozens of vendors and come in a wide variety of types, sizes, styles, functions and prices. We also have different types of digital, analog and hybrid sensors that output various kinds of values or data. Where to begin?
A good place to start is with sensor basics and that will be the topic of this edition of the Off The Shelf Hacker column.
What Is a Sensor?
A sensor is a mechanical device that converts its reaction to strain, light, temperature, magnetic fields, and so on, into an electrical signal that is read by a measurement device. Sensors may be active or passive, meaning that they may generate their own signal, in the case of active or change their signal or value in the case of passive.
An example of an active sensor might be a piezoelectric button that generates a brief electrical pulse when tapped by a finger.
An example of a passive sensor might be a photo-resistor that has value changes in relation to the amount of light falling on it’s front surface.
Microcontrollers are able to capture those pulses and measure the value changes on their input pins. Sometimes the signals are too small to be directly detected by a microcontroller input pin. For example, the output of a thermocouple, two dissimilar metals fused at the end to measure temperature, is so small you need driver board to be able to get any usable data. It’s all good because the driver board normalizes the signals from a variety of thermocouples, so their behavior is pretty standard and predictable. Software libraries make using drivers with sensors fairly painless, as well.
Digital, Analog or Hybrid?
I like to break sensors into three broad categories: digital, analog and hybrid.
First, there’s a digital sensor, like a push-button. Push-buttons are binary. They just sit there minding their own business on the front panel of your project with no connection between their two contacts. As soon as you push the button the contacts come together and make an electrical connection. You can think of the no-connection or open position as a zero (0) value, while the pushed or closed button position is a one (1). We typically connect the push-button between 3.3 volts and the microcontroller’s general purpose input/output pin. A small value resistor (say 10K Ohms) also connects between the input pin and ground. The resistor ensures that the input pin is held to a 0 logical value when the input is effectively open. As soon as the button gets pushed, the pin reads a positive voltage of 3.3 volts and registers a logical 1 value at its input.
Most general-purpose input pins on microcontrollers can read digital signals. The voltage an input pin can handle varies according to the processor. Some input pins can safely read 5.0 volts, where others are only able to handle 3.3-volt signals. Boards like the BeagleBone are rated not-to-exceed 1.8 volts. Be sure to check the spec sheet of your microcontroller for pin voltage capabilities.
Microcontrollers can also read their pins very quickly. A regular input pin on an Arduino is so fast that it can detect when the contacts on the push-button almost touch and a microscopic spark jumps the gap. This might happen a couple of times before a solid connection is actually made. It’s called switch bounce and is something that is usually filtered out in the software, running on the microcontroller. Reading a digital sensor 1,000 times per second is child’s play for most microcontrollers.
The second category of sensors is analog. These things either generate a small voltage or change their value as the device is operated. The variable voltage or value is then read by the input pin on a microcontroller.
This is important to note because not all microcontrollers have analog input capability. Extra hardware, called an analog-to-digital converter (ADC), is built into the microcontroller that interprets the variation. Note that the Raspberry Pi and its clones are strictly digital on their input pins. Other microcontrollers, like the Arduino, ESP8266 chips and BeagleBone series have from one to maybe a dozen analog input pins available.
A photoresistor is a passive analog device. Its resistance varies depending on the amount of light falling on the cell. You connect the analog device just like a push button, but instead of registering a 0 or 1, the ADC interprets the voltage, assigning a proportional value between 0 and 1023, for 10-bit analog inputs. You have to specifically assign the pin as an analog or digital input, in the software running on the microcontroller. Only select pins can handle analog input.
The last category of sensors are what I call hybrids. The above-mentioned thermocouple, while an analog device would be a hybrid. It’s output, routing through the driver board is actually digital data, that’s read by one of several data buses, built into the microcontroller. The MAX31855 driver board used with a regular thermocouple outputs it’s data over the SPI bus, which uses 3 GPIO pins. This category outputs data, as opposed to a 0 or 1 (0 or 3.3 volts) value in the case of digital or a variable value or voltage, in the case of analog.
Another type of hybrid device is the relatively new JeVois smart machine vision sensor. This thing is a small video camera connected to an ARM processor, running a lightweight version of Linux. It grabs video frames, runs them through some artificial intelligence algorithms and then sends data out over a serial line and the USB connection. That data contains the location coordinates of objects the sensor finds in its field of view and the object’s superimposed outline in the USB video feed. You might use the serial data to drive servos or steppers, in a robot. The video feed information could be sent to another computing device to do additional analysis or logging functions. Some of the hybrid sensors seem to be solutions looking for problems to solve, which is a common situation with brand new technology. It will take time for the technology to mature and become effectively integrated into everyday life.
We’ve only briefly touched on the sensor basics. We have passive and active. There’s also digital, analog and hybrids. I expect the hybrids to expand with ever faster processors and smart people writing increasingly complex software.
Nevertheless, digital and analog sensors will always be important. Push-buttons, photocells and other simple sensors are tried and true ways for microcontrollers to react to their environments.
Feature image: Small photocell.