Fun Programming Project for Kids: The Halloween LED Pumpkin
By: Aurélien Esprit
Tags: Halloween, Led Lighting, Arduino, Pumpkin, Esp8266

In this 2020 special context, we had to come up with new ideas to keep the Halloween a fun moment for kids. I spent more time on the pumpkin Carving, and the kids and I decided it would be the most geeky pumpkin in the neighborhood :) It would be a LED illuminated pumpkin, cycling through a set of programmed colors, fading from one into another. In this short article, I’ll share the basics to do this with your kids. The video is available at the end of this article.

The hardware for the project:

We will use a LED strip, an ESP8266 chip (Arduino like), a couple of wires, and a DC power. Ideally, the wires should be soldered, but pin wires would be fine too. Ohhh. A pumpkin too :)

Of course, we also need a computer with Arduino IDE installed, a standard USB to micro USB cable, and the ESP8266 drivers (explained below).

The learning along this project:

Basically, three primary colors are necessary to render the wide range of natural colors. That’s the theory. We will prove it with the LED strip in a fun way

Arduino basic programming, including the use of library to drive a LED strip, the principle of loops and delays.

Even though is it accepted that LEDs are low energy, we still need to do the math to make sure we have an appropriate DC power.

Duration:

It all depends on how much you want to explore the various themes with your kids (and their age): 1-3 hours for the programming part. For the pumpkin carving part, that’s up to you again.

Discovering the hardware

The LED strip: link (Canada), link (USA)

5050 RGB LED Strip, 5 meters, 300 LEDs

There is a huge variety of LED strip, combination of LED technology, number of led per meters, Ingress Protection Code (IP65…), power etc. For this project, we used this LED strip: WS2812B Individually Addressable LED Strip Light 5050 RGB, 5 meters (16.4 ft), 300 LED. Each ‘LED’ is actually composed of 3 single LEDS, one Red, one Green, one Blue, and one chipset. Similarly to a painter using the brush to mix those three primary colors, the chip is in charge of doing the mix. While the painter is playing with the proportions to find the expected color, the chip is only taking as input a set of three values: Red, Green, Blue = (R,G,B) and will set the brightness of the each individual light to the received value. These values will be sent by the ESP8266 chip, depending on what the programmer coded. The result is stunning and the eye is totally tricked. We will experiment in the next chapter.

The ESP8266 chipset: link (Canada), link (USA)

This chip is my favorite little piece of hardware ever :) It is equipped with a lot of GPIOs, and the HUGE advantage compared to the various Arduinos, has an wifi chip Embedded (This open some huge possibilities when building some IOT devices based on ESP8266) In a previous article, I explain how to create connected devices for home automation based on this chipset. Finally, it is easily powered via an standard USB cable.

GPIO, to simplify, are some pins allowing to either receive some signal from the outside word through a sensor (temperature, brightness, sound level, contactor, etc…), or to send some signal to another device (Here, our LED strip).

The DC Power: link (Canada), link (USA)

This LED strip uses a 5V power. Some other strips use 12V. The choice of a 5V model is not random. The ESP8266 also requires 5V input power, the idea here is to power both ESP8266 and the LED strip with a single source (We will see that both need to share a same ground to work correctly, so using a common source ground also makes this easier).

Remember that each ‘LED’ of the strip is actually a set of three LEDs. Each of these three LEDs are consuming about 50mW each at full brightness. (This varies from one model of LED to another, check the technical datasheet)

Power(Watts) = Voltage(Volts) x Intensity(Amperes).

Under a 5V voltage, a single color LED will draw 10mA . That’s a total of 30mA per strip ‘LED’. The strip has 300 ‘LEDS’, so it could consomme up to 300 x 0.03A = 9A. 10A, 5V DC power will be able to cover for this.

Note that this DC power comes with a domino that ease the connection of the 2 items we want to power:

Curious kids section: Assuming each ‘LED’ (set of 3 individual LEDs) consume 50mW, what power would you need to power a 1 meters strip, 30 LEDs / meter, with a 12 Volts power? Considering a 20% safety margin, what is the safe amperage for the power? What is the corresponding Watt power?

Connecting the power, ESP8266 and the LED Strip together

The DC power delivers +5V on one on the domino’s connector (marked as +), and 0V on the other one (marked as -).

The ESP8266 can be powered through micro USB connector or directly through the VV pin and the ground pin. You can either cut a standard USB cable an connect the red wire to the + connector and the red cable to the — connector, or you can use some pin wires as shown in below picture:

To power the strip, you will need some pin wires, or to solder some basic wires. Finally, to drive the strip from the ESP8266, you will need to have a pin wire from ESP8266 D1 pin to the ‘data’ pin of the LED strip.

On the domino connector:

connect the ‘+’ to the red (+5V) wire of the LED Strip.

Connect the ‘+’ to the ‘VV’ pin of the ESP8266

Connect the ‘-’ to the white (marked ‘-’) wire of the LED strip.

Connect the ‘-’ to any ground pin of the ESP8266 (marked ‘G’)

Finally, connect the ‘D1’ to the green wire (market ‘data’) of the LED Strip

Alternatively, you can power the ESP8266 from another source of power, but then you need to connect a ground ‘G’ pin of the ESP8266 to a ‘-’ connector of the DC power. Both the ESP and the LED strip need to share a common ground (same 0 reference). Otherwise, the LED strip will behave strangely.

Note: While experimenting with the code, you can just power the LED strip through the ESP8266 (ESP VV pin to red wire of the LED strip, ESP ground pin to the white (-) wire of the LED strip). In that case, you will need to limit the number of LEDs used in the code to 20, as the ESP8266 should not deliver more than 1A (we will see the details in the next chapter).

The coding part

Prerequisites:

This section will assume you have Arduino IDE installed on you computer

ESP8266 Node MCU 1.0 board config added to the IDE (apply the instructions from this web site).

The adafruit library added to your IDE (folow ‘first method’ from this web site)

Structure of a standard code

The structure of an Arduino code is standardized. It starts with a declaration section where we will declare variables, constants, import libraries. It is then followed by a setup section, that is run once at the chipset startup, that can initiate a state. We then have a ‘loop’ section, that will play again and again the code snippet contained in this section. And finally, an optional functions section, where we can define various function for code clarity sake. Let’s explore this various sections content using a basic example.

Building A first basic code to light 20 LEDs with a given color

The declaration section will look like this:

#include <Adafruit_NeoPixel.h> // ESP Pin driving the LED strip

#define PIN D1 // Total number of LEDs to control (Note that you need about 30mA per LED)

#define NUMPIXELS 20 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); // Declare variables r, g, and b as integers that will store each primary color component

int r; int g; int b; // Declare a colorId, set it to 0

int colorId = 0;

Import the adafruit library. The library contains a set of functions to control the LED strip. Importing the library will import these functions (and therefore, they will be known and can be called in the code).

#include <Adafruit_NeoPixel.h>

Declare the PIN that controls the LED strip. This has to match the GPIO pin you will use on the ESP8266. Note that the format is Dx (x is a number).

// ESP Pin driving the LED strip #define PIN D1

Declare The number of LED we want to use for the project. Let set 20 for now, so we can power the LED strip directly from the ESP8266.

// Total number of LEDs to control (Note that you need about 30mA per LED) #define NUMPIXELS 300

Declare the ‘strip’

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);



Declare the red, green and blue integers, that all together define a color. Note that we do not need to set a value now.

// Declare variables r, g, and b as integers that will store each primary color component

int r; int g; int b;

Finally, declare a colorId:

// Declare a colorId, set it to 0

int colorId = 0;

The setup function will look like this:

void setup()

{

strip.begin();

strip.setBrightness(255);

strip.clear();

strip.show(); // Initialize all pixels to ‘off’

}

We first initiate the strip, with strip.begin(). We call the setBrightness() function to set the brightness of the LED strips to maximum value (this is a generic value for the entire strip, and we will see that we can adjust each LED brightness individually). Then we call the clear function to turn off the LED strips, and finally, by calling the show function, we actually apply the new LEDs’ settings.

The loop:

void loop(void)

{

switch (colorId){

case 0:

r = 255; g = 0; b = 0;

break; case 1:

r = 4; g = 255; b = 0;

break; case 2:

r = 0; g = 222; b = 255;

break; case 3:

r = 255; g = 107; b = 0;

break; case 4: r = 255; g = 0; b = 235;

break; case 5:

r = 255; g = 230; b = 0;

break;

} pumpkinStrip(r, g, b, NUMPIXELS, 2000, 10, 500, 1000);

colorId++; if (colorId > 5){colorId = 0;};

}

The void loop(void) section in run forever by the chipset.

The switch function takes as argument our colorId we declared previously to 0, an apply the ‘case’ logic matching the actual value of the colorId variable. So it will start with case 0; This will set the value or r, g and b variables. The break will exit the switch without reading the following lines.

switch (colorId){

case 0:

r = 255; g = 0; b = 0;

break;

The loop will then call the pumpkinStrip function with 8 arguments.

pumpkinStrip(r, g, b, NUMPIXELS, 2000, 10, 500, 1000);

This is the magic function that will actually drive the LED strip. This function will be declared bellow the loop section.

The loop will then increment the colorId value, setting it to 1.

colorId++;

Finally, the loop will check whether colorId is now greater than 5 (after 5 loops are completed, this will happen…), and if it is the case, reset the value to 0, to reset the sequence.

The final section of code: functions declaration

void pumpkinStrip(int red, int green, int blue, int numberOfLEDs, int fullBrightnessDuration, int fadeSteps, int fadeDelay, int coolDown)

{

for (int i = 0; i < numberOfLEDs; i++)

{

// set the color of LED number i to (red, green, blue). These variables are passed to the function as argument

strip.setPixelColor(i, red, green, blue);

} // Actually show the LEDs with their new setting once all of them received their setting

strip.show(); // Keep the LEDs on for fullBrightnessDuration ms

delay(fullBrightnessDuration); // Fade color to brightness null

float nextRed, nextGreen, nextBlue;

for (int j = fadeSteps; j >= 0; j--)

{

nextRed = j * (red / fadeSteps);

nextGreen = j * (green / fadeSteps);

nextBlue = j * (blue / fadeSteps); for (int i = 0; i < numberOfLEDs; i++)

{

strip.setPixelColor(i, nextRed, nextGreen, nextBlue);

} //Show the LEDs with their new color (decreased brightness)

strip.show(); // Wait fadeDelay ms before decreasing again the brightness

delay(fadeDelay);

} // Let coolDown ms for the LEDs to cool down before a new cycle.

delay(coolDown);

}

This function relies on the setPixelColor function:

strip.setPixelColor(i, red, green, blue)

This function will set the LED at position ‘i’ to (red,green,blue) color. each of these 3 integer variables can range between 0 (the light is off) and 255 (the light has maximum brightness).

Curious kids section: The strip can be cut at any position (very useful to do multiple projects requiring only small length of strip). If you cut the strip between LED 9 and LED 10, and want to light the second LED of the second strip (original position 11), you will use strip.setPixelColor(1, R, G, B) and not strip.setPixelColor(11, R, G ,B). Try to figure out how the LED are controlled.

Curious kids section: Experimenting with primary colors. What color correspond to (255,0,0) ? (0,255,0)? (0,0,255)? (255,255,255)?

Write a code snippet that turn on all the LEDs to (255,255,255). Now change the value to (5,5,5). Observe the LEDs. What can you notice?

Curious kids section: How many values can each variable(red , green or blue) take? Are you familiar with this number? Can you express it in binary notation? How many combinations (red,green,blue) exist?

Go to https://htmlcolorcodes.com/ to customize your pumpkin’s colors :)

This is our final code:

#include <Adafruit_NeoPixel.h> // ESP Pin driving the LED strip

#define PIN D1 // Total number of LEDs to control (Note that you need about 30mA per LED)

#define NUMPIXELS 20 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); // Declare variables r, g, and b as integers that will store each primary color component

int r; int g; int b; // Declare a colorId, set it to 0

int colorId = 0; void setup()

{

strip.begin();

strip.setBrightness(255);

strip.clear();

strip.show(); // Initialize all pixels to ‘off’

} void loop(void)

{

switch (colorId){

case 0:

r = 255; g = 0; b = 0;

break; case 1:

r = 4; g = 255; b = 0;

break; case 2:

r = 0; g = 222; b = 255;

break; case 3:

r = 255; g = 107; b = 0;

break; case 4: r = 255; g = 0; b = 235;

break; case 5:

r = 255; g = 230; b = 0;

break;

} pumpkinStrip(r, g, b, NUMPIXELS, 2000, 10, 500, 1000);

colorId++; if (colorId > 5){colorId = 0;};

} void pumpkinStrip(int red, int green, int blue, int numberOfLEDs, int fullBrightnessDuration, int fadeSteps, int fadeDelay, int coolDown)

{

for (int i = 0; i < numberOfLEDs; i++)

{

// set the color of LED number i to (red, green, blue). These variables are passed to the function as argument

strip.setPixelColor(i, red, green, blue);

} // Actually show the LEDs with their new setting once all of them received their setting

strip.show(); // Keep the LEDs on for fullBrightnessDuration ms

delay(fullBrightnessDuration); // Fade color to brightness null

float nextRed, nextGreen, nextBlue;

for (int j = fadeSteps; j >= 0; j--)

{

nextRed = j * (red / fadeSteps);

nextGreen = j * (green / fadeSteps);

nextBlue = j * (blue / fadeSteps); for (int i = 0; i < numberOfLEDs; i++)

{

strip.setPixelColor(i, nextRed, nextGreen, nextBlue);

} //Show the LEDs with their new color (decreased brightness)

strip.show(); // Wait fadeDelay ms before decreasing again the brightness

delay(fadeDelay);

} // Let coolDown ms for the LEDs to cool down before a new cycle.

delay(coolDown);

}

Video of the result:

Hope you enjoyed this tutorial and had fun! Please share in comments your wonderful projects based on ESP8266 and LED strip, and feel free to ask any question!