Projects / PWM brightness and the eye

PWM brightness and the eye

A white LED fades up and down in equal PWM steps and then with a gamma-corrected table, and a button flips between the two so you can see why the eye needs a curve. A pot sets the fade speed.

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO13.7 mA

    Wire the LED

    D9 through 220 ohms to the long leg, short leg to GND.

  2. NANO

    Wire the pot

    Outer legs to 5V and GND, middle to A0.

  3. NANO

    Wire the button

    D2 to GND through the button.

  4. NANO13.7 mA

    Upload the sketch

    The LED fades up and down.

  5. NANO

    Turn the pot

    The fade gets faster or slower.

  6. NANO

    Press the button

    The curve changes between linear and gamma.

  7. NANO

    Compare the two in a dim room

    Gamma looks even, linear seems to jump at the bottom.

In short

How do you build a PWM brightness and the eye?

A white LED fades up and down in equal PWM steps and then with a gamma-corrected table, and a button flips between the two so you can see why the eye needs a curve. A pot sets the fade speed. You need Arduino Nano, LED 5 mm white, Resistor 220 Ω, Potentiometer 10 kΩ, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LED long legNano D9 through a 220 ohm resistorPWM output
LED short legNano GNDCommon ground
Pot outer legsNano 5V and GNDSpeed knob
Pot middle legNano A0Reads the knob
ButtonNano D2 and GNDFlips between linear and gamma

Code

PWM switches the LED on and off very fast, and the share of time it is on is the duty cycle. The Nano steps the duty number from 0 to 255 in equal steps and back. With the linear mode the output follows the number directly, but the eye does not judge brightness in equal steps, so the low end seems to leap and the top end seems to stall. In gamma mode every step goes through a table that raises the value to the power 2.2, so low steps are much smaller and the fade looks smooth. The table is built once when the board starts. Turn the pot to change the fade speed, and press the button to flip between the modes while the LED fades. Do not stare at a bright LED from very close, and use a diffuser such as a piece of white paper over it if it feels too sharp.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t LED_PIN = 9;       // PWM pin
const uint8_t BTN_PIN = 2;
const uint8_t POT_PIN = A0;

uint8_t gammaTable[256];
bool useGamma = false;
bool lastBtn = HIGH;
int level = 0;                   // 0 to 255, the number we step in equal amounts
int stepDir = 1;

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  for (int i = 0; i < 256; i++) {
    gammaTable[i] = (uint8_t)(pow(i / 255.0, 2.2) * 255.0 + 0.5);   // gamma 2.2 curve
  }
  Serial.begin(9600);
  Serial.println("Mode: linear");
}

void loop() {
  bool btn = digitalRead(BTN_PIN);
  if (btn == LOW && lastBtn == HIGH) {
    useGamma = !useGamma;
    Serial.println(useGamma ? "Mode: gamma" : "Mode: linear");
    delay(40);                   // simple debounce
  }
  lastBtn = btn;

  analogWrite(LED_PIN, useGamma ? gammaTable[level] : level);

  level += stepDir;
  if (level >= 255) { level = 255; stepDir = -1; }
  if (level <= 0)   { level = 0;   stepDir = 1;  }

  int speedMs = map(analogRead(POT_PIN), 0, 1023, 2, 30);   // 2 to 30 ms per step
  delay(speedMs);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Mode: linear

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Linear modeQuick rise, then little change at the top
Gamma modeSmooth even fade
Pot at one endSlow fade
Pot at the other endFast fade
Serial MonitorPrints the mode each time you press

If it does not work

LED does not fade

Make sure it is on D9 (a PWM pin) and the right way round.

Button does nothing

Check D2 and GND, and the debounce delay.

LED too dim

Use a smaller resistor, but never below 150 ohms.

Flicker on camera

The PWM is about 490 Hz, which a phone camera may show.

Fade is jumpy

Slow it down with the pot.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the PWM brightness and the eye?

Arduino Nano, LED 5 mm white, Resistor 220 Ω, Potentiometer 10 kΩ, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the PWM brightness and the eye take to build?

About 60 minutes for a beginner maker, in 7 steps.

How do I know the PWM brightness and the eye works?

Linear mode: Quick rise, then little change at the top. Gamma mode: Smooth even fade. Pot at one end: Slow fade.

LED does not fade: what should I check?

Make sure it is on D9 (a PWM pin) and the right way round.

Button does nothing: what should I check?

Check D2 and GND, and the debounce delay.

LED too dim: what should I check?

Use a smaller resistor, but never below 150 ohms.

Flicker on camera: what should I check?

The PWM is about 490 Hz, which a phone camera may show.