0Pricing
Arduino & IoT Academy · Lesson

Knob Controls LED Brightness

Combine a pot reading with PWM output.

Knob Controls LED Brightness is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Let a Knob Do the Driving

Now you combine two skills: read a potentiometer and use that reading to set an LED's brightness in real time. 🎛️

The Knob Speaks 0 to 1023

analogRead gives you a 0 to 1023 value as you turn the pot. That is the position of the knob expressed as a number.

int knob = analogRead(A0);

The LED Wants 0 to 255

But analogWrite only accepts 0 to 255. The two ranges do not match, so you cannot feed the knob value straight in.

map() Bridges the Gap

The map function rescales a number from one range to another. It turns the wide knob range into the narrow LED range.

int led = map(knob, 0, 1023, 0, 255);

Send It to the LED

Pass the mapped value to analogWrite. Now the LED brightness tracks exactly where you set the knob.

analogWrite(9, led);

Wire the Potentiometer

Connect the pot's outer legs to 5V and GND, and its middle wiper leg to analog pin A0 so the board can read it.

Pin Setup in setup()

The LED pin still needs OUTPUT. Analog input pins do not need pinMode, so setup stays short and simple.

void setup() {
  pinMode(9, OUTPUT);
}

The Full loop()

Each pass, read the knob, map it, and write it. Because loop repeats fast, the LED feels instant as you turn the dial.

void loop() {
  int knob = analogRead(A0);
  int led = map(knob, 0, 1023, 0, 255);
  analogWrite(9, led);
}

Skip map With a Shortcut

You could also divide the knob value by 4, since 1023 over 4 is about 255. It works, but map reads more clearly.

int led = knob / 4;

Watch the Direction

If the LED dims when you turn up, just swap map's output ends to 255 then 0, or flip the pot's outer wires.

You Just Built an Interface

This read, map, write pattern is everywhere in IoT: a sensor in, a useful range out, an action on the hardware. 🚀

Quick Check

Recall why map matters in this project.

Recap: Knob to Brightness

Read the knob with analogRead, map 0 to 1023 into 0 to 255, then analogWrite it. You built a real dimmer. 🎉

Frequently asked questions

Is the “Knob Controls LED Brightness” lesson free?

Yes — the full text of “Knob Controls LED Brightness” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Knob Controls LED Brightness”?

Combine a pot reading with PWM output. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Knob Controls LED Brightness” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What PWM Actually Does
  2. analogWrite 0–255
  3. Fade an LED Up and Down
  4. Knob Controls LED Brightness
← Back to Arduino & IoT Academy