Analog Inputs With Arduino UNO [Code and Circuit Diagram]
by STEAM-DIY in Circuits > Arduino
152 Views, 1 Favorites, 0 Comments
Analog Inputs With Arduino UNO [Code and Circuit Diagram]
Welcome back to another exciting project! In this guide, we will learn how to get analog inputs using the Arduino UNO board. If you're new to analog inputs, they help convert real-world data, such as light, temperature, or in our case, a variable resistance (potentiometer), into signals the Arduino can understand.
Supplies
We'll be using the analog pins on the Arduino, which can read values ranging from 0 to 1023, and then display them on the Serial Monitor.
Supplies Needed:
- Arduino UNO board –
- Potentiometer –
- Breadboard –
- Jumper wires –
Identify Your Components
Gather all the components: Arduino UNO, potentiometer, breadboard, and jumper wires. The potentiometer is the key component in this project, as it allows us to simulate analog input by adjusting its resistance.
Connect the Potentiometer
Place the potentiometer onto the breadboard.
Wire the Circuit
- Connect the middle pin of the potentiometer to A0 (analog pin 0) on the Arduino board.
- Attach one of the outer pins of the potentiometer to VCC (5V).
- The other outer pin should go to GND.
Upload the Code
Now that the circuit is ready, let's upload the code.
void setup() {
Serial.begin(9600); // Enable Serial Monitor
}
void loop() {
int value = analogRead(A0); // Read analog value from A0
Serial.println(value); // Print value to Serial Monitor
}
This code will read the analog value from pin A0 and display it on the Serial Monitor.
Upload and Monitor the Output
Once the code is uploaded:
- Open the Serial Monitor in the Arduino IDE.
- Rotate the potentiometer, and you'll see the values on the Serial Monitor change in real-time.
Bonus Project: Controlling LED Brightness With Potentiometer
Now, let's take this project a step further by controlling an LED’s brightness based on the potentiometer's position.
Additional Supplies:
- LED
- Resistor (220 ohm)
Build the Circuit
Connect the LED to pin 9 (PWM pin) and add a resistor in series to limit the current. The potentiometer wiring remains the same as in the previous circuit.
Code
void setup() {
pinMode(9, OUTPUT); // Define LED pin
Serial.begin(9600); // Enable Serial Monitor
}
void loop() {
int brightness = analogRead(A0); // Read analog value
brightness = map(brightness, 0, 1023, 0, 255); // Map value to 0-255
analogWrite(9, brightness); // Control LED brightness
Serial.println(brightness); // Print brightness value
}
Observe the Changes
Rotate the potentiometer and watch how the LED's brightness changes. The values will also be displayed in the Serial Monitor.