Home · Academy · Robotics & Coding · Arduino · Analog Reading

Analog Reading

Learn to read a 0–1023 value from a potentiometer or sensor with analogRead.

LESSON COMPASS

What will you use this page for?

Core idea

So that a pin can read not just on/off but every value in between, we use the analogRead command to get a number from 0 to 1023.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Confusing analogRead with digitalRead analogRead(A0) gives a number from 0 to 1023. digitalRead(A0) gives only HIGH / LOW . If you expect a graded value from a sensor, you must use analogRead . Giving analogWrite a value above 255 analogWrite accepts 0–255. If you pass the 0–1023 value from analogRead directly, the…

Next connection

Serial Monitor: the window that lets the Arduino show what it is doing by sending us numbers and messages, and lets us watch sensor values in real time.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteDigital Input and Output
ContentStandard lesson · 1,529 words
Last updated

One-sentence summary

So that a pin can read not just on/off but every value in between, we use the analogRead command to get a number from 0 to 1023.

Why does it matter?

In the previous lesson we learned digital input: a button is either pressed or it is not, and digitalRead gives us only HIGH or LOW. But most things in the world are not that sharp. Temperature rises slowly, a room can be half dark, and we can turn a knob a little or all the way.

To measure these "in-between" values we use analog reading. Many parts, such as a potentiometer (adjustment knob), a light sensor, a moisture sensor or a sound sensor, send a voltage to the Arduino. The Arduino turns that voltage into a number, and we make decisions from it: how bright to make a lamp, how fast to spin a motor, or whether it has gone dark.

This lesson is the foundation of working with sensor modules. If you cannot read a sensor, you cannot make anything respond to it.

Analog signals and the ADC

The difference between digital and analog

A digital signal has only two values: on (HIGH, about 5 volts) and off (LOW, 0 volts). Think of a light switch: it is either on or off.

An analog signal can take any value between 0 and 5 volts: 1.2 volts, 3.7 volts, 4.95 volts, and so on. Think of a dimmer knob: you adjust the brightness smoothly.

ADC: the converter that turns voltage into a number

Inside the Arduino's analog pins, named A0A5, there is a circuit called the ADC. It stands for Analog-to-Digital Converter. Its job is to turn the voltage on a pin into a whole number the Arduino can understand.

The Arduino Uno's ADC is 10-bit. That means:

So analogRead always gives us a value between 0 and 1023. Because it divides 5 volts into 1024 steps, each step is about 4.9 millivolts. If you want a rough estimate of the voltage:

voltage = analogRead value × 5 / 1023

Reading a potentiometer

A potentiometer is an adjustment knob whose resistance changes as you turn it. It has three legs: the two outer legs connect to power (5V) and ground (GND), and the middle leg goes to pin A0. As you turn the knob, the voltage on the middle leg changes between 0V and 5V.

Let the first sketch simply read the value and print it to the Serial Monitor:

const int potPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(potPin);  // 0 - 1023
  Serial.println(value);
  delay(200);
}

When you turn the knob to one end you will see numbers close to 0, and near 1023 at the other end. (We will study the Serial Monitor in detail in the next lesson; for now, seeing the values change is enough.)

Using the value you read: LED brightness

Let us put the number to work. We will control an LED's brightness with the potentiometer. But there is a catch: analogRead gives 0–1023, while analogWrite accepts only the 0–255 range. The map command matches these two ranges:

const int potPin = A0;
const int ledPin = 9;   // PWM (~) pin

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int value = analogRead(potPin);              // 0 - 1023
  int brightness = map(value, 0, 1023, 0, 255);
  analogWrite(ledPin, brightness);
  delay(10);
}

Wiring: the LED's long leg (+) goes through a 220 ohm resistor to pin 9, and the short leg goes to GND. As you turn the knob, the LED dims and brightens. A dimmable living-room lamp or the volume knob on a music player works with exactly this idea.

Reading a light sensor (LDR)

An LDR (light-dependent resistor) is a part whose resistance drops as more light falls on it. It does not produce a voltage on its own; we build a voltage divider with it and a fixed resistor.

Wiring:

Now let us make a night light that turns on when it gets dark:

const int ldrPin = A0;
const int ledPin = 8;
const int threshold = 400;   // light threshold

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int light = analogRead(ldrPin);
  Serial.println(light);
  if (light < threshold) {
    digitalWrite(ledPin, HIGH);  // dark: turn LED on
  } else {
    digitalWrite(ledPin, LOW);   // bright: turn LED off
  }
  delay(200);
}

You may need to change the threshold to match the real light in your room: first read the bright and dark values in the Serial Monitor, then pick a number near the middle of the two. Street lamps turning on by themselves in the evening use the same idea.

Mini activity

If you have a potentiometer, try this task: use it to set an LED to three levels. When the value is low, keep the LED off; in the middle, light it half bright; when high, light it fully.

const int potPin = A0;
const int ledPin = 9;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(potPin);
  if (value < 341) {
    analogWrite(ledPin, 0);      // off
  } else if (value < 682) {
    analogWrite(ledPin, 128);    // half bright
  } else {
    analogWrite(ledPin, 255);    // full bright
  }
  delay(50);
}

Then try this: instead of delay, can you use millis() to print the value every 200 milliseconds while updating the LED without pausing? This is a good exercise for seeing the difference between delay, which makes the program wait, and millis(), which measures time without waiting.

Common mistakes

Confusing analogRead with digitalRead

analogRead(A0) gives a number from 0 to 1023. digitalRead(A0) gives only HIGH/LOW. If you expect a graded value from a sensor, you must use analogRead.

Giving analogWrite a value above 255

analogWrite accepts 0–255. If you pass the 0–1023 value from analogRead directly, the result will be wrong. Always put map in between.

Forgetting the voltage divider

If you wire the LDR by itself between A0 and 5V, you will read a fixed, meaningless value. The other side of the sensor must always have a resistor and a GND connection.

Mixing up when pinMode is needed

You do not need pinMode for analog reading; but if the same sketch uses an LED as an output, remember to write pinMode(ledPin, OUTPUT) for it.

Safety note

Lesson summary

Check questions

  1. What are the smallest and largest values analogRead returns?
  2. What does ADC stand for, and what is its job?
  3. Which pin does the middle leg of a potentiometer connect to, and what does it carry?
  4. Why can we not pass the value from analogRead directly to analogWrite?
  5. When making an LDR night light, what does the line if (light < threshold) do?

Answers

  1. The smallest is 0 (about 0 volts) and the largest is 1023 (about 5 volts).
  2. Analog-to-Digital Converter; it turns the analog voltage on a pin into a whole number the Arduino can understand.
  3. It connects to an analog pin (for example A0) and carries a voltage that changes with the knob's position.
  4. Because analogRead works in the 0–1023 range while analogWrite works in 0–255; we must match the two ranges with map.
  5. It is the condition that turns the LED on when the light value drops below the threshold (when the surroundings get dark); otherwise the LED stays off.

Source and verification note

For “Analog Reading”, verification focuses on whether the relationship between Analog signals and the ADC and ADC: the converter that turns voltage into a number remains consistent across examples. Pin, voltage and current limits can differ between Arduino-compatible boards. Compiling code does not guarantee a safe circuit; loads such as motors and servos require a suitable driver and external power where appropriate.

Next lesson

Serial Monitor: the window that lets the Arduino show what it is doing by sending us numbers and messages, and lets us watch sensor values in real time.

Start QuizBack to Arduino
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.