Tutorial 19 – Using a Potentiometer for Analog Input

Welcome back to CraftedTech Engineering's Arduino tutorial series! In this tutorial, we’ll explore how to use a potentiometer to provide analog input to your Arduino. Potentiometers are great for controlling variables, such as the brightness of an LED or the speed of a motor, making them a fundamental part of many projects.


🔌 What You’ll Need

  • 1 Arduino Board (e.g., Arduino UNO)
  • 1 Potentiometer (10kΩ is typical)
  • 1 LED (any color)
  • 1 220Ω Resistor
  • Breadboard
  • Jumper wires
  • USB cable for Arduino

You can order the required components here:


🧠 What Is a Potentiometer?

A potentiometer is a three-terminal variable resistor that allows you to adjust its resistance by turning a knob or slider. This change in resistance can be read by your Arduino as an analog value (ranging from 0 to 1023), making it useful for controlling analog outputs like LED brightness or motor speed.


🧾 Step-by-Step Instructions

🔧 Step 1: Wiring the Potentiometer and LED

  • Connect one outer leg of the potentiometer to 5V on the Arduino.
  • Connect the other outer leg of the potentiometer to GND (ground).
  • Connect the middle leg (wiper) of the potentiometer to analog pin A0 on the Arduino.
  • For the LED:
    1. Connect the long leg (anode) of the LED to digital pin 9 on the Arduino.
    2. Connect the short leg (cathode) of the LED to one end of the 220Ω resistor.
    3. Connect the other end of the resistor to GND.

💻 Step 2: Arduino Code to Read Potentiometer and Control LED

int potPin = A0; // Pin connected to the potentiometer int ledPin = 9; // Pin connected to the LED int potValue = 0; // Variable to store potentiometer value void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output Serial.begin(9600); // Start serial communication } void loop() { potValue = analogRead(potPin); // Read potentiometer value int ledBrightness = map(potValue, 0, 1023, 0, 255); // Map potentiometer value to 0-255 analogWrite(ledPin, ledBrightness); // Control LED brightness Serial.println(potValue); // Output potentiometer value to Serial Monitor delay(10); // Small delay for stability }

🔍 Code Breakdown

  • analogRead(): Reads the analog value from the potentiometer (from 0 to 1023).
  • map(): Maps the potentiometer value (0-1023) to a range of 0-255 (for LED brightness control).
  • analogWrite(): Adjusts the LED brightness based on the mapped potentiometer value.
  • Serial.println(): Outputs the potentiometer value to the Serial Monitor for debugging.

🧪 Try This!

  • Change the LED's behavior by controlling its brightness with the potentiometer.
  • Try using the potentiometer to control other outputs, such as a motor or the speed of a fan.
  • Experiment with mapping different ranges of the potentiometer value to control various aspects of your project.

🛠️ Troubleshooting Tips

  • Make sure the potentiometer’s middle leg (wiper) is connected to the correct analog input pin.
  • If the LED isn't responding, check the wiring of the LED and resistor.
  • Ensure the potentiometer is working by printing its value to the Serial Monitor.

🧭 What You Learned in This Tutorial

✔️ How to use a potentiometer to provide analog input to Arduino
✔️ How to map analog values for controlling outputs like LED brightness
✔️ How to use analogWrite() for PWM control of outputs
✔️ Basic interaction with analog inputs in Arduino projects


📌 Next Up: Tutorial 20 – Controlling a Servo Motor with Arduino

In the next tutorial, we’ll use the potentiometer to control the position of a servo motor, demonstrating another useful application of analog input in Arduino projects.


📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!

🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering

Back to blog

Leave a comment