Reading Analog Signals on Arduino with analogRead()
Reading Analog Signals on Arduino with analogRead()
Arduino can read continuous analog signals, unlike digital signals which only have two states (0 and 1). Many external devices such as potentiometers, light sensors, and temperature sensors output voltage levels that we can read through Arduino’s analog inputs.
How Analog Input Works on Arduino
Arduino boards contain a built-in Analog to Digital Converter (ADC) with 6 channels labeled A0-A5.
- Accepts voltage range from 0 to 5 volts
- Converts to 10-bit digital values from 0 to 1023
- No unit attached (raw digital value from ADC)
Using analogRead()
The function to read analog values is analogRead(pin) where pin is one of A0-A5.
// Define the pin connected to the analog device
const int sensorPin = A0;
void setup() {
Serial.begin(9600); // Open Serial Monitor to see values
}
void loop() {
int value = analogRead(sensorPin); // Read value 0-1023
Serial.println(value); // Print value to Serial
delay(100);
}
Example: Control LED Blink Speed with Potentiometer
Connect a potentiometer to A3 and use the read value to control how fast an LED on pin 13 blinks.
[image: Breadboard wiring showing potentiometer connected to Arduino with A3 and LED on pin 13 labeled]
// Define potentiometer pin and LED pin
const int sensorPin = A3;
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read analog value from potentiometer
int sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
// Pass value to Blinky function to control LED
Blinky(sensorValue);
}
void Blinky(int timeMs) {
digitalWrite(ledPin, HIGH);
delay(timeMs); // Delay equals the read value
digitalWrite(ledPin, LOW);
delay(timeMs);
}
Key Concepts to Understand
ADC Resolution
A 10-bit ADC divides the 0-5V range into 1024 steps (0-1023). If the voltage changes very slightly, the read value may not change at all. This is a hardware limitation of Arduino’s built-in converter.
Converting ADC Value to Voltage
If you need the actual voltage in volts, apply this formula:
volts = (read value / 1023) × 5
float voltage = (analogRead(A0) / 1023.0) * 5.0;
Serial.println(voltage); // Shows voltage like 2.34
Do Not Use Digital Pins for Analog Input
Digital pins (D0-D13) only work with digital signals. To read analog input, you must use the analog pins A0-A5.
[image: Arduino UNO pinout diagram highlighting A0-A5 as analog input channels]
Reference Video
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย