GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Beginner Kit · Lesson 04 of 12

The Dimmer

A button says yes or no. A knob says how much. Today your light learns to whisper.

Unlock your lessons

Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.

Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.

LICENSED TO , PERSONAL USE · © GAMERIFT

BRIEF The volume knob's secret

Think about a volume knob. It is not on or off, it lives everywhere in between. Today you wire the kit's twisty knob (its proper name is a potentiometer) and use it to dim an LED smoothly from off to full blaze, like the lights in a cinema before the movie. On the way you learn the difference between the two languages electronics speaks: digital and analog.

Time: 45–60 minutes · Difficulty: 2/5 · You need: Lesson 03 done.

KIT What you need

Concept · Digital vs analog

Digital is yes or no, HIGH or LOW, 1 or 0, the button's whole vocabulary. Analog is a smooth range, like a ramp instead of a step. Your board's A0 to A5 pins are special ears that measure analog voltage and report it as a number from 0 to 1023. Twist the knob, the number glides.

Concept · PWM, the great dimming trick

Here is a secret: your board cannot actually make a pin half-on. Instead it flicks the pin on and off hundreds of times a second, and your eye averages the flicker into a brightness. On 30% of the time looks 30% bright. This trick is called PWM, and the pins that can do it have a ~ mark next to the number (3, 5, 6, 9, 10, 11). The command is analogWrite(pin, 0 to 255). Dimmed lights, robot motor speeds, laptop screen brightness: all PWM.

Concept · Two different rulers

Careful: analogRead gives 0 to 1023, but analogWrite wants 0 to 255. Different rulers. You must translate between them, either divide by 4, or use the friendly map() function that re-scales any range to any other. Forgetting this is the classic bug of this lesson.

BUILD Wire it

Unplug the USB first. Push the potentiometer's three legs into three separate breadboard rows.

FromToWhy
Potentiometer left legR3 5VOne end of the ramp: full power
Potentiometer right legR3 GNDOther end of the ramp: zero
Potentiometer middle legR3 pin A0The slider that rides the ramp
R3 pin 9 (has the ~ mark)Resistor → LED long legA PWM pin, so it can dim
LED short legR3 GNDLoop complete

CODE Program it

// GAMERIFT Beginner Kit, Lesson 04: The Dimmer
// Read a knob, dim a light. Analog in, PWM out.

const int knobPin  = A0;
const int lightPin = 9;   // must be a ~ pin for PWM

void setup() {
  pinMode(lightPin, OUTPUT);
  Serial.begin(9600);        // open a chat window to the computer
}

void loop() {
  int knob = analogRead(knobPin);              // 0 to 1023
  int bright = map(knob, 0, 1023, 0, 255);   // translate rulers

  analogWrite(lightPin, bright);               // the PWM trick

  Serial.print("knob: ");                      // spy on the numbers
  Serial.println(knob);
  delay(50);
}
  1. Upload, then open Tools → Serial Monitor (set it to 9600 baud, bottom right).
  2. Twist the knob slowly. Watch the number glide from 0 to 1023 while the LED breathes brighter and dimmer under your fingertips.
  3. The Serial Monitor is your new best friend. When a circuit misbehaves, printing the numbers is how you see what the board sees.

BREAK IT Now break it on purpose

  1. Swap the potentiometer's outer legs (5V and GND). Nothing breaks, but the knob now works backwards. Every dimmer installer has done this at least once.
  2. Move the LED wire from pin 9 to pin 12 (no ~ mark) and change the code to match. The smooth dim becomes a crude on/off around half way. Now you know why the ~ pins matter.
  3. Pull the middle leg wire out. A0 is now floating, watch the serial numbers wander on their own, haunted by static. Same ghost you met in Lesson 03.

REMIX Make it yours

LOG IT Show and tell

Film 15 seconds of your remix and save it for your lesson log. Stuck? Send a photo of your wiring via contact us, a real person will spot it.

Next · Lesson 05: Rainbow Maker

You can dim one colour. Next lesson you get the kit's fanciest LED, one bulb with red, green and blue inside, and mix any colour you can imagine. Millions of them.

All lessons