TinkerBlock UNO kit/Colour/10. Lab: the first colours
Colour · 10 of 21

Lab: the first colours

The TK33 bar on D5 needs a library to talk to it: Adafruit NeoPixel, from the Library Manager. Then five lines light it red, green, blue, white and a rainbow. At the sketch's brightness of 40 the bar draws about 30 mA, and even at full white about 182 mA, well inside the UNO's 0.5 A USB fuse.

The TK33 bar is five LEDs with a driver inside each, fed colours down one wire as a stream of precisely timed pulses. Writing those pulses by hand is a job for a library, so this is the kit's first: what a library is, and how to install one.

Where it plugs in

ModuleSocketPin
TK33 WS2812 LED barD4/D5D5

The bar takes its supply from the socket's VCC, the UNO's 5 V, which is what its LEDs want.

The library

Open Sketch > Include Library > Manage Libraries, search for Adafruit NeoPixel, and install the one by Adafruit. The sketch was compiled against version 1.15.5, the same library the TK33's own book uses. If the Library Manager offers to install dependencies, this library has none.

Then the sketch starts with one line that names the library's header:

#include <Adafruit_NeoPixel.h>

The sketch

The strip is created once, with the pin, the number of LEDs, and the order the LEDs read their colours in:

Adafruit_NeoPixel bar(LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);

NEO_GRB is not a typo: these LEDs take green first, then red, then blue, as the TK33 book's colour order article shows. Told RGB, the bar shows green where you asked for red.

After that, every change happens in two steps. fill() or setPixelColor() writes colours into the library's copy of the bar, in the UNO's memory, and nothing on the bar changes. show() sends the whole copy down the wire, and all five change at once. Forgetting show() is the most common reason a colour never appears.

The rainbow uses the library's rainbow(), which spreads the colour wheel along the bar; moving its first hue a little every 20 ms turns it.

What it draws

What the bar draws
30 mA
setBrightness40 of 255
Bar current
30 mA
Of the USB fuse
6 %
Full white, 255
182 mA
30 mA: bright enough to read across a desk and a small fraction of either fuse. This is why the sketch says 40.

There is no resistor to choose on this bar. Each colour in each LED runs at a constant 12 mA at full, so the current is a count, and setBrightness() scales it down. At the sketch's 40 of 255, all five white draw about 30 mA. At 255 they draw about 182 mA: the figure in the TK33 book's current article, arithmetic on the LED's data sheet rather than a measurement.

Two fuses sit between a computer's USB port and the bar. The UNO's own, F2, holds 0.5 A. The shield's F1, between the UNO's 5 V and every socket, holds about 1 A. On USB the UNO's is the smaller of the two and comes first, so it is the one that matters, and the bar at full white uses about a third of it. The UNO itself and the other modules draw from the same supply, so the bar is never the whole story, but in this kit it is the biggest load.

Make it yours

  • Light one LED at a time: bar.clear(), then bar.setPixelColor(i, 0xFF8000) and bar.show(), with i running from 0 (the top LED) to 4.
  • Try a colour by its numbers: bar.Color(255, 80, 0) is an orange, red 255, green 80, blue 0.
  • Leave BRIGHTNESS alone and pass a lower value to rainbow() instead: its fourth argument is the brightness of the colours themselves.

The code

First_Colours.ino

Red, green, blue and white on all five LEDs for a second each, then a rainbow that turns round the bar for five seconds, and again. The Serial Monitor names what the bar should be showing.

/*
  The first colours                   TinkerBlock UNO kit, /manuals/tkbu

  The TK33 WS2812 LED bar shows red, green, blue and white for a
  second each, then a rainbow that turns for five seconds, and starts
  again. The Serial Monitor names what the bar should be showing.

  Wiring, on the TinkerBlock UNO shield:
    TK33 WS2812 LED bar    in the D4/D5 socket, so it is on D5
  A module is on the second pin printed beside its socket.

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port
    Library: Adafruit NeoPixel by Adafruit, 1.15.5, from
             Sketch > Include Library > Manage Libraries.
    Serial Monitor at 115200.
*/

#include <Adafruit_NeoPixel.h>

const int DATA_PIN = 5;    // TK33 DATA
const int LEDS = 5;        // five on the bar, and nothing after them

// Out of 255. At 40, all five white draw about 30 mA; at 255,
// about 182 mA (the TK33's book, "What five LEDs draw").
const int BRIGHTNESS = 40;

// NEO_GRB: these LEDs read green first. NEO_KHZ800: 800 kbit/s.
Adafruit_NeoPixel bar(LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);

const uint32_t COLOUR[] = { 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF };
const char *NAME[] = { "red", "green", "blue", "white" };

void setup() {
  Serial.begin(115200);
  bar.begin();
  bar.setBrightness(BRIGHTNESS);
  bar.show();               // a dark frame clears power-up junk
}

void loop() {
  for (int c = 0; c < 4; c++) {
    bar.fill(COLOUR[c]);    // every LED the same colour
    bar.show();             // nothing changes until show()
    Serial.print("all five ");
    Serial.println(NAME[c]);
    delay(1000);
  }

  Serial.println("rainbow");
  unsigned long start = millis();
  while (millis() - start < 5000) {
    // The hue of LED1 moves round the colour wheel; the rest follow.
    // 65536 is once round the wheel.
    uint16_t hue = (millis() - start) * 26;
    bar.rainbow(hue, 1, 255, 255, true);
    bar.show();
    delay(20);
  }
}

If the monitor says red and the bar shows green, the colour order is wrong: the strip must be created with NEO_GRB. BRIGHTNESS is 40 of 255; raise it to see the difference, up to 255.

View on GitHub · kits/tkbu/first-colours/First_Colours/First_Colours.ino @ v1.14

When it does not work

The upload fails with Adafruit_NeoPixel.h: No such file or directory.

The library is not installed, or it went into a different Arduino IDE. Open Sketch > Include Library > Manage Libraries, search for Adafruit NeoPixel, install it, and upload again. The library's name in the list is Adafruit NeoPixel, by Adafruit.

Nothing lights, and the monitor runs.

The TK33 must be in the D4/D5 socket, so it is on D5, and DATA_PIN must say 5. In the D2/D3 socket the bar would be on D3, and the sketch would be talking to an empty pin. Check the cable is in fully at both ends.

The monitor says red and the bar shows green.

The strip was created with NEO_RGB. These LEDs read green first, so the line must say NEO_GRB + NEO_KHZ800. Blue and white look right either way, which is why red is the colour to check.

The colours are too dim in daylight.

BRIGHTNESS is 40 of 255 to keep the bar gentle on the eyes and on the supply. Raise it. All five at full white draw about 182 mA, which the UNO's USB supply carries without trouble.

Where this goes next

A pattern written as a table of frames, played without delay().

Lab: police lights →

Edit this page — content/books/tkbu/first-colours.mdx

Community

Questions about this product

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

Ask a question ↗

TinkerBlock UNO R3 Starter Kit, 12 Modules

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 →