Resistive Sensors and Voltage Dividers

I just wanted to say a word or two about sensors as they pertain to the Arduino. Hopefully this will make some of the circuitry you have seen in the workshop a little bit more transparent.

As we have seen in class, many analog sensors are resistive in the sense that they vary their electrical resistance based on the magnitude of the stimulus that they receive from the environment. It is the job of the electrical circuitry to turn this change in resistance into a change in voltage that we can then measure using the A/D converters on the Arduino board.

Let's first look at a circuit that does NOT work,

      +5V
       |
       +-----------O A0 (A/D input Arduino)
       |
       R (Sensor)
       |
       =  (GND)
In this curcuit it does not matter what the resistance is of the sensor R, the voltage on one terminal of the sensor will always be 5V and the voltage on the other terminal will always be zero. This means the A/D converter will always see 5V regardless of what the stimulus on the sensor R is. Let's change this circuite slightly,
      +5V
       |
       R1 (Sensor)
       |
       +-----------O A0 (A/D input Arduino)
       |
       R2 (Resistor)
       |
       =  (GND)
This circuit is called a **voltage divider** and the voltage at A0 can be computed as
             A0 = [R2/(R1+R2)]5V
If we make R1 and R2 exactly the same value, say 1KOhm, then we can compute the voltage at A0 as
             A0 = [1K/(1K+1K)]5V = [1K/2K]5V = 1/2 5V = 2.5V
That is, if the resistance values R1=R2 then the voltage divider splits the original voltage in half. Now, if we chose the resistance values as R1=1K and R2=2K, then
            A0 = [2K/(1K+2K)]5V = 2/3 5V
It is straight forward to work out additional examples (e.g. R1=4K and R2=1K).

Now back to our sensors. Let's take our flex sensor. It has a resistance value of 10KOhm when it is not bent and has a resistance value of about 20KOhm when it is bent at 90 degrees. We now use a voltage divider in order to convert the stimulus on the sensor into different voltages. Let us design it in such a way that when the flex sensor is not bent then we receive a voltage of 2.5V. This means when there is no stimulus on the sensor then we split the 5V original voltage in half. We know that our flex sensor R1 has a resistance of 10K so in order to split the voltage in half we have to pick a resistor R2 also with the value 10K in order to split the voltage evenly,

      +5V
       |
       R1=10K (Sensor)
       |
       +-----------O A0 (A/D input Arduino)
       |
       R2=10K (Resistor)
       |
       =  (GND)
Now, what kind of voltage can we expect from the voltage divider when the flex sensor is bent to 90 degrees. Recall that when the sensor is bent it has a resistance of 20K. This gives us the following values for the voltage divider above: R1=20K and R2=10K. Now, plugging this into the voltage divider formula for A0:
             A0  = [R2/(R1+R2)]5V = [10K/(20K+10K)]5V = 10/30 5V = 1/3 5V= 1.7V
This means, as we bend the flex sensor back and forth the voltage on A0 swings between 1.7V and 2.5V.

Now, what kind of values can we expect in digital form on the Arduino once the voltage has passed through the A/D converter? The A/D converter divides the HIGH=5V voltage on A0 into 1023 discrete little steps as follows:

         0V ----> 0
       2.5V ----> 511
         5V ----> 1023
This means when the sensor is not bent the A/D converter delivers a value of 511 and when the sensor is bent then it delivers a value of
           (1.7V/5V) 1023 = (0.34) 1023 = 347
Again, this means as we flex the sensor the values the Arduino chip sees range between 511 (not bent) to 347 (90 degrees).

In order to get a better dynamic range that is easier to work with we can use the map function and map the A/D values into a range between 0 (not bent) and 100 (90 degrees)

         val = map(x,511,347,0,100)
Now, try to do a similar calculation with a photoresistor (spec: 12Kohm LIGHT, 1000Kohm DARK).

Happy Hardware Hacking,
Lutz