Potentiometer Analog Input Using Arduino Uno in Wokwi – Complete Beginner Guide

 Introduction


In this project, we will learn how to read analog input from a potentiometer using Arduino Uno in the Wokwi simulator. A potentiometer is a variable resistor that allows us to control voltage levels, making it perfect for adjusting brightness, motor speed, or sensor calibration.

This is one of the most important beginner Arduino projects because it teaches:

  • Analog signal reading

  • ADC (Analog to Digital Conversion)

  • Serial Monitor output

  • Real-time value control

You can simulate this project online using Wokwi without any physical hardware.


Components Required (Wokwi Simulation)

  • Arduino Uno

  • Potentiometer (10kΩ)

  • Jumper wires


Circuit Connections

Potentiometer Pin Connections:

Potentiometer PinArduino Connection
Left Pin5V
Middle Pin (Wiper)A0
Right PinGND

The middle pin sends variable voltage to Arduino analog pin A0.

How the Code Works

  • analogRead(A0) reads voltage from the potentiometer.

  • Arduino converts analog voltage (0–5V) into digital values (0–1023).

  • The value is printed on the Serial Monitor.

  • Rotating the knob changes the output value in real-time.


Code:
/*
 * Potentiometer Analog Input with Arduino
 * Features: LED brightness control, servo motor, RGB LED, serial output
 * Demonstrates analog input reading and PWM output
 */

#include <Servo.h>

// Pin definitions
const int POT_PIN = A0;        // Potentiometer connected to A0
const int LED_PIN = 9;         // PWM LED on pin 9
const int LED_RED = 3;         // RGB LED Red
const int LED_GREEN = 5;       // RGB LED Green
const int LED_BLUE = 6;        // RGB LED Blue
const int SERVO_PIN = 10;      // Servo motor pin

// Variables
int potValue = 0;              // Raw potentiometer value (0-1023)
int brightness = 0;            // LED brightness (0-255)
int servoAngle = 0;            // Servo angle (0-180)
float voltage = 0.0;           // Voltage reading (0-5V)
int percentage = 0;            // Percentage (0-100%)

// Servo object
Servo myServo;

// Timing variables
unsigned long lastUpdate = 0;
const int updateInterval = 100; // Update every 100ms

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
 
  // Configure LED pins as outputs
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
 
  // Attach servo
  myServo.attach(SERVO_PIN);
 
  // Startup message
  Serial.println("=================================");
  Serial.println("  Potentiometer Analog Input");
  Serial.println("=================================");
  Serial.println();
  Serial.println("Raw\tVoltage\tPercent\tBright\tAngle");
  Serial.println("-----------------------------------------");
 
  // Initial test sequence
  testSequence();
}

void loop() {
  unsigned long currentMillis = millis();
 
  // Read and process potentiometer value
  readPotentiometer();
 
  // Control outputs based on potentiometer
  controlLED();
  controlRGBLED();
  controlServo();
 
  // Display data on serial monitor
  if (currentMillis - lastUpdate >= updateInterval) {
    lastUpdate = currentMillis;
    displayData();
  }
 
  delay(10); // Small delay for stability
}

// Read potentiometer value and calculate derived values
void readPotentiometer() {
  potValue = analogRead(POT_PIN);
 
  // Calculate voltage (0-5V)
  voltage = potValue * (5.0 / 1023.0);
 
  // Calculate percentage (0-100%)
  percentage = map(potValue, 0, 1023, 0, 100);
 
  // Calculate LED brightness (0-255)
  brightness = map(potValue, 0, 1023, 0, 255);
 
  // Calculate servo angle (0-180)
  servoAngle = map(potValue, 0, 1023, 0, 180);
}

// Control single LED brightness with PWM
void controlLED() {
  analogWrite(LED_PIN, brightness);
}

// Control RGB LED colors based on potentiometer position
void controlRGBLED() {
  if (potValue < 341) {
    // Range 0-33%: Red
    analogWrite(LED_RED, map(potValue, 0, 341, 0, 255));
    analogWrite(LED_GREEN, 0);
    analogWrite(LED_BLUE, 0);
  } else if (potValue < 682) {
    // Range 34-66%: Green
    analogWrite(LED_RED, 0);
    analogWrite(LED_GREEN, map(potValue, 341, 682, 0, 255));
    analogWrite(LED_BLUE, 0);
  } else {
    // Range 67-100%: Blue
    analogWrite(LED_RED, 0);
    analogWrite(LED_GREEN, 0);
    analogWrite(LED_BLUE, map(potValue, 682, 1023, 0, 255));
  }
}

// Control servo motor position
void controlServo() {
  myServo.write(servoAngle);
}

// Display all values on serial monitor
void displayData() {
  // Tabular format for Serial Monitor
  Serial.print(potValue);
  Serial.print("\t");
  Serial.print(voltage, 2);
  Serial.print("V\t");
  Serial.print(percentage);
  Serial.print("%\t");
  Serial.print(brightness);
  Serial.print("\t");
  Serial.print(servoAngle);
  Serial.print("°");
  Serial.println();
}

// Startup test sequence
void testSequence() {
  Serial.println("Running startup test...");
 
  // Test LED brightness
  for (int i = 0; i <= 255; i += 5) {
    analogWrite(LED_PIN, i);
    delay(10);
  }
  for (int i = 255; i >= 0; i -= 5) {
    analogWrite(LED_PIN, i);
    delay(10);
  }
 
  // Test RGB colors
  analogWrite(LED_RED, 255);
  delay(300);
  analogWrite(LED_RED, 0);
 
  analogWrite(LED_GREEN, 255);
  delay(300);
  analogWrite(LED_GREEN, 0);
 
  analogWrite(LED_BLUE, 255);
  delay(300);
  analogWrite(LED_BLUE, 0);
 
  // Test servo sweep
  for (int angle = 0; angle <= 180; angle += 10) {
    myServo.write(angle);
    delay(20);
  }
  for (int angle = 180; angle >= 0; angle -= 10) {
    myServo.write(angle);
    delay(20);
  }
 
  Serial.println("Test complete! Ready for input.");
  Serial.println();
}



Understanding Analog Input in Arduino

The Arduino Uno has a 10-bit ADC (Analog to Digital Converter).

This means:

  • 0V → 0

  • 5V → 1023

  • 2.5V → Around 512

This allows precise voltage measurement and control.

Diagram.json:

{
  "version": 1,
  "author": "Potentiometer Analog Input",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-arduino-uno",
      "id": "uno",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "type": "wokwi-potentiometer",
      "id": "pot1",
      "top": -57.6,
      "left": -201.6,
      "rotate": 90,
      "attrs": {
        "value": "512"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -96,
      "left": 153.6,
      "attrs": {
        "color": "yellow"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -9.6,
      "left": 153.6,
      "rotate": 90,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led_red",
      "top": -96,
      "left": 249.6,
      "attrs": {
        "color": "red"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led_green",
      "top": -96,
      "left": 297.6,
      "attrs": {
        "color": "green"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led_blue",
      "top": -96,
      "left": 345.6,
      "attrs": {
        "color": "blue"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -9.6,
      "left": 249.6,
      "rotate": 90,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -9.6,
      "left": 297.6,
      "rotate": 90,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -9.6,
      "left": 345.6,
      "rotate": 90,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-servo",
      "id": "servo1",
      "top": 172.8,
      "left": -288,
      "attrs": {}
    }
  ],
  "connections": [
    [
      "pot1:GND",
      "uno:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "pot1:VCC",
      "uno:5V",
      "red",
      [
        "v0"
      ]
    ],
    [
      "pot1:SIG",
      "uno:A0",
      "green",
      [
        "v0"
      ]
    ],
    [
      "led1:A",
      "r1:1",
      "yellow",
      [
        "v0"
      ]
    ],
    [
      "r1:2",
      "uno:9",
      "yellow",
      [
        "v0"
      ]
    ],
    [
      "led1:C",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "led_red:A",
      "r2:1",
      "red",
      [
        "v0"
      ]
    ],
    [
      "r2:2",
      "uno:3",
      "red",
      [
        "v0"
      ]
    ],
    [
      "led_red:C",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "led_green:A",
      "r3:1",
      "green",
      [
        "v0"
      ]
    ],
    [
      "r3:2",
      "uno:5",
      "green",
      [
        "v0"
      ]
    ],
    [
      "led_green:C",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "led_blue:A",
      "r4:1",
      "blue",
      [
        "v0"
      ]
    ],
    [
      "r4:2",
      "uno:6",
      "blue",
      [
        "v0"
      ]
    ],
    [
      "led_blue:C",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "servo1:GND",
      "uno:GND.3",
      "black",
      [
        "v0"
      ]
    ],
    [
      "servo1:V+",
      "uno:5V",
      "red",
      [
        "v0"
      ]
    ],
    [
      "servo1:PWM",
      "uno:10",
      "orange",
      [
        "v0"
      ]
    ]
  ],
  "dependencies": {}
}



Applications of Potentiometer with Arduino

  • LED brightness control

  • Servo motor angle control

  • Volume adjustment systems

  • Sensor calibration

  • Robotics control panels

  • IoT variable input systems


Why Use Wokwi Simulator?

  • No hardware required

  • Easy debugging

  • Fast circuit setup

  • Perfect for students and beginners

Wokwi makes learning Arduino projects simple and accessible.


FAQ

Q1: Why does analogRead give values from 0–1023?

Because Arduino Uno uses a 10-bit ADC.

Q2: Can I control LED brightness using this?

Yes, by mapping the value and using PWM output.

Q3: Is this project beginner friendly?

Yes, this is one of the first projects every Arduino learner should try.


Conclusion

The Potentiometer Analog Input using Arduino Uno in Wokwi is a fundamental electronics project that teaches analog signal reading and ADC concepts. It is ideal for beginners, school students, and robotics enthusiasts.

Mastering analog input is the first step toward advanced Arduino sensor projects.

Comments