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]

44444.png

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

1.png

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:

  1. Arduino UNO board
  2. Potentiometer
  3. Breadboard
  4. 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

2.png


Place the potentiometer onto the breadboard.


Wire the Circuit


  1. Connect the middle pin of the potentiometer to A0 (analog pin 0) on the Arduino board.
  2. Attach one of the outer pins of the potentiometer to VCC (5V).
  3. 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:

  1. Open the Serial Monitor in the Arduino IDE.
  2. 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:

  1. LED
  2. Resistor (220 ohm)

Build the Circuit

33333.png


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.