Breadboard
docs
Example project · Microcontrollers

Analog dimmer (pot to PWM)

Turn the trim pot and the LED follows: the ATmega328P reads the wiper with its real 10-bit ADC and drives the LED on D9 with genuine Timer1 hardware PWM, printing the raw reading over serial. Click the pot and drag its Position slider in the Inspector to dim the light. The Code tab holds the same dimmer as an editable Arduino sketch.

The Analog dimmer (pot to PWM) circuit as rendered by the simulator

What's on the bench

  • Arduino Uno
  • Battery
  • LED
  • Resistor
  • Trim Pot

The code

This Arduino C++ sketch lives in the Code tab; sign in and press Compile & upload to build real firmware for the emulated board.

// The same dimmer as an Arduino sketch. Edit it, then Compile & upload
// (sign-in needed): the server builds real AVR firmware and flashes it.
const int POT = A0;
const int LED = 9;  // Timer1 PWM pin

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int v = analogRead(POT);   // 0..1023 from the real ADC
  analogWrite(LED, v / 4);   // 0..255 duty on D9
  Serial.print("A0 ");
  Serial.println(v);
  delay(200);
}