Analog input: ADC and sensors
Convert a voltage into a number, and a resistance that changes with temperature into a voltage.
Explanation
Digital pins recognise only high and low. Most real-world quantities lie in between: temperature, light, the position of a knob. An ADC (analog to digital converter) measures the voltage on a pin and returns a number. A 12-bit ADC returns 0 to 4095. With a 3.3 V reference, 0 means 0 V, 4095 means one step below 3.3 V, and half the reference gives half the range: 1.65 V reads about 2048. Voltage = code x reference / 4096. Not every ADC is 12 bits: the Arduino Uno's ADC is 10 bits and returns 0 to 1023 against a 5 V reference, so use 1024 and 5.0 in the formula on that board.
Many sensors do not output a voltage. They are a resistance that changes: a potentiometer, a light dependent resistor, a thermistor. To read a resistance with an ADC, put it in series with a fixed resistor across the supply and measure the voltage at the midpoint. This is a voltage divider, and the output is supply x bottom / (top + bottom). With 10 kOhm on top and 20 kOhm below, 5 V becomes 3.33 V.
The most common temperature sensor for beginners is an NTC thermistor. NTC means negative temperature coefficient: its resistance drops as it gets warmer, steeply and not in a straight line, roughly halving every 15 to 25 degrees depending on the part. The reading therefore needs a short formula or a lookup table to become a temperature. The datasheet gives the values.
A divider also solves a wiring problem: a 5 V sensor output must not be connected to a 3.3 V input that is not 5 V tolerant. A pair of resistors brings the signal down to a safe level. Note also: set a pin used for the ADC to analog mode, so that the digital input stage is disconnected and does not load or disturb the measurement.
Key points
- A 12-bit ADC gives 0 to 4095. Voltage = code x reference / 4096.
- Divider output = supply x bottom / (top + bottom).
- An NTC thermistor's resistance falls as it warms, steeply and nonlinearly.
- A divider also brings a 5 V signal down to a safe 3.3 V.
Check your understanding
3 questions on this lesson. An explanation follows each answer.
Sign in to answer the questions. Your answers count towards your level.
You can read the lesson and the project without an account.
Project
Night light with a knob
What you need
- Your board with serial output working.
- A 10 kOhm potentiometer, a light dependent resistor (LDR) and a 10 kOhm resistor.
- The LED with its resistor.
Steps
- 1Wire the potentiometer as a divider: the ends to your board's ADC reference voltage (3.3 V on most boards, 5 V on the Uno) and to ground, and the middle pin to an ADC pin. Print the raw code and the voltage you calculate from it. Turn the knob and watch both values change.
- 2Wire the LDR and the 10 kOhm resistor as a divider and print the reading. Cover the LDR with your hand and note the change.
- 3Switch the LED on when the room is dark. Use the knob to set the threshold, and print the threshold whenever it changes.
Done when
Covering the LDR switches the LED on, the knob sets how dark it must be, and the serial output shows readings in volts, not only raw codes.