0Pricing
Arduino & IoT Academy · Lesson

analogWrite 0–255

Set duty cycle to control LED brightness.

analogWrite 0–255 is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 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.

The Command That Does PWM

To produce PWM you call one function: analogWrite. You give it a pin and a level, and the chip handles the fast pulsing for you.

analogWrite(9, 200);

The Value Range Is 0 to 255

The second argument goes from 0 to 255. That is 256 steps, because one byte holds exactly that many values.

0 Means Fully Off

Pass 0 and the duty cycle is zero. The pin stays LOW the whole time, so the LED is completely dark.

analogWrite(9, 0); // LED off

255 Means Fully On

Pass 255 and the duty cycle is 100%. The pin behaves just like digitalWrite HIGH, so the LED is at full brightness.

analogWrite(9, 255); // LED full

The Middle Gives You Dimming

A value of 128 is roughly half power, so the LED looks about half as bright. Values in between give you a smooth scale.

analogWrite(9, 128); // about half

Bigger Number, Brighter Light

The relationship is simple: a higher value means more on-time and more brightness. Lower values dim the LED down.

Pick a PWM-Capable Pin

analogWrite only does true PWM on the tilde-marked pins. On a non-PWM pin it just snaps fully on or off at the halfway point.

Set the Pin as OUTPUT First

Just like a normal LED, declare the pin as OUTPUT in setup so it can drive current before you call analogWrite.

pinMode(9, OUTPUT);

A Tiny Working Sketch

Here is the whole idea: in setup mark the pin OUTPUT, then in loop hold it at a steady mid-brightness level.

void setup() {
  pinMode(9, OUTPUT);
}
void loop() {
  analogWrite(9, 100);
}

Brightness Is Not Perfectly Linear

Doubling the value does not exactly double how bright it looks, because human eyes sense light on a curve, not a straight line.

It Works on Motors Too

analogWrite is not only for LEDs. The same 0 to 255 value can set a motor's speed through a driver. ⚙️

Quick Check

Make sure the value range is crystal clear.

Recap: analogWrite Levels

analogWrite takes a pin and a value from 0 to 255: 0 is off, 255 is full, and the middle dims smoothly on PWM pins. 🌟

Frequently asked questions

Is the “analogWrite 0–255” lesson free?

Yes — the full text of “analogWrite 0–255” 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 “analogWrite 0–255”?

Set duty cycle to control LED brightness. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “analogWrite 0–255” 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