Lesson 7: Analog Inputs: Potentiometers, Light and SoundNew-age
Digital inputs tell you only on or off. The real world is rarely so simple: a light level, a temperature, the position of a knob, the loudness of a sound — all of these vary smoothly, and a microcontroller reads them through an analog input. This lesson explains how analog-to-digital conversion works, how to wire the three commonest analog sensors, and how to turn raw readings into useful values such as degrees, percentages and thresholds.
You will build a dimmer, a light-controlled lamp and a sound-triggered alarm, and you will learn the two calculations that appear in every analog project: mapping a reading from one range to another, and smoothing a noisy signal so that it does not flicker.
- Analog input — a pin that reads a continuous voltage and converts it to a number.
- ADC — analog-to-digital converter: the circuit that turns a voltage into a number.
- Resolution — how many distinct values an ADC can report, such as 1024 values for a 10-bit ADC.
- Potentiometer — a variable resistor with a knob, used as a dial or position sensor.
- LDR — light-dependent resistor: its resistance falls as light increases.
- Voltage divider — two resistors in series that split a voltage in proportion to their values.
- Mapping — converting a reading from one range to another, such as 0–1023 to 0–255.
- Smoothing — averaging several readings to reduce noise.
How analog reading works
An analog pin measures a voltage between 0 V and the supply (usually 5 V or 3.3 V) and converts it to a number. On an Arduino Uno the ADC has 10-bit resolution, so it reports 1024 values: 0 for 0 V, 1023 for 5 V, and about 512 for 2.5 V. Each step is therefore about 5 mV. That is precise enough for most sensors, but it also means the reading is sensitive to noise — a long wire or a nearby motor can make the last digits flicker.
Read an analog pin with analogRead(), which returns an integer from 0 to 1023. The value is a measurement of voltage, not of the thing you care about: a temperature sensor’s voltage must be converted to degrees, a light sensor’s reading to a brightness. That conversion is your code’s job, and it is usually a simple calculation based on the sensor’s datasheet.
The potentiometer and the voltage divider
A potentiometer is a resistor with a sliding contact, controlled by a knob. Wire its two outer pins to 5 V and ground, and its middle pin to an analog input: turning the knob moves the middle pin’s voltage smoothly from 0 V to 5 V, and the reading moves from 0 to 1023. It is the simplest analog input, and it is the standard way to add a dial to a project — for volume, speed, brightness or a menu selection.
The potentiometer is a voltage divider: two resistances in series, with the output taken from the join. The same idea is used to read sensors that change resistance, such as an LDR. Wire the LDR in series with a fixed resistor between 5 V and ground, and read the voltage at the join: as light increases, the LDR’s resistance falls, and the voltage at the join changes. Choose the fixed resistor to be near the LDR’s resistance in typical light — 10 kΩ is a good start — so the reading sits in the middle of its range.
Light and sound sensors
An LDR reads light level, but its response is not linear: it is very sensitive in dim light and less sensitive in bright light, and it reacts slowly, over tens of milliseconds. That makes it good for detecting day and night, or whether a lamp is on, and poor for measuring exact brightness. If you need a threshold — “turn the lamp on when it gets dark” — compare the reading with a value you choose by testing, not by calculating from the datasheet.
Mapping and smoothing
Mapping converts a reading from one range to another. The Arduino map() function does this: map(reading, 0, 1023, 0, 255) turns an analog reading into a PWM brightness value. But map() is linear, and human senses are not: doubling the PWM value does not look twice as bright. For a dimmer that feels smooth, you may want to square the mapped value or use a lookup table — a small refinement that makes a project feel professional.
Worked example — a light-controlled lamp
Wire an LDR in a voltage divider with a 10 kΩ resistor, read it on pin A0, and switch an LED on pin 13 when it gets dark. Test first: in daylight the reading is about 800, in a dim room about 300. Choose a threshold of 500 and add hysteresis — turn on below 450, turn off above 550 — so the lamp does not flicker when the light hovers near the threshold.
int v = analogRead(A0); if (v < 450) digitalWrite(13, HIGH); if (v > 550) digitalWrite(13, LOW);
Common mistakes
- Treating the raw ADC number as the physical value. Convert it: a reading of 512 is 2.5 V, not 512 of anything.
- Using a single threshold for on and off, so the output flickers when the reading hovers near it.
- Forgetting that an LDR is slow and non-linear — it is good for thresholds, poor for precise measurement.
- Reading an analog pin with nothing connected and treating the random numbers as data.
Discussion questions
- An LDR reading of 512 could mean many different light levels in different rooms. What does that tell you about calibrating a sensor versus calculating from a datasheet?
- Smoothing makes a reading stable but slow. When would you prefer a fast, noisy reading instead?
- A potentiometer gives a position; a button gives an event. Which kinds of control suit each, and why?
Practice questions
- A 10-bit ADC reads 512 on a 5 V Arduino. What voltage is that? Answer: About 2.5 V. Each step is 5 ÷ 1023 ≈ 4.9 mV, so 512 × 4.9 mV ≈ 2.5 V. Explanation: Reading × step size = voltage.
- Why does a potentiometer’s middle pin give a voltage between 0 V and 5 V? Answer: It is a voltage divider: the knob moves the contact along the resistance, splitting the supply voltage in proportion. Explanation: Two resistances, one output.
- What is hysteresis, and why does the lamp example use it? Answer: Two different thresholds for switching on and off (450 and 550). It stops the lamp flickering when the reading hovers near one threshold. Explanation: A gap between on and off.
- Your LDR reading flickers between 490 and 510. What is the simplest fix? Answer: Smooth it: average the last ten readings and use the average. The flicker is noise, not real change. Explanation: Average out the noise.
- map(reading, 0, 1023, 0, 255) gives 127 for a reading of 512. Why might the LED still look dim? Answer: Human brightness perception is not linear, and 127 out of 255 PWM is about half power but looks dimmer than half. Squaring the mapped value feels smoother. Explanation: Eyes are not linear.
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.