The Arduino Uno supports analog input and output operations, enabling you to interact with sensors and actuators requiring precise values. Analog operations are essential for controlling devices like LEDs, motors, and reading inputs from sensors such as potentiometers or light sensors. This tutorial will guide you through the setup, analog reading and writing, and using logic operations like if
statements with analog data.
What You Will Need
- Arduino Uno with USB Cable
- Potentiometer (or any variable resistor) for analog input
- LED and a 220-ohm Resistor for analog output
- Breadboard and Jumper Wires
- A computer with the Arduino IDE installed
Step 1: Understanding Analog Pins on Arduino
The Arduino Uno has six analog input pins (A0-A5) that can read a voltage between 0 and 5V and convert it into a digital value between 0 and 1023. For analog output, Arduino uses PWM (Pulse Width Modulation) on certain digital pins marked with ~
(e.g., 3, 5, 6, 9, 10, 11).
Functions Used
-
Analog Input:
analogRead(pin)
- Reads a voltage (0-5V) and returns a value between 0 and 1023.
-
Analog Output:
analogWrite(pin, value)
- Outputs a PWM signal where
value
ranges from 0 (0% duty cycle) to 255 (100% duty cycle).
- Outputs a PWM signal where
Step 2: Wiring Analog Input (Potentiometer)
Connect a potentiometer to an Arduino analog pin:
Potentiometer Pin | Arduino Connection |
---|---|
1 (End Pin) | 5V |
2 (Middle/Output) | A0 |
3 (End Pin) | GND |
Step 3: Wiring Analog Output (LED)
Connect an LED with a 220-ohm resistor to an Arduino PWM pin (e.g., pin 9):
LED Pin | Arduino Connection |
---|---|
Long Leg (+) | Digital Pin 9 |
Short Leg (-) | GND |
Step 4: Reading Analog Input
Use the analogRead()
function to read data from the potentiometer:
Example Code: Reading Analog Input
#define potPin A0 // Potentiometer connected to A0
void setup() {
Serial.begin(9600);
Serial.println("Analog Input Test");
}
void loop() {
int sensorValue = analogRead(potPin); // Read analog value
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
delay(500); // Wait for half a second
}
Run the code and observe the potentiometer values (0-1023) in the Serial Monitor.
Step 5: Writing Analog Output
Use the analogWrite()
function to control the brightness of an LED:
Example Code: Analog Output to LED
#define ledPin 9 // LED connected to pin 9
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Increase brightness
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Decrease brightness
delay(10);
}
}
Step 6: Combining Analog Input and Output
You can use analog input values to control the output, such as adjusting LED brightness based on the potentiometer position.
Example Code: Potentiometer Controls LED Brightness
#define potPin A0 // Potentiometer connected to A0
#define ledPin 9 // LED connected to pin 9
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(potPin); // Read potentiometer value
int brightness = map(sensorValue, 0, 1023, 0, 255); // Map to PWM range
analogWrite(ledPin, brightness); // Set LED brightness
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" -> Brightness: ");
Serial.println(brightness);
delay(100);
}
Step 7: Using if
Statements with Analog Data
You can create conditions using analog input to perform specific actions:
Example Code: Conditional Control
#define potPin A0 // Potentiometer connected to A0
#define ledPin 9 // LED connected to pin 9
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(potPin);
if (sensorValue > 512) {
analogWrite(ledPin, 255); // Full brightness if sensor value > 512
Serial.println("Bright!");
} else {
analogWrite(ledPin, 0); // Turn off LED otherwise
Serial.println("Off");
}
delay(500);
}
Applications of Analog Read/Write
- Reading environmental sensors (e.g., light, temperature, humidity)
- Adjusting motor speeds
- Controlling LED brightness
- Audio signal processing
- Creating analog-based user interfaces
Troubleshooting
- Incorrect readings from the sensor: Ensure proper wiring and verify the potentiometer connections.
- LED not lighting up: Confirm the LED orientation and use a resistor to limit current.
- Output not smooth: Add a small delay or average the input values for stability.
Conclusion
You’ve learned how to perform analog read and write operations using Arduino Uno, map sensor values to outputs, and use if
statements for conditional logic. These skills are crucial for creating responsive, interactive projects. Experiment further by integrating other analog sensors and actuators to expand your capabilities!