Reading the World: Master Arduino Analog Inputs & Potentiometers

Reading the World: Master Arduino Analog Inputs & Potentiometers


📑Table of Contents
What You'll Need 6 items
Arduino Uno R3
Buy
Breadboard
Buy
Potentiometer (10kΩ)
Buy
5mm LED
Buy
220Ω Resistor
Buy
Jumper Wires
Buy

Affiliate links help support this site at no extra cost to you.

Welcome to Day 14. So far, our Arduino has lived in a Digital world.

  • digitalWrite(HIGH): 5 Volts.
  • digitalWrite(LOW): 0 Volts.

It was Black or White. Yes or No. But the real world isn’t digital. The real world is Analog. The temperature isn’t just “Hot” or “Cold”—it’s 23.4°C. The volume of music isn’t just “Mute” or “Max”—it’s a smooth slider. The light in a room varies from pitch black to blinding sun.

If we want our robots to interact with reality, they need to see these shades of gray. Today, we unlock the ADC (Analog to Digital Converter). We are going to teach the Arduino to read voltage.

Analog Digital Conversion Intro

The Hardware: The Potentiometer

We met this component back in Day 6 (The Voltage Divider). A Potentiometer (or “Pot”) is a variable resistor. It has a knob. As you turn the knob, it changes resistance.

Inside the Pot

It has 3 legs.

  1. Leg 1: Connected to Ground (0V).
  2. Leg 3: Connected to VCC (5V).
  3. Leg 2 (Middle): The Wiper.

Inside, there is a resistive track. The wiper slides along it.

  • If the wiper is near Leg 1, the output is nearly 0V.
  • If the wiper is near Leg 3, the output is nearly 5V.
  • If the wiper is in the middle, the output is 2.5V.

It is a physical mechanical machine that creates an analog voltage.

Potentiometer Internal Cutaway

Concept 1: The ADC (Analog to Digital Converter)

The Arduino’s processor (ATMega328P) is digital. It only understands 1s and 0s. It cannot understand “2.5 Volts”. So, it has a special translator module called the ADC.

The 10-Bit Resolution

The ADC measures the voltage on a pin (A0 to A5) and converts it into a number. But what number? The standard Arduino Uno (R3) has a 10-bit ADC. This means it splits 5 Volts into 2102^{10} steps. 210=10242^{10} = 1024 steps.

  • 0 Volts -> 0
  • 5 Volts -> 1023
  • 2.5 Volts -> 511 (Roughly half).

Every step represents about 4.9 millivolts (5V / 1024).

ADC Steps Infographic

The Code: syntax analogRead()

Reading this value is absurdly easy.

int sensorValue = analogRead(A0);

That’s it. sensorValue will now hold a number between 0 and 1023.

Project 1: The Serial Monitor (Seeing the Matrix)

Since we don’t have a screen on the Arduino, how do we know what the value is? We use the Serial Monitor. It allows the Arduino to send text back to your computer via the USB cable.

The Code

// Day 14: Analog Read Test

void setup() {
  // Start serial communication at 9600 bits per second
  Serial.begin(9600);
}

void loop() {
  // Read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  
  // Print out the value:
  Serial.println(sensorValue);
  
  // Wait a bit so we aren't flooded with data
  delay(100);
}

How to Monitor

  1. Upload the code.
  2. Click the Magnifying Glass Icon (Top Right of IDE).
  3. Turn the knob on your potentiometer.
  4. Watch the numbers scroll: 0, 10, 50, 500, 1000, 1023.

Serial Monitor Screenshot

Breadboard Wiring A0

Seeing numbers is boring. Let’s make them do something. Let’s use the knob to control the speed of a blinking LED. Remember delay(1000)? What if we replaced 1000 with our sensorValue?

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

void loop() {
  int speed = analogRead(A0); // Read the knob (0-1023)
  
  digitalWrite(13, HIGH);
  delay(speed); // Wait for 0ms to 1023ms
  digitalWrite(13, LOW);
  delay(speed);
}

Try it.

  • Turn knob left: The LED buzzes (strobe light).
  • Turn knob right: The LED blinks slowly (1 sec interval).

Concept 2: The map() Function

There is a problem. The ADC gives us 0 to 1023. But what if we want to control an LED’s brightness (which we will learn tomorrow uses 0-255)? Or what if we want to control a Servo motor (0 to 180 degrees)?

We need to compress the range. We could do math: value / 4. But Arduino has a magic function called map().

Syntax

output = map(value, fromLow, fromHigh, toLow, toHigh);

Example: Convert Potentiometer (0-1023) to Percentage (0-100). int percent = map(sensorValue, 0, 1023, 0, 100);

This performs the linear interpolation for you. It works backwards too! int reverse = map(sensorValue, 0, 1023, 100, 0); (Turning the knob right makes output go down).

Challenge: The DIY VU Meter

You know those bouncing bars on a stereo system? You can build one. Hardware:

  • Potentiometer on A0.
  • 5 LEDs on Pins 2, 3, 4, 5, 6. Software Logic:
  • Read A0.
  • If value > 100, turn on LED 1.
  • If value > 300, turn on LED 2.
  • If value > 500, turn on LED 3.
  • … This is basically a “Battery Level Indicator”. Try to code it using an Array (from Day 13) to keep it clean!

History: Who invented the Potentiometer?

It was invented by Thomas Edison in 1872. (Or at least, he improved the rheostat design significantly). The name comes from “Potential” (Voltage) and “Meter” (Measure). Originally, it was used to calibrate telegraph lines. Today, 150 years later, the design is almost identical. A wiper sliding on Carbon. If it isn’t broken, don’t fix it.

Limitations: The map() Trap

A warning about the map() function. It uses Integer Math. It chops off decimals. If you try map(val, 0, 1023, 0, 5), and the result should be 2.8, map will give you 2. It does not round up. It just deletes the decimal. If you need precision, do the math manually using float.

Map Function Funnel Diagram

Math Class: Converting Bits back to Volts

Sometimes you want to know the actual voltage, not just a number from 0-1023. How do we reverse the math? float voltage = sensorValue * (5.0 / 1023.0); Why the .0? Because in C++, dividing integers (5/1023) results in zero. You must use Floating Point numbers. If sensorValue is 512: 5120.00488=2.50512 * 0.00488 = 2.50 Volts.

Hardware Deep Dive: Linear vs Logarithmic Pots

Go look at your potentiometer. Does it say B10K or A10K?

  • B-Type (Linear): 50% turn = 50% resistance. Perfect for Arduino inputs.
  • A-Type (Logarithmic/Audio): 50% turn = 10% resistance. Designed for Volume knobs (because human ears hear logarithmically). Warning: If you use an A-Type pot for this project, the numbers will jump up suddenly at the end of the turn. It works, but it feels weird. Stick to B-Type.

Advanced Project: The Smart Night Light

Let’s combine logic (Day 7) with analog sensing. We want an LED to turn on automatically if it gets dark. (Pretend the Potentiometer is an LDR light sensor for now).

int threshold = 300; // The "Darkness" limit

void loop() {
  int lightLevel = analogRead(A0);
  
  if (lightLevel < threshold) {
    digitalWrite(13, HIGH); // Turn on lights!
  } else {
    digitalWrite(13, LOW);  // Save energy
  }
}

This is a Threshold Trigger. Every smart device uses this logic. The thermostat triggers only when temp < 18°C. Analog Input -> Digital Decision.

Advanced Academy: Smoothing the Noise

Remember how we said analogRead can be jumpy? If your numbers are bouncing (510, 515, 509, 512), you can smooth them out using software. It’s called Averaging. Instead of taking 1 reading, take 10 readings and divide by 10.

long total = 0;
for (int i = 0; i < 10; i++) {
  total = total + analogRead(A0);
  delay(1);
}
int average = total / 10;

This simple code makes your knob feel “Heavy” and smooth, like a luxury car volume dial.

Concept 3: The Joystick Secret

Have you ever used a PS5 or Xbox controller? The thumbsticks? They are just Two Potentiometers stuck together.

  • One tracks X-Axis (Left-Right).
  • One tracks Y-Axis (Up-Down). When you push the stick forward, you are just turning a hidden knob. Now that you know how to read one pot, you know how to read a game controller. Just connect X to A0 and Y to A1.

Deep Dive: The AREF Pin (Advanced Accuracy)

You might have noticed a pin on the Arduino called AREF (Analog Reference). By default, the ADC compares input voltage to 5V. But what if you are measuring a sensor that only outputs 0-3.3V? You are wasting the top range of your ADC bits. You can connect 3.3V to the AREF pin and tell Arduino: “Use THIS as the ceiling, not 5V.” analogReference(EXTERNAL); This increases your precision significantly. Warning: If you use EXTERNAL reference, and you accidentally touch 5V to the analog pin… you destroy the ADC. Use with caution.

The Sensor Compatible List

Now that you know analogRead(), you can read ANY sensor that outputs 0-5V. Here is your shopping list for the future:

  1. LDR (Light Dependent Resistor): Dark = High Resistance. (Day 16).
  2. Thermistor: Heat = Low Resistance.
  3. Flex Sensor: Bent = High Resistance. (Used in Power Gloves).
  4. Force Sensitive Resistor (FSR): Squeeze = Low Resistance.
  5. Hall Effect Sensor: Magnet = Voltage.
  6. Piezo Element: Vibration = Voltage Spike. They all work exactly the same way.
  7. Build a Voltage Divider.
  8. Connect middle to A0.
  9. analogRead(A0). You have unlocked the physical world.

Deep Dive: Floating Pins (Ghost Data)

Try this experiment.

  1. Unplug the Potentiometer from Pin A0.
  2. Run the Serial Monitor code.
  3. Watch the numbers. They won’t be 0. They will jitter wildly. 300… 120… 800… 250… Why? The pin is Floating. It acts like an antenna. It is picking up magnetic interference from your mains electricity (50Hz/60Hz), your WiFi router, and even your own hand. Rule: analogRead() is random noise unless something is firmly connected to it. (This is actually how you generate random numbers on Arduino: randomSeed(analogRead(A0))).

Digital vs Analog Waves

Diagnostics: The Multimeter Truth

Don’t trust the code blindly. If analogRead says “512”, get your multimeter (Day 3). Put the black probe on GND and the red probe on the Wiper (Leg 2). It should read 2.50V. If it reads 2.50V but Arduino says “900”, your reference voltage (5V USB) might be fluctuating. This happens often when running off laptop USB power. The “5V” is often actually 4.7V. This causes ADC errors. For precision, use an external 9V power supply.

R3 vs R4: The Resolution War

If you have the new Arduino Uno R4, you have a superpower. The R4 has a 14-bit ADC. 214=16,3842^{14} = 16,384 steps. While the R3 can only detect changes of 4.9mV. The R4 can detect changes of 0.3 millivolts. This allows for extreme precision in scientific sensors. However, by default, the R4 runs in 10-bit mode to remain compatible with old code. You have to unlock the 14-bit mode manually (analogReadResolution(14)).

R3 vs R4 Resolution Comparison

Troubleshooting

  1. Potentiometer gets hot? You wired 5V directly to GND through the pot (Short Circuit). Check your wiring! The Middle pin MUST go to the Analog Input (A0).
  2. Values are backwards? 1023 when turned left? Swap the 5V and GND wires on the outer legs.
  3. Values don’t reach 0 or 1023? Cheap potentiometers often have mechanical limits. They might only go from 10 to 1010. This is normal.
  4. Values are shaky? The contact wiper inside might be dirty. Wiggle it back and forth to clean it.

The Engineer’s Glossary (Day 14)

  • ADC: Analog to Digital Converter.
  • Resolution: The number of steps the voltage is chopped into (10-bit = 1024).
  • Potentiometer: Variable resistor used as a voltage divider.
  • Floating: An input pin not connected to anything, picking up noise.
  • Linear: The change is constant (Straight line graph).
  • Logarithmic: The change curves (Used for Audio Volume knobs).

The Bill of Materials (BOM)

ComponentQuantityValueNotes
Arduino Uno1R3 or R4The Brain
Potentiometer110kΩ (B10K)The Sensor
LED1ColorOutput Indicator
Resistor1220ΩFor LED
Breadboard1Half+Prototyping

Conclusion

You have opened the eyes of the machine. Before today, your Arduino was blind. Now, it can sense intensity. Using analogRead, you can read:

  • LDRs: Light levels (Day 16).
  • Thermistors: Temperature.
  • Microphones: Sound volume.
  • Flex Sensors: Finger bending.

Tomorrow, we do the opposite. We have learned analogRead (Input). Tomorrow, we learn analogWrite (Output). But wait… digital pins can’t output 2.5V… or can they? Get ready for PWM (Pulse Width Modulation) and the art of faking it.

See you on Day 15.

Comments