Assembling the Circuit

Figure 6-2 shows how to assemble the circuit. Just as you did in “Controlling Light with PWM” use a 220-ohm resistor (red-red-brown) with each LED, and just as you did in “Analogue Input”, use a 10 K ohm resistor with the photoresistor.

Remember from “Controlling Light with PWM” that LEDs are polarized: in this circuit, the anode (long lead, positive) should go to the right, and the cathode (short lead, negative) to the left. Figure 6-2 also shows the flattened side of the LED, which indicates the cathode.

Build the circuit as shown, using one red, one green, and one blue LED. Next, load the sketches into Arduino and Processing. Upload the Arduino sketch to the Arduino, and then run the Processing sketch and try it out (you will need to press the button to get the lamp to come on). If you run into any problems.

Instead of using three separate LEDs, you can use a single RGB LED, which has four leads coming off it. You’ll hook it up in much the same way as the LEDs shown in Figure 6-2, with one change: instead of three separate connections to the ground pin on Arduino, you’ll have a single lead (called the common cathode) going to ground.

figure6 2

Adafruit sells a four-lead RGB LED for a few dollars. Also, unlike discrete single-colour LEDs, the longest lead on this kind of RGB LED is the one that goes to ground. The three shorter leads will need to connect to Arduino pins 9, 10, and 11 (with a 220-ohm resistor between the leads and the pins, just as with the separate red, green, and blue LEDs).

The Maker Shed Getting Started with Arduino Kit includes an RGB LED as well.

NOTE

The Arduino sketch is designed to work with a common cathode RGB LED (one where the long lead goes to ground). If you’re getting the wrong output, you might have a common anode RGB LED. If that’s the case, change the code where you set the LED intensity as shown (you are basically inverting the values; where you used 0, you’d now use 255):

  if (state == 1) { // if the lamp is on
    analogWrite(R_LED, 255 - r);  // turn the leds on
    analogWrite(G_LED, 255 - g);  // at the colour
    analogWrite(B_LED, 255 - b);  // sent by the computer
  } else {
    analogWrite(R_LED, 255);  // otherwise turn off
    analogWrite(G_LED, 255);
    analogWrite(B_LED, 255);
  }

Now let’s complete the construction by placing the breadboard into a glass sphere. The simplest and cheapest way to do this is to buy an IKEA FADO table lamp. It’s now selling for about US$19.99/€14.99/£11.99 (ahh, the luxury of being European).


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *