Analog Awesomeness
It's easy to run out of analog pins on your Arduino far too soon when you are working on a project. Add a multiplexer or two, though, and you're back in business.
It's easy to run out of analog pins on your Arduino far too soon when you are working on a project. Add a multiplexer or two, though, and you're back in business.
Analog Multiplexer
In a previous article on how to hack the Robosapien [1], I showed how a digital multiplexer, such as the MCP23017, could help you out. The limitation was that, although you could control several DC motors from a couple of pins (no mean feat) and you could have them change direction on the fly by adding H-bridges, you could not control the speed or use the multiplexer to read in analog input, such as from temperature sensors, light sensors, and so on.
Plenty of analog multiplexing shields are available out there, but it is cheaper and more fun to use the chips themselves. You get to learn the ins and outs (literally) of the Integrated Chip (IC). This knowledge can come in handy for projects in which a shield simply won't do because of cost or space constraints.
In this article, you'll get to know the MC14051B [2] (Figure 1), which is a very common and inexpensive analog multiplexer.
As a soft way in, you're going to learn how to read data from different basic analog sensors. Then, when you have that down, you'll move onto piping several analog inputs through a multiplexer. (If you're already familiar with reading analog input from sensors, you can skip to the "Through the Multiplexer" section below.)
For this experiment, you'll be using a potentiometer, a temperature sensor (I used an LM35DZ), and a light sensor (Figure 2). These are very common, simple sensors that you can find in any half-decent Arduino kit, and they don't require any other special hardware, except for a pull-down resistor in the case of the light sensor, or software – no messing around with libraries!
All of these devices work on the same principle: You run a current through them and they modify their resistance depending on external conditions, modifying the outgoing flow of electricity. Figure 3 shows how to wire them up. Neither the potentiometer nor the light sensor care about polarity, so it doesn't matter which side gets the 5V and which is connected to ground (GND).
Do notice, however, that the light sensor does need a pull-down resistor on the same leg that connects to the Arduino analog pin so that it's not left floating. (See the "Resistor Pull" box.) Although you will need resistors with different resistance depending on whether you need to measure brightly illuminated or darker areas (a lower resistance is called for brighter environments and higher resistance for darker), a 1Kohm resistor (gold, brown, black, and red bands) works okay-ish in a room with natural light. A very … er … illuminating article at Adafruit [3] on how to calculate which resistor to use and the exact amount of lux you're measuring is well worth reading.
Resistor Pull
Pull-up and pull-down resistors hold a circuit at a defined logic level when no devices are attached. A pull-up resistor lies between the signal source (external device) and power source and "pulls" the circuit toward the input voltage level (high logic level). A pull-down resistor is connected to ground and "pulls" the voltage toward 0V (low logic level). When a device is attached, it takes over control of the logic level.
The temperature sensor does care about polarity, so hold LM35DZ with the flat part of the head facing you and the prongs pointing down. Connect 5V to its left prong, GND to the right prong, and the analog pin on your Arduino to the central prong.
A sketch to read from any of these three sensors and write the result to the serial monitor is shown in Listing 1. If you're using the light sensor for example, upload this to your Arduino, open the serial monitor, and flash a bright light onto the sensor. You should see the values jump up to beyond 400. If you cover the sensor with your hand, the values should drop to single digits.
Listing 1
Reading Analog Input
01 int analogPin = A0; // Change this to whichever analog pin \ your sensor is connected to 02 03 void setup() 04 { 05 pinMode(analogPin, INPUT); 06 Serial.begin(9600); 07 } 08 09 void loop() 10 { 11 int val = analogRead(analogPin); 12 Serial.print("Value = "); 13 Serial.println(val); 14 15 delay(100); 16 }
You can do the same for the potentiometer. With the temperature sensor, however, you need to modify the values to make the sensor's output meaningful. To convert the output to Celsius and Fahrenheit, add lines 4-6 shown in Listing 2 after line 11 in Listing 1 and print cel
or farh
, not val
, to the serial monitor.
Listing 2
Calculating Celsius and Fahrenheit
01 . 02 . 03 . 04 float mv = (val/1024.0)*5000; 05 float cel = mv/10; 06 float farh = (cel*9)/5 + 32; 07 . 08 . 09 .
Pages: 6
Price $15.99
(incl. VAT)