This tutorial has been created to explain the fundamentals of using analog sensors with a microcontroller. In this tutorial we use a thermistor (temperature dependent resistor) as example for sensing temperature . Understanding how to read this sensor and how to connect it gives you the ability to adapt to other analog resistive sensors easily.

Note: Before you start make sure you also have the Arduino IDE software installed and tested on your laptop. To do so, follow the instructions on the links in the Read More section of this page.

## Some background information

Many analog sensors change their electrical resistance under influence of changing physical parameters (e.g. temperature, illumination level, strain). Common microcontrollers like Arduino are not able to sense a changing resistance. They are however able to measure voltage differences. This means that the resistance value of a sensor must be converted to a voltage difference. This can be achieved with the help of a voltage divider circuit in which one of the resistors is the sensor.

Most microcontrollers are equipped with one or more Analog to Digital Converters (ADC). The Arduino Uno for example has 6 ADC channels. In the UNO each individual ADC has a resolution of 10 bits which means that it can measure $2^{10} =1024$ different possible values of the analog input signal. This means that for example an Arduino Uno can measure 5 V in 1024 steps, or about 4.88 mV/step.

This also implies that in order to measure changes in input, the change in signal should at least be a few times this amount so at least 20-30 mV,  and preferably in the range of a few V if you want to have stable and accurate measurements.

Using proper code in the microcontroller you are now able to build smart systems by sensing the outside world (light, temperature etc) to control for example an actuator .

## Analog sensors

We will use a thermistor or temperature dependent resistor as example for a sensor that changes its electrical resistance depending on changing environmental circumstances (i.e. temperature).

Temperature Dependent Resistor

A thermistor or temperature dependent resistor changes its electrical resistance as function of the temperature. From the datasheet of this thermistor you can see the relation between the temperature and the electrical resistance. An increasing temperature will result in an decreasing resistance. (NTC = Negative Temperature Coefficient).

At a (room) temperature of 25° C the resistance is 10 kΩ.

Example thermistor characteristic from datasheet

## Voltage divider

To convert the sensor output (changing resistance value) to a usable input for your microcontroller, you can use a voltage divider. The output of this circuit is a voltage based on the resistance of the components which in turn is related to the physical value measured by the sensor. This voltage is the input for the ADC of the microcontroller (pins A0 – A5 on Arduino Uno for example).

Combined with knowledge of the datasheet of the sensor it should be possible to construct a setup for a system. A good starting point is to look for the sensor resistance at midrange of the required physical scope. Assuming that that the goal is to build a temperature controller with a range between 20° and 40° C. From the datasheet you notice that @ 30° C the resistance is 8 kΩ. Use this value for $R_2$ in the standard voltage divider. The practical value of this resistor is 8.2 kΩ (E12 range)

Voltage divider schematic

Using 8.2 kΩ for R$_2$ and the standard thermistor as shown in datasheet @ 30° C, the sensor output voltage can be calculated.

$V_{sensor} = V_{cc}\frac{R_2}{R_2 + R_{sensor}}$

$V_{sensor} = 5V \frac{8k2 }{8k2 + 8k} = 2.5 V$

2.5 Vdc at the ADC of an Arduino Uno with 1024 steps resolution and 5V internal reference will result in an analogReading of:

$analogReading = 2.5V\frac{1024 }{5V} = 512$

For 20° C $R_{sensor} = 12 kΩ$ (see datasheet)

$V_{sensor} = 2.0 V$, $analogReading = 410$

For 40° C $R_{sensor} = 6 kΩ$ (see datasheet)

$V_{sensor} = 2.857 V$, $analogReading = 585$

So with this setup you can expect to obtain input values in your Arduino between 410 and 585 corresponding to temperatures between 20 and 40° C.

Important note: This sensor is an example of a non linear sensor. Its electrical resistance does not relate in a linear way to the temperature. Also the conversion from resistance to voltage by the voltage divider is non-linear. This is illustrated by the calculation results. The reading for 30° C, value 512, is not the average of the reading s for 20° C and 40° C which is 497.5. This has to be taken in account when calculating the temperature based on the analogReading. Calibration of this system is important.

## Sample code

In this tutorial we use the built-in serial plotter of the Arduino IDE to visualize the sensor data. It is up to your own creative ideas to make the system really smart. Pinch the thermistor between your fingers to heat it, then let it cool down again.

Arduino file

The screenshot below  illustrates the use of the serialplotter. It visualizes the analogReading and the output. Comparing the reading with the threshold value in the software gives you the possibility to decide which action has to follow. In this example we decided that as soon as the reading is below the threshold the output becomes HIGH. You can use this output for controlling actuators of your choice.

Example output of the Serial Plotter.