03 / Templates · beginner
POTENTIOMETER
Open in PlaygroundRead an analog value from a potentiometer and use it to fade an LED's brightness via PWM, while printing the raw and mapped values to the Serial Monitor.
beginneranalog inputPWMserialpotentiometer
sketch.inoArduino C++
25 lines
// Potentiometer — Analog Read → LED Brightness
// Turn the knob on the canvas and watch the LED fade + the value print to Serial.
const int POT_PIN = A0;
const int LED_PIN = 9; // PWM-capable pin
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Potentiometer ready — turn the knob!");
}
void loop() {
int raw = analogRead(POT_PIN); // 0–1023
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
Serial.print("Raw: ");
Serial.print(raw);
Serial.print(" Brightness: ");
Serial.println(brightness);
delay(100);
}- Arduino Uno01
- Potentiometer02
- LED (yellow)03
- Resistor 220Ω04
| Component Pin | Arduino Pin |
|---|---|
| Potentiometer VCC | 5V |
| Potentiometer GND | GND |
| Potentiometer Signal | A0 |
| Resistor Pin 1 | Pin 9 |
| LED Anode (+) | Resistor Pin 2 |
| LED Cathode (−) | GND |