Introduction: Arduino Mood Light
This is a simple project based on Adafruit Arduino - Lesson 3. RGB LED
You will find lots of useful information on RGB LEDs if you follow the link.
The original programme was to cycle through a number of discrete colours.
What I wanted to see was the LEDs cycling through the full spectrum of colours with a gradual and almost imperceptible change.
I decided that the best way to do this was with sine waves.
Step 1: A Little About Sine Waves
Sine waves are continuous waves which can be found in nature as sound waves, radio waves, light waves and many other forms. They can be described by their amplitude (how big they are) and their frequency (the rate at which the wave repeats).
Now for a little Maths
The sine wave can be describe by the formula A sin(2*PI*F*t)
Where A is the amplitude.
Amplitude is the maximum value that the wave deviates from the central axis (usually zero).
F is the frequency or how many times the wave cycles in 1 second and is measured in (Hertz: Hz), The wave shown in the picture is of frequency 1Hz.
t is the time in seconds (s) and is the variable quantity that allows the formula to describe a wave rather than just give a fixed answer.
The period as shown on the diagram (sometimes known as the wavelength) can be described as:
p (period) = 1/F (frequency).
2*PI is the ratio of the radius of a circle (analogous to the amplitude), to the circumference of a circle (analogous to the period of the wave).
Step 2: What You Will Need
A computer loaded with the arduino ide https://www.arduino.cc/en/Main/Software
Resistors 330 ohm x 3 or 6 if you use two LEDs
Step 3: Connecting Everything Up
Connect as shown. Please note: I used common cathode LEDs, if you have used common anode you may need to take this into account when wiring.
The common cathode LEDs are wired as follows.
From the flat side
Cathode (longest pin)
For the Arduino I have used pins 9, 10 and 11, these are PWM pins which are necessary for the programme to work.
Step 4: The Code
Based on Adafruit Arduino - Lesson 3. RGB LED
Programme to vary colours on RGB LED's using sinewaves
By Ray Houghton
Please feel free to use this software or muck about with
it as you please.
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
/* Output pins for LEDs (these are all PWM outputs*/
//uncomment this line if using a Common Anode LED
//set pins to output
for (t = 0; t < 1000; t = t + 0.001)
// sets instantaneous time element of sine functions.
/* Sine functions to vary the PWM outputs are of the form
offset + amplitude * sin(2 * pi * F * t)
Where offset ensures sinewave does not go less than zero and
where F is the desired frequency
NOTE: Red green and blue are different brightnesses with RGB LEDs,
so offset and amplitude are adjusted to compensate.
int redsat = 126 + 126*sin(2*3.141592654*0.03*t);
int greensat = 64 + 64*sin(2*3.141592654*0.027*t);
int bluesat = 32 + 32*sin(2*3.141592654*0.025*t);
//Call function setColor
setColor(redsat, greensat, bluesat);
void setColor(int red, int green, int blue)
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
Step 5: A Little Explanation of the Code
The code describes the sine waves in the above diagram.
To give a variation between zero output and maximum output, the sine waves need to vary between 0 and 255. A normal sine function of the form:
Will give a wave which varies between – 128 and +128. To make the wave between 0 and 255 we need to add an offset. This gives the form
MAX/2 + A sin(2*PI*F*t)
Where A is the maximum (MAX) amplitude available (256) divided by 2.
As the RGB LEDs give different intensities for red, green and blue, I have reduced the amplitude and the offset.
The frequencies used give a period of around 30 seconds.
redsat = 126 + 126*sin(2*3.141592654*0.03*t);
greensat = 64 + 64*sin(2*3.141592654*0.027*t);
bluesat = 32 + 32*sin(2*3.141592654*0.025*t);
The three sine waves described in the code are at slightly different frequencies,
this means that the mix of colours will change in a way which will not repeat until the variable (t) expires (1000 in steps of 0.001).
We have a be nice policy.
Please be positive and constructive.