Arduino Analog Read with Potentiometer

Difficulty Level: Beginner

In this experiment, you’ll learn how to read an analog signal from a potentiometer using the Arduino. This tutorial introduces the analogRead() function, which converts analog input into a readable value between 0 and 1023.

Components Needed

To complete this project, you'll need the following components:

Step-by-Step Wiring Instructions

Follow these instructions to wire the potentiometer correctly:

  • The middle pin of the potentiometer connects to analog input pin A0 on the Arduino.
  • One outer pin connects to 5V (power).
  • The other outer pin connects to GND (ground).

Arduino Code

Copy and paste the following code into your Arduino IDE:


// Analog Read with Potentiometer

// Define potentiometer pin
int potPin = A0;
int potValue = 0;  // Variable to store the potentiometer value

// Setup function
void setup() {
  // Initialize serial communication at a baud rate of 9600
  Serial.begin(9600);
}

// Main loop function
void loop() {
  // Read the analog value from the potentiometer
  potValue = analogRead(potPin);

  // Output the potentiometer value to the Serial Monitor
  Serial.println(potValue);

  // Delay for readability
  delay(500);
}
                

Testing the Code

To test the code, follow these steps:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and copy the code above into the editor.
  3. Select your board and the correct port in the IDE.
  4. Click the upload button to upload the code to the Arduino.
  5. Open the Serial Monitor (Tools > Serial Monitor) and watch the potentiometer values change as you adjust the knob.

How the Code Works

In this sketch, the potentiometer's position changes the value read from 0 to 1023, corresponding to the voltage range of 0 to 5V (or 0 to 3.3V, depending on your board).

Main Functions Explained

  • analogRead(pin): Reads the analog value (0-1023) from the specified analog input pin.
  • Serial.println(): Outputs the potentiometer’s value to the Serial Monitor for real-time feedback.

Code Breakdown

In the setup() function, we start serial communication with the Serial.begin(9600) command. In the loop() function, the analogRead() function reads the potentiometer's value, which ranges from 0 to 1023. This value is then printed to the Serial Monitor, giving real-time feedback as the potentiometer is adjusted.

Conclusion

By completing this experiment, you learned how to read an analog signal using the Arduino's analogRead() function. This type of reading is useful in controlling different parameters, such as motor speed, brightness, or other analog components in your projects.