A button says yes or no. A knob says how much. Today your light learns to whisper.
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.
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.
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.
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.
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.
Unplug the USB first. Push the potentiometer's three legs into three separate breadboard rows.
| From | To | Why |
|---|---|---|
| Potentiometer left leg | R3 5V | One end of the ramp: full power |
| Potentiometer right leg | R3 GND | Other end of the ramp: zero |
| Potentiometer middle leg | R3 pin A0 | The slider that rides the ramp |
| R3 pin 9 (has the ~ mark) | Resistor → LED long leg | A PWM pin, so it can dim |
| LED short leg | R3 GND | Loop complete |
// 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); }
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.
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.