dual bright LEDs/Driving it/06. The first light
Driving it · 06 of 9

The first light

Four wires, no library, and a sketch that shows the warm white, the cool white, the two together and then off, two seconds each. VCC goes to 5V (VBUS on a Pico); 3000K and 6500K to GPIO 4 and 5 on an ESP32-S3, 25 and 26 on an ESP32, D9 and D10 on an Uno, GP14 and GP15 on a Pico.

Four wires

Four wires
Your board
VCC to
5V
3000K to
GPIO 4
6500K to
GPIO 5
GND to GND, VCC to 5V, 3000K to GPIO 4 and 6500K to GPIO 5. The ESP32-S3's pins run at 3.3 V and VCC can still be 5 V: it only feeds the LEDs and never reaches a pin. Neither is a strapping pin, which matters because each colour pin holds its GPIO under a volt through the transistor's base.

GND to GND. VCC to 5V, or VBUS on a Pico: it only feeds the LEDs, so 5 V is safe on a 3.3 V board, and it comes from USB rather than from your board's 3.3 V regulator. The two colours to two PWM pins. On an ESP32 or a Pico every GPIO can do PWM; on an Uno only six can.

None of the pins is a strapping pin. That matters in an unusual direction here: each colour pin sits on 1 kΩ and a transistor's base, which holds the pin under a volt whenever something tries to pull it up.

The sketch

pinMode and digitalWrite(pin, LOW) for both pins first, because nothing on the board holds the transistors off. After that the sketch only uses analogWrite: on the ESP32's Arduino core 3.x a digitalWrite after an analogWrite on the same pin is ignored, so mixing the two is a trap.

MAX_DUTY is 170 of 255, the cap for VCC on 5V from five volts is the limit. With VCC on 3V3 you can raise it to 255.

What you should see

At 115200 the serial monitor prints warm: 3000K, cool: 6500K, both, half each: about 4100 K and off, two seconds apart. The upper LED lights yellowish white, then the lower one bluish white, then both, and the light on the wall is a white between the two.

Do not look into them from close up. The back of the board says it in capitals, and it means it.

The code

No library. analogWrite sets each colour's duty: warm alone, cool alone, half of each, then off, two seconds apart, with a line to the serial monitor each time. Change WARM_PIN and COOL_PIN to the pins you wired.

dual_leds_first_light.ino
/*
  Dual Bright LEDs - the first light                     TK95 / /p/tk95

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V (VBUS on a Pico). It only feeds the LEDs, so 5V is
              safe on a 3.3 V board. Never the Uno's 3.3V pin.
    3000K  -> D9, GPIO 25, GPIO 4 or GP14   (a PWM pin)
    6500K  -> D10, GPIO 26, GPIO 5 or GP15  (a PWM pin)

  Uno, ESP32, ESP32-S3, Pico, in that order.

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.
    Serial Monitor                115200

  Do not look into the LEDs from close up.
*/

// The pins 3000K and 6500K are wired to.
// Uno: 9. ESP32: 25. ESP32-S3: 4. Pico: 14.
const int WARM_PIN = 4;
// Uno: 10. ESP32: 26. ESP32-S3: 5. Pico: 15.
const int COOL_PIN = 5;

// With VCC on 5V an LED held fully on runs past its rating, so the
// sketch stops at two thirds. With VCC on 3V3, 255 is fine.
const int MAX_DUTY = 170;

void show(int warm, int cool, const char *what) {
  analogWrite(WARM_PIN, warm);
  analogWrite(COOL_PIN, cool);
  Serial.println(what);
  delay(2000);
}

void setup() {
  // LOW first: nothing on the board holds the transistors off.
  pinMode(WARM_PIN, OUTPUT);
  digitalWrite(WARM_PIN, LOW);
  pinMode(COOL_PIN, OUTPUT);
  digitalWrite(COOL_PIN, LOW);
  Serial.begin(115200);
}

void loop() {
  show(MAX_DUTY, 0, "warm: 3000K");
  show(0, MAX_DUTY, "cool: 6500K");
  show(MAX_DUTY / 2, MAX_DUTY / 2, "both, half each: about 4100 K");
  show(0, 0, "off");
}

setup() drives both pins LOW before anything else, because nothing on the board holds the transistors off. MAX_DUTY is 170, the cap for VCC on 5V. The sketch compiles for an ESP32-S3, an ESP32 and an Uno.

View on GitHub · blocks/tk95-dual-bright-leds/arduino/dual_leds_first_light/dual_leds_first_light.ino @ v1.8

When it does not work

Neither LED lights.

Check VCC first: the pins only switch the transistors, and with VCC unwired there is nothing to switch. Then GND, then that the sketch's WARM_PIN and COOL_PIN are the GPIO numbers you wired, counting from the square pad.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port brings up no serial port at boot, so the sketch runs, and lights the LEDs, with nowhere to print.

It is too bright to look at.

That is the block working. Point it at a wall or the desk and look at what it lights. For a dimmer first light, lower MAX_DUTY in the sketch; 20 is plenty to see it is alive.

The Uno lights one colour fully and never dims it.

That pin has no PWM. On an Uno only D3, D5, D6, D9, D10 and D11 can dim; the book uses D9 and D10. An older lesson put the warm LED on D2, which only ever switches fully on or off.

Where this goes next

What a duty cycle does to the current, and a fade that looks even.

Dimming with PWM →

Edit this page — content/books/dual-bright-leds/the-first-light.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Dual Bright LEDs

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →