Skip to content
Sign in
Theme

Reading the real world: ADC and sensors

Turn a voltage into a number, and turn a resistor that changes with temperature into a voltage.

Read this first

Digital pins only know high and low. Most of the world is 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 is 10 bits and returns 0 to 1023 against a 5 V reference, so use 1024 and 5.0 in the formula there.

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 middle. That is a voltage divider, and the output is supply x bottom / (top + bottom). A 10 kOhm on top and 20 kOhm below turns 5 V into 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. So the reading needs a small formula or a lookup table to become a temperature, and the datasheet gives the numbers.

The 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. Two resistors bring it down to a safe level. And one detail for later: a pin used for the ADC should be put in analog mode, so the digital input stage is disconnected and does not load or disturb the measurement.

To remember

  • 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 what you read

3 questions from the question bank on the ideas above. Each one comes with an explanation after you answer.

Sign in to answer the questions. Your answers count towards your level.

Sign in

The lesson text and the project are free to read without an account.

Build this

Night light with a knob

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

  1. Wire the potentiometer as a divider: ends to your board's ADC reference voltage (3.3 V on most boards, 5 V on the Uno) and ground, middle to an ADC pin. Print the raw code and the voltage you calculate from it. Turn the knob and watch both.
  2. Wire the LDR and the 10 kOhm resistor as a divider and print its reading. Cover the LDR with your hand and note the change.
  3. Turn 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 turns the LED on, the knob sets how dark it must be, and the serial output shows readings in volts, not only raw codes.