Find a course

Module 3: Analog Input and PWM OutputLesson 5 of 1215 min

Analog Input and Sensors

Use analogRead to measure voltages from potentiometers, light sensors and temperature sensors, and convert raw 0 to 1023 readings into real units.

Step 1 of 12Learn

Not everything is on or off

A button is either pressed or not. But a volume knob, the brightness of a room or the temperature of a greenhouse varies smoothly. To measure those, the Uno has six analog input pins, A0 to A5, connected to an analog-to-digital converter (ADC).

The Uno's ADC has 10-bit resolution: it splits the range 0 to 5 V into 210 = 1024 steps and returns a whole number from 0 to 1023.

int raw = analogRead(A0);   // 0 ... 1023
  • 0 V reads as 0
  • 2.5 V reads as about 512
  • 5 V reads as 1023

Each step is about 5 / 1024 ≈ 4.9 millivolts. A change smaller than that is invisible to the board.

To convert a raw reading back to volts:

voltage = raw × 5.0 / 1023

So a reading of 307 means about 307 × 5 / 1023 ≈ 1.50 V. (Some references divide by 1024 instead; the difference is under 0.1% and doesn't matter for hobby projects.) Note the 5.0: in C++, raw * 5 / 1023 with whole numbers throws away the fraction and gives 1 instead of 1.5.

You don't need pinMode for analogRead. The analog pins can also be used as extra digital pins (A0 is sometimes called pin 14) if you run out.

Step 2 of 12Try the calculator

analogRead converter

See how a raw ADC reading maps to a voltage, and how a voltage maps back to a reading.

Voltage for the raw reading
2.502 V
Reading you would get for that voltage
675
Size of one step
4.88 mV

Step 3 of 12Practice · Work it out

analogRead(A0) returns 818 on a 5 V Uno. What voltage is on the pin, to two decimal places?

Step 4 of 12Practice · Diagram

analogRead values and the voltages they mean

On a 5 V Uno, each step of the 10-bit converter is about 4.9 mV.

A light sensor reads 300. Which marked voltage is it closest to?

Tap the shape that answers the question, then press Check.

analogRead values and the voltages they meanScale: 0 V, 1 V, 2.5 V, 4 V, 5 V.02004006008001,00000 V2051 V5122.5 V8184 V1,0235 V
0 V

Pin connected to GND.

1 V

1 / 5 × 1023 ≈ 205.

2.5 V

Halfway: a pot in the middle, or two equal divider resistors.

4 V

4 / 5 × 1023 ≈ 818.

5 V

Pin at the reference voltage. Anything higher can damage the chip.

Step 5 of 12Learn

The potentiometer and the voltage divider

A potentiometer ("pot") is a resistor with a sliding contact called the wiper. Connect its outer legs to 5 V and GND and the wiper to A0. Turning the knob moves the wiper along the resistive track, and its voltage slides smoothly from 0 to 5 V. That's a voltage divider: two resistances in series split the supply voltage in proportion to their values.

V_out = V_in × R2 / (R1 + R2)

where R2 is the resistance between the output and GND.

Many sensors are just a resistance that changes. A photoresistor (LDR) drops from many kilohms in darkness to a few hundred ohms in bright light. The board can't measure resistance directly, so you pair the sensor with a fixed resistor to make a divider and read the voltage in the middle.

Example: an LDR on top (to 5 V) and a 10 kΩ resistor on the bottom (to GND). In a dim room the LDR is about 30 kΩ, so V = 5 × 10 / (30 + 10) = 1.25 V, a reading near 256. In bright light it falls to 1 kΩ: V = 5 × 10 / 11 ≈ 4.5 V, a reading near 930. Brighter light means a higher reading. Swap the two parts and the relationship flips.

Choose the fixed resistor close to the sensor's middle-of-range resistance to get the widest swing.

Step 6 of 12Explore

Voltage divider explorer

The chart shows the output voltage as the top resistor (for example, an LDR) changes from 0 to 50 kΩ. Move the sliders to change the bottom resistor and the supply.

012345 01020304050
Output with top = 10 kΩ
2.5 V
Reading with top = 10 kΩ
512

Step 7 of 12Practice · Choose one

A light sensor divider has the LDR between 5 V and A0, and a 10 kΩ resistor between A0 and GND. The room gets darker. What happens to analogRead(A0)?

Step 8 of 12Learn

From numbers to real units

Raw readings are rarely what you want to show. Two common conversions:

Temperature from a TMP36. This three-legged analog sensor outputs 0.5 V at 0 °C and rises 10 mV per degree. So:

temperature (°C) = (voltage − 0.5) × 100

A reading of 153 gives 153 × 5 / 1023 ≈ 0.748 V, so (0.748 − 0.5) × 100 ≈ 24.8 °C.

Scaling with map(). Arduino's map(value, fromLow, fromHigh, toLow, toHigh) rescales a number linearly:

int raw = analogRead(A0);            // 0..1023
int percent = map(raw, 0, 1023, 0, 100);

map() uses whole-number math, so it drops fractions. It also doesn't clamp: if the input goes beyond the range, the output does too. Wrap it with constrain(x, 0, 100) when you need hard limits.

Readings jitter by a count or two because of electrical noise. If your display flickers between 24.8 and 25.3 °C, average several readings (for example, add up 10 readings and divide by 10) before showing the result.

Step 9 of 12Practice · Work it out

A TMP36 sensor produces 0.72 V. What temperature in °C does that represent?

Step 10 of 12Note

Step 11 of 12Tap to reveal

Sensors you can read with analogRead

  1. Potentiometer

    A knob or slider. Already a voltage divider, so it connects straight to an analog pin. Used for volume, speed and setpoint controls.

  2. Photoresistor (LDR)

    Resistance falls as light increases. Needs a fixed resistor to form a divider. Good for "is it dark?" decisions, not precise light measurement.

  3. Thermistor and TMP36

    A thermistor changes resistance with temperature and needs a divider plus a conversion formula; a TMP36 outputs a voltage that is directly proportional to temperature.

  4. Soil moisture probe

    Wet soil conducts better, which changes the reading. Capacitive probes last much longer than bare metal ones, which corrode within weeks.

  5. Force and flex sensors

    Their resistance drops as you press or bend them. Ideal for detecting a squeeze, a step or a finger bend in a glove controller.

  6. When analog isn't enough

    Many modern sensors (humidity, distance, motion, accurate temperature) send digital data over interfaces such as I2C instead. You read them with a library rather than analogRead.

Step 12 of 12Practice · Fill in the blanks

Fill in the blanks

The Uno's analog-to-digital converter has a resolution of bits, so analogRead returns whole numbers from 0 to .