Hardware Reference
In-Depth Information
Analog Sensor Reader
Listing 6-1 is the reader code for both analog sensor examples. This code should be loaded onto the Arduino that
is to be used as the sensor reader; the other Arduino will be used as the sensor that provides the analog signal. The
way the code works for Listing 6-1 has not been changed from the original online example from the LadyADA web
site (located at www.ladyada.net/learn/sensors/tmp36.html ) , although the comments have been reworked. The
example is for a temperature sensor, but the concept of reading the analog pin and then correlating the math with the
sensor's output works for other analog-type sensors. Listing 6-1 reads analog pin 0 and prints the data converted to
temperature to the serial monitor.
Listing 6-1. LadyADA Temperature Sensor Reader Code with Reworked Comments
int sensorPin = 0;
void setup() {
Serial.begin(9600);
} // end void setup()
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0; // covert reading to voltage
voltage /= 1024.0; // divide the income voltage by max resolution of the ADC
Serial.print(voltage); Serial.println(" volts");
float temperatureC = (voltage - 0.5) * 100 ; // reduce by 500mV and mutiply
// 100 to get degrees Celcius
Serial.print(temperatureC); Serial.println(" degrees C");
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; // convert C to F
Serial.print(temperatureF); Serial.println(" degrees F");
delay(1000);
} // end void loop()
RC Low-Pass Filter
The first method to achieve analog output is to use an RC low-pass filter. The filter is comprised of a capacitor and a
resistor connected in serial. The capacitor is charged by a pulse-width modulation (PWM) signal from the Arduino
and drains through the resistor to the analog input on the reading Arduino. This method of converting a digital signal
to an analog signal works because a capacitor takes time to fully charge, and by controlling the time that the digital pin
is high, the charge within the capacitor will achieve a percentage of the total voltage possible from the digital pin.
A PWM at a duty cycle of 50 percent will charge to approximately 50 percent, making it half of the voltage available.
For a digital pin capable of 5V, the total charge will be ~2.5V.
In this setup, if the capacitor is too small, it will drain faster than the pulses can charge it and not provide an
adequate voltage to the analog pin; a large capacitor will increase the time the filter takes to drop from a full charge.
As long as the capacitor value is not to small, a low capacitance can be used to simulate sensors that are very
responsive and undergo rapid voltage changes. It may be advantageous on less responsive sensors to use not only
a higher capacitance but a somewhat higher resistance to slow the voltage change. The resistor keeps the capacitor
from draining back into the PWM pin: use a low resistance to avoid lowering the overall voltage. This method is an
effective way to convert a digital signal to analog when precision is not as important because the PWM only has 256
steps (0 to 255) for a 5V system that is approximately 0.019 to 0.02V per step. There is also a small amount of jitter
associated with the RC filter in this setup, which reduces the precision. This jitter is not entirely a bad thing, especially
for a sensor setup such as a control loop that responds directly to the input. Simply, a sensor that sends an analog
signal may experience some jitter, so a simulated sensor that jitters will in those cases better match the actual sensor.
 
Search WWH ::




Custom Search