Tuesday, August 13, 2013

Using Sensors with Arduino

Using Sensors with the Arduino Uno

Most projects require some kind of input from the user or environment. Inputs can include push-buttons, rotary knobs, temperature sensors, light sensors or any number of other options. These sensors are typically read using an analog input.

Analog Inputs
The Arduino Uno provides 6 analog inputs that report a number between 0 and 1023 which corresponds to a voltage between 0 and 5 volts. For example, a reading of 511 would indicate that input pin was near 2.5 volts. These 10-bit analog inputs provide a resolution of 4.88 mV per bit.

It is important to keep the voltage at the analog inputs from exceeding 5 volts. You may not damage the chip when you exceed 5 volts but you know nothing about your signal, other than it is over +5 volts.

Voltage dividers are used to measure signals that can exceed the 5 volt limit. For example a vehicle electrical system can vary from 11 volts with a low battery to 14 volts when idling (with transients much higher and lower during engine starting and switching of high power accessories like AC).

For measuring battery voltage I typically connect a 1k resistor between the analog input and ground and a 3k resistor between the analog input and the battery to be monitored. This produces a four to one divider and allows up to 20 volts before reaching the 5 volt input limit.

These resistors present a fixed 4k load to the battery, drawing a nominal (I=12V/4k) load of 3mA while connected. This load can be reduced, up to a point, by using larger divider resistors. Very large divider resistors will contribute error to the measurement. There is a very cool trick you can use with a p-FET between the battery positive and the resistor divider to reduced the steady-state current to effectively zero.

Measure Battery Voltage with Zero Steady State Divider Losses, http://jeelabs.org/2013/05/18/zero-power-measurement-part-2/

Low Voltage Signals
There are many options for working with low voltage signals you can use an op-amp or instrumentation amplifier to boost your signal. You can also change the Arduino analog reference voltage from 5 volts (DEFAULT) to (INTERNAL) which is 1.1 volts. The INTERNAL reference provides a resolution of 1.074 mV for the analog inputs. This can help you get more accurate readings for lower signal levels. Just remember you need to keep your analog inputs below 1.1 volts when using INTERNAL.

analogReference(INTERNAL);   //set full scale to 1.1 volts

Please read the analog reference notes at the Arduino site. There are some important details on switching between INTERNAL and EXTERNAL references.

You can also specify an external reference and use a lower or custom reference voltage. Be sure you have a reference connected if you use EXTERNAL or you may get random readings from your analog inputs.
analogReference(EXTERNAL);  //use external analog reference, user supplied

The Arduino environment has several functions that come in handy when working with sensors (see Arduino-> Reference-> MATH). One of the most useful is the Map function that allows you to automatically generate a specific output from an input range.

map(inputSignal, 0, 1023, 0, 255);  //will map an analog input to 8-bit analog write values

The ATtiny84 can use Vcc, an external reference or an internal 1.1 volt reference for analog inputs (from 16.12.1 of the device manual). 

Low pass filters and averaging. Often a signal will have noise or jitter that needs to be removed to make the signal more useful or easy to display. A resistor and capacitor can be used as a low-pass filter. Taking multiple readings and averaging can also smooth date to reduce the impact of glitches or noisy data.

Digital interfaces
Digital interfaces are used on an increasing number of devices. I recommend looking for working example code when using digital interfaces. Google can help you find other people working with the same or similar devices. The vendor website may have sample code for another platform that you can translate into Arduino. Hit the Forums and post your question if you are still having trouble. There will probably be someone who can give you a hand up on getting your code working.

Polarity Protection with FET for Near-zero Voltage Drop
I first saw this trick on the Pololu Baby Orangutan B controller board (with a p-FET connected to Vin). 

The TI document below discuss the technique in section 2.1 and provides these examples. Note the load serves as the gate pull-down resistor to keep the gate voltage from floating. Also note Figure 2 should be captioned PMOS in Positive Path. 

The NMOS version will be cheaper and/or have a lower voltage drop.

The PMOS is more expensive but maintains the integrity of the load ground connection.

Carl Nobile pointed out this video that explains the p-fet version of the circuit.

Arudino Prototyping Tips:
Use an Arduino Uno with a DIP socket for the 328p processor. ($24.95)

Buy spare Arduino chips with boot-loader ready to go ($5.50 each)

Buy an organized assortment of resistors at Amazon.

Adafruit - full-featured multimeter for $59.95

Sparkfun Inventor's Kit ($99.95)- great start if you do not have a well stocked parts bin.

Other Resources
I have no connection with any of these sites. I just think they are cool and or handy.

Measure Battery Voltage with Zero Steady State Divider Losses

Omega Handbooks - Free!.  Great resource for information about measurement and control. Check out their website. Temperature book has great information on tuning PID controllers. 

Adafruit  - great tutorials for using sensors with Arduino, more info added frequently

SparkFun Inventors Kit Guide Book- buy the paper book or...

download the free PDF of the Inventors Kit Guide!

Pololu - great power boards, motor drivers, very cool 328p with motor driver! They also have some simple motor drives that fit amazing power into tiny boards.
Rocket Scream - Great low power library and low power (Mini Ultra 8 MHz , 1.7uA) board

Nathan Chantrell - blogger with a cool ATtiny based board, wireless sensors

Jeelab - Low power, one device on battery power for 3 YEARS!

JeeLab - Arduino compatible, 328p, small form factor, wireless, plug in boards, very cool

Modern Devices - US vendor of the JeeNode v6 kit ($22.95), many other cool parts