In this Arduino project, we will learn how to read analog input using a potentiometer and connect it to an analog input pin on the Arduino board. We will also write a program that reads and displays the current values.

Introduction to Analog I/O

In the previous Arduino Project #2, we covered reading digital inputs, where the value could only be 0 or 1 (LOW or HIGH). However, with analog input/output (I/O), we can read a range of values. Arduino boards have a set of pins dedicated to analog I/O, denoted with an “A” prefix. For example, the Arduino Uno has 6 analog I/O pins, labeled from A0 to A5. The Arduino MKR 1010 WiFi, on the other hand, has 7 analog I/O pins, from A0 to A6. The number and placement of these pins may vary depending on the Arduino board model.

Using Analog Signals

Analog signals can represent a wide range of values. In the case of Arduino, the analog inputs can read values from 0 to 1023, which corresponds to a voltage level from 0V to 5V. Many sensors, such as temperature sensors, distance sensors, sound sensors, and motion sensors, provide us with analog values. With Arduino, we can read these values and perform various actions based on the data received.

Reading Analog Input with analogRead()

To read analog input, we can use the analogRead() function provided by Arduino. Before starting the programming part, let’s build a simple circuit to connect a potentiometer to the Arduino. This circuit is similar to the one we used in the “Build a LED dimmer with a potentiometer” project, with the LED replaced by the Arduino.

Circuit Setup

To set up the circuit, follow these steps:

  1. Connect the 0V (GND) and 5V output pins of the Arduino to the power rails on the breadboard.
  2. Using jumper wires, connect the GND rail to one of the outer pins (usually the leftmost) of the potentiometer.
  3. Connect the 5V rail to the other outer pin (usually the rightmost) of the potentiometer.
  4. Connect the middle pin of the potentiometer to the analog pin A1 on the Arduino.
  5. Make sure the connections are secure and the circuit is properly set up.

Reading Analog Input with the Arduino Program

Once the circuit is set up, we can write an Arduino program to read the analog input and display the values on the serial monitor in the Arduino IDE. Here is an example program:

void setup() {
 Serial.begin(9600);
}

void loop() {
 int value = analogRead(A1);
 Serial.println(value);
}

In this program, we utilize the Serial library to communicate with the Arduino IDE’s serial monitor. We set the baud rate to 9600 in the setup() function. In the loop() function, we use the analogRead() function to read the value from the analog pin A1, which is connected to the potentiometer. We then print the value read to the serial monitor using Serial.println(). Note that we don’t need to explicitly set A1 as an input pin since it is already configured as an input by default.

Interpreting the Results

After uploading the program to the Arduino board, we can open the serial monitor in the Arduino IDE to view the analog input readings. When the potentiometer is turned all the way to the left, the reading should be around 0. When turned all the way to the right, the reading should be around 1023 (sometimes 1022 due to analog precision limitations). Values in between indicate the position of the potentiometer.

By reading and interpreting these analog input values, we can implement various functionalities and control other components based on the sensor data.

Tags: Arduino, Analog Input, Sensor Data, Potentiometer, Circuit, Serial Monitor, analogRead