arduino
Arduino Nativity Set LED Light Controller – Led fading effects
Now it’s time for some code.. :)
With Arduino it’s easy writing a simple sketch for dimming LEDs via PWN.. also for guy with (very) limited programming skills like me.
The learning material is very helpful and rich of examples.
We need to manage 3 independent led channels and a create a kind of “program” for lights effects.
Requirements for this first version:
- the total duration of the program/sequence should be configurable
- led brightness should be dimmable and max brightness configurable
- each led channel should have an independent fade-in phase, a constant light phase and a fade-out phase with scheduled start and stop times
Timing
The sequence is divided in 1024 steps, each one lasting for a number of milliseconds calculated dividing the total duratation (in ms) by 1024.
Of course we need to have only ONE delay at the end of the main loop.
..
float delayvalue = 0;
int cycledelay = 0;
int programduration = 30; // light program duration in seconds
…
void setup() {
…
delayvalue = ((float(programduration) * 1000) / 1024); // main delay in ms calculated on program duration
cycledelay = int(delayvalue);
}
…
void loop() {
…
delay(cycledelay);
}
In this example the program will last about 30 seconds.
Now we need to have a Fading (In and Out) lasting for the duration of the entire phase.
analogWrite function have only 255 values and we may want to have a longer lasting FadeIn/Out phase.
So we need to calculated the Fading step related to the duration of the phase.
It’s also useful to be able to control the maximum brightness we want to reach for creating for example weak illumination effects.
…
int brightnessCH1 = 0; // how bright the LED is
int brightnessCapCH1 = 0; // the maximum value of brigther (0-255)
float fadeAddCH1 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated
float fadeSubCH1 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated
float brightfloatCH1 = 0.0; // brightness in float
…
// Parameters, edit these for creating your custom show
…
int fadePcCH1 = 20; // the maximum percent of Fade for CH1
int CH1FadeInStart = 1; // start time of FadeIn phase for CH1
int CH1FadeInStop = 256; // stop time of FadeIn phase for CH1
int CH1FadeOutStart = 257; //start time of FadeOut phase for CH1
int CH1FadeOutStop = 512; //stop time of FadeOut phase for CH1
…
void setup() {
…
pinMode(3, OUTPUT);
…
}
…
void loop() {
cycleValue = cycleValue + 1;
// CHANNEL1
// Lets calculate the fadeStep related to duration of FadeIn and the cap percent
if (fadeAddCH1 == 0) {
fadeAddCH1 = ((255 * (float(fadePcCH1) / 100)) / (float(CH1FadeInStop) – float(CH1FadeInStart)));
//Serial.print(“fadeAddCH1: “);
//Serial.println(fadeAddCH1);
}
…
// Lets calculate the fadeStep related to duration of FadeOut and the cap percent
if (fadeSubCH1 == 0) {
// fadeSubCH1 = ((float(CH1FadeOutStop) – float(CH1FadeOutStart)) / 255) * (float(fadePcCH1) / 100);
fadeSubCH1 = ((255 * (float(fadePcCH1) / 100)) / (float(CH1FadeOutStop) – float(CH1FadeOutStart)));
//Serial.print(“fadeSubCH1: “);
//Serial.println(fadeSubCH1);
}
…
// FadeIn phase
if (cycleValue >= CH1FadeInStart && cycleValue < CH1FadeInStop) {
brightfloatCH1 = brightfloatCH1 + fadeAddCH1;
brightnessCH1 = int(brightfloatCH1);
}
...
// control Max brightness
brightnessCapCH1 = int(fadeAddCH1 * 255);
constrain(brightnessCH1, 0, brightnessCapCH1);
//Serial.print("brightnessCH1: ");
//Serial.println(brightnessCH1);
...
// FadeOut phase. Some extra cycles are useful to be sure to bring to 0 variables
if (cycleValue >= CH1FadeOutStart && cycleValue < (CH1FadeOutStop + 20)) {
brightfloatCH1 = brightfloatCH1 - fadeSubCH1;
brightnessCH1 = int(brightfloatCH1);
}
// brightness must be positive
if (brightnessCH1 < 0) {
brightnessCH1 = 0;
brightfloatCH1 = 0.0;
}
...
analogWrite(3, brightnessCH1);
if (cycleValue == 1024 ) {
cycleValue = 0;
}
// The delay must be only one
delay(cycledelay);
}
In this example Leds conntected to Channel 1 (PWN Pin 3);
- will have a maximum brightness of the 20 percent
- the FadeIn phase will start at step 1 and will end at step 256 (reaching the max brightness of the 20%)
- the FadeOut phase will start at step 257 (no constant phase) and will end at step 512 (reaching a zero value)
The code can be optimized a lot with for cycles and arrays instead of variables.. but it works and it’s easier to read.
This is the download link for the full sketch.
Have fun and please leave a comment if something is unclear.. :)
Arduino Nativity Set LED Light Controller – Hardware
My job as IT consultant leave empty the (human) need of creating something “physical”, something made with your brain but also with your hands.. so a few months ago I started playing with Arduino and a bunch of electonics components.
The most amazing part of Physical Computing for a “software” grown-up kid like me (C64 generation..) is that you really create “things”, without simply configuring a “limited” device.
It’s really a new world!
A few days ago my 10 years old brother asked me to add some “cool” light effects to the nativity set that he is building year after year with “some” help of our father.. :)
The set is built mainly with wood and paperboard, so we have to keep low the risk of fire avoiding incandescent light bulbs.
Why not using LEDs? They are cheap, available in multiple colors and very brigth.
And why not using an Arduino for creating a sequence program with some dimming effect?
This nativity set needs al least 3 independent “channels” for lights: manger, village houses and street lamps.
Basic requirements for the 0.1alpha version of this project are:
- having a night/day cycle with a given duration
- controlling 3 independent channel with 10-15 high brightness LEDs each one
- managing dimming effects as fade-in and fade-out
Arduino can “simulate” the dimming of led with PWM via the AnalogWrite function.
Arduino Uno has 6 PWN Digital pins but each each one has a maximum output current of 40 mA.
Each LED needs 20 mA, so we need to “amplify” the output of PWN Pin current with a NPN transistor.
The best choice if using a ULN2003A Seven Darlington Arrays which is very useful for driving loads like some LEDs.
Each of the seven driver has a maximum output current of 500mA.. enough for more of 20 standard LEDs.
This is my first Fritzing design.. :)
After some test with a breadboard (not usable in the set) I made my first Arduino custom shield.
For this first “milestone” we need:
- An Arduino 2009 or UNO
- a pad board / stripboard with 0.1 inch spacing and a tool for cutting it (I used a Dremel with no. 409 Cut-Off Wheel)
- 1 DIP-16 ULN2003A
- a DIP-16 socket (not strictly needed)
- some screw terminal block connector with 0.1 inch spacing
- male-male headers with 0.1 spacing
- some copper wire and a soldering iron
Excluding the Arduino, less than 5 euros of components.
I was a bit disappointed when I discovered that spacing between digital pins blocks is not 0.1 inch compatible..
For the moment i needed only 3 PWM pins plus +5V and GND pins.. so i used only 1 block of 8 male header and one block of 2 male header..
This is the final result.
Warning: Soldering (and photography) skills below zero.. :)
In the next post I will describe the first sketch that will manage LEDs.
See you soon!
Categories
- Arduino (2)
- AskoziaPBX (6)
- cisco (1)
- Debian Lenny (6)
- general (1)
- Home server (6)
- Linux (6)
- m0n0wall (1)
- switching (1)
- voice (1)
- Voyage Linux (6)


