Resistive Sensors and Arduino: Analog Input, Digital Output
Resistive Sensors
Resistive sensors are variable resistors that change their resistance depending on the values of an external physical magnitude.
Examples:
- LDR (Light Dependent Resistor): Changes resistance in an inversely proportional way to the amount of light it receives.
- Thermistor: Its resistance changes when the temperature changes.
- Potentiometer: The resistance changes when you turn a movable terminal.
When connected as above, using another resistor (a fixed one) in series, the change in resistance translates into a change in voltage that can be used by the Arduino board. This kind of circuit is called a voltage divider.
Voltage Dividers and Analog Input
Voltage dividers give an analog input because they translate a continuous change of a physical magnitude into a continuous change of voltage.
The Arduino board contains an ADC (Analog-to-Digital Converter) that will finally convert the voltage values into numbers on a scale. Voltages would go from 0V to 5V, and numbers will start at 0 and end at 1023, with all the intermediate values corresponding in a linear proportional way. Most of the time, the actual range of variation will be shorter.
Calibrating Sensors
The correspondence between the amount of light or the temperature and the analog input as numbers can be obtained by checking the values as they change on the computer’s screen using this simple program:
void setup(){
Serial.begin(19200); // This line initializes the serial connection at 19200 bps of speed.
}
void loop(){
int lectura = analogRead(5); // This line stores into the variable 'lectura' the numbers read from analog pin 5.
Serial.println(lectura); // Prints on separate lines on the computer's screen the values read from the sensor.
delay(100); // Waits for 100 milliseconds so as not to saturate the serial communication.
}
So, if you are using a thermistor, you can compare the readings from an actual thermometer and the values on the screen and get the equivalence.
Analog Input and Digital Output
With the thermistor, we can make a thermostat and control the temperature of an oven, a heating system, or an air conditioning unit. If the thermistor is an NTC, it changes resistance inversely with the temperature.
Let’s say it is an oven, and we want it to be turned on if the temperature gets under 210°C. The oven will turn off when the temperature is higher.
Using a Relay for Digital Output
The turning on and off is accomplished by a relay when it receives either 5V or 0V. Giving 5V or 0V to a relay is a kind of digital output.
Digital output can be:
- HIGH for a 5V output
- LOW for a 0V output
The command is (if we connect the relay to pin number 7):
digitalWrite(7, HIGH);
or
digitalWrite(7, LOW);
Before, inside the setup, pin number 7 had to be declared as an output:
void setup(){
pinMode(7, OUTPUT);
}
Thermostat Control Code
If when using the code in the previous section we find out that 210°C corresponds to an analog input number of 320, the control code for the thermostat is:
void setup(){
pinMode(7, OUTPUT);
}
void loop(){
int lectura = analogRead(5);
if (lectura < 320){
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}
}
Summary
The sensor reacts to different values in a physical magnitude, changing its resistance. As resistance is not useful yet, it has to be transformed into a change in voltage. That is accomplished by the sensor circuit, the voltage divider. The variable voltage is the input in the programmable board. The analog-to-digital converter included in the board translates it into numbers that can be used in computation.
If the output is digital, we first establish a threshold by inspecting on the computer’s screen the value of the number that goes with the value of the physical magnitude that separates the two outcomes. That is called calibrating the sensor.
Once we know the number for the limit, we just compare the sensor readings with it to choose one output or the other. The continuous change of the analog input gets divided into two regions: one for ON, the other for OFF.