So we didn’t get the chance to do color mixing with the LEDs, but I hope you all try it out. Here’s some notes on them….
The LEDs you have in the ARDX kits are common anode, that means that one leg goes to power, and the others connect to your Arduino through a resistor (a small one, like the 560 ohm ones in your kits). A simple circuit without any sensors may look like this :
(my illustration above may not have the proper wiring for the LED, I was doing it on a plane without a datasheet, check the documentation in your kit to verify the wiring.)
To make the elements in the LED glow, you need to create a voltage difference across the anode (connected to power) and the pins connected to the Arduino. When you create this difference, current will flow from power through the LED’s elements, turning it on.
So, in order to PWM the LED’s pins, you wouldn’t do analogWrite(greenPin, 255) to turn it to full brightness. That would put 5V on the Arduino’s output, and there would be no voltage difference. Instead, you would reverse the numbers so that 0 = 5V and 255 = 0V. Here’s some code that should fade the LED through various colors using the above circuit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// variables to hold the LED color int rVal = 254; int gVal = 1; int bVal = 127; int rDir = -1; int gDir = 1; int bDir = -1; // constants to name the pins const int rPin = 11; const int gPin = 10; const int bPin = 9; void setup() { // declare the pinModes pinMode(rPin, OUTPUT); pinMode(gPin, OUTPUT); pinMode(bPin, OUTPUT); } void loop() { // PWM the LED // when using a common anode RGB LED like the ones in // your kits, you create a voltage difference across // each diode to light up the LED, that is, a PWM value // of 255 will turn that light off, while a PWM value of 0 // will turn that light on fully. analogWrite(rPin, rVal); analogWrite(gPin, gVal); analogWrite(bPin, bVal); // change the values of the LEDs rVal = rVal + rDir; gVal = gVal + gDir; bVal = bVal + bDir; // for each color, change direction if // you reached 0 or 255 if (rVal >= 255 || rVal <= 0) { rDir = rDir * -1; } if (gVal >= 255 || gVal <= 0) { gDir = gDir * -1; } if (bVal >= 255 || bVal <= 0) { bDir = bDir * -1; } // slight delay so it doesn't rotate color too quicky delay(33); } |
This simply adds or subtracts a number from the analogWrite() value to each pin. If I did this correctly, you should see the colors in the LED slowly change over time. Groovy, man.
If you wanted to add interaction to this, like, say a potentiometer to each color for accurate mixing, wire up three sensors to A0, A1, & A2. Each of these will control a different color element in the LED. Illustration and code are below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// variables to hold the LED color int rVal = 0; int gVal = 0; int bVal = 0; // variables for the sensor values int pot0Val; int pot1Val; int pot2Val; // constants to name the pins const int rPin = 11; const int gPin = 10; const int bPin = 9; const int pot0Pin = A0; const int pot1Pin = A1; const int pot2Pin = A2; void setup() { // declare the pins connected to the LED as OUTPUT pinMode(rPin, OUTPUT); pinMode(gPin, OUTPUT); pinMode(bPin, OUTPUT); } void loop() { // read the sensors pot0Val = analogRead(pot0Pin); // slight delay to settle the ADC between readings delay(2); pot1Val = analogRead(pot1Pin); // slight delay to settle the ADC between readings delay(2); pot2Val = analogRead(pot2Pin); // map the values appropriately // when using a common anode RGB LED like the ones in // your kits, you create a voltage difference across // each diode to light up the LED, that is, a PWM value // of 255 will turn that light off, while a PWM value of 0 // will turn that light on fully. rVal = 255-pot0Val/4; gVal = 255-pot1Val/4; bVal = 255-pot2Val/4; // PWM the LED analogWrite(rPin, rVal); analogWrite(gPin, gVal); analogWrite(bPin, bVal); } |
Happy blinking!