RGB LEDs with Arduino

One of the things I love about putting things together is adding light to them. RGB LEDs are some of my favorite little lights. You can have just about any color.
When I first started reading about RGB LEDs I wanted to try one. I went to the local (40 miles away) electronics store. They have single RGB LEDs in a little bag. It was over $5 for one LED. I thought this can get expensive.
When I brought the LED home and put it into a breadboard I couldn’t get it to light. I followed the instructions again after ripping out the jumper wires from the breadboard and the Arduino. Start over, is what I do when something doesn’t work out correctly. Some simple testing revealed that the long pin on the RGB LED I bought was not for ground. The one I bought is a “common anode” RGB LED. The instructions I read covered “common cathode” RGB LEDs. The difference between the two is like night and day.

Testing the RGB LED

You can easily test the RGB LED to see if it has a common cathode, or a common anode, by putting it into a breadboard and using a battery and a resistor. Using a 220-ohm resistor connect one end of the resistor to one of the color legs and the other end of the resistor to the other side of the trough on the breadboard. With the battery leads connect the ground (-) of the battery to the long leg of the LED and the positive lead of the battery (+) to the far end of the resistor. If the LED lights up this is a “common cathode” RGB LED. If the LED does not light up, and the battery is not dead, It is probably a “common anode” RGB LED. Switch the two battery leads to find out. Positive (+) lead goes to the longest leg of the LED and the ground goes to the far end of the resistor. If in this position the LED lights up you have a “common anode” RGB LED. This is important to know because it will affect your Arduino sketch a lot.
Once you get the LED to light up note the color that displays for that leg. Move the resistor to a different shorter leg on the RGB LED and test it with the battery. Note the color for that leg and do the same for the final leg of the RGB LED.

Setting up the RGB LED

Put the LED’s pins into a breadboard.
With a “common cathode” LED the long leg goes to ground. Using a jumper wire connect the longest leg of the LED to ground (GND) on the Arduino. With a “common anode” LED the long leg goes to 5v. You can choose the 3.3v, too.
The other three legs are for the actual color LEDs that are inside the little plastic lens. One is for red, one is for blue, one is for green. Using a 220-ohm resistor connect the first LED leg to the other side of the center trough of the breadboard. Then using a jumper wire connect the other end of the resistor to one of the pins on the Arduino. On the Arduino Uno I choose pin 9. Now do the same for the other two legs of the LED, but connect the second one through a resistor to pin 10 and the third to pin 11. I choose these pins because they are right next to each other on the Uno.
Now you can add some code in the Arduino IDE to turn on the colors of the LED.

// set a color leg of the RGB LED to
// a pin on the Arduino
int redPin = 9;
int bluePin = 10;
int greenPin = 11;


void setup() {
// initialize the digitals pin as outputs
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
// Turn on the red light in the RGB LED.
// IMPORTANT! Only for common cathode RGB LEDs.
digitalWrite(redPin, HIGH);
}


void loop() {
// This is the part of the code that
// runs over and over


}

That is pretty easy and we only turned on the red light in the RGB LED. If this were a common anode RGB LED the light would remain off. For a common anode RGB LED you would use digitalWrite(redPin, LOW); to turn on the red light.
If you wanted to turn on both the red and blue in the RGB LED at the same time and then turn them off a second later. Then you could wait another second and turn them on again, you could do this in the loop() section.
This different code would look like:

// set a color leg of the RGB LED to
// a pin on the Arduino
int redPin = 9;
int bluePin = 10;
int greenPin = 11;


void setup() {
// initialize the digitals pin as outputs
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
}


void loop() {
// This part of the code that runs over and over
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
delay(1000);
}

In this different set of code the loop() section causes the LED to blink. On a common cathode RGB LED writing HIGH to the red and blue pins turns the light ON. On a common anode RGB LED writing HIGH would turn OFF the light. In this small loop the lights cycle ON-OFF-ON-OFF-ON-OFF, etc., for a common cathode RGB LED. The pattern would be OFF-ON-OFF-ON-OFF-ON, etc., for a common anode LED. Not much of a difference, is it?
For this type of Arduino Sketch it doesn’t really matter. It only mattered where you hooked up the long leg of the RGB LED.

Now for what drove me bonkers for a few days

You can change the brightness of the color of the LED, but there is a big difference between the common cathode and the common anode RGB LEDs. If you wanted to cycle the brightness from off to fully bright for the red color on a common cathode RGB LED you would use analogWrite(redPin, brightnessNumber);. In the loop() section of the sketch you can have the brightnessNumber increase from 0 to 255 and once it reaches 256 reset it back to 0. The loop() section will keep doing this for you with the correct code. I’ll just include the loop() section in this next example, and please include the line:

int brightnessNumber = 0;

at the top of the sketch where the other variables are (such as redPin) to create the variable and set it to 0.


void loop() {
analogWrite(redPin, brightnessNumber);
//increase brightnessNumber by 1
brightnessNumber += 1;
if (brightnessNumber > 255) {
brightnessNumber = 0;
}
delay(50)
}

This will work just fine on a common cathode RGB LED. On a common anode RGB LED the effect will be to start from full bright and dim downward to off.
On a common cathode RGB LED the number 255 for analogWrite means fully bright. On a common anode RGB LED the same number means fully off. Your code to get the same effect would have to be changed as below.

int brightnessNumber = 255;

at the top of the sketch to create the variable and set it to 255.


void loop() {
analogWrite(redPin, brightnessNumber);
//decrease brightnessNumber by 1
brightnessNumber -= 1;
if (brightnessNumber < 0) {
brightnessNumber = 255;
}
delay(50)
}

Did you notice that you have to start with brightnessNumber at 255 and subtract it by 1 to increase the brightness of the light. When the brightnessNumber gets to -1 it is less than zero and needs to be changed to 255 to restart the cycle.

If you don’t have the correct coding for the RGB LED that you have, you won’t get your desired effect.

Now watch after all this I got the common cathode confused with the common anode.

LMT86 and Arduino

Temperature with an Arduino Uno and an
LMT86 Analog Temperature Sensor – by Greg Bushta

The Datasheet from TI for the LMT86 has a chart with the corresponding temperatures in °C and a mV. I noticed that the number read from the center pin is 1/5 that of what is in the chart. I am powering the LMT86 with 5v from the Arduino Uno. I noticed that there is about 11mV different from one °C to the next degree. However, when the number read on the pin goes down the corresponding temperature rises. I looked at the number corresponding to 0°C, 2100, and the number for 100°C, 997. These represent freezing and boiling of water. I subtracted 997 from 2100 and divided the result by 100 to get the approximate difference per degree C that the mV number would be. The result was 11.03. So, I figured that for every rise in degree C starting at 0° I could subtract 11.03. I thought why am I multiplying the number read on the pin by 5 and then doing my calculations with 11.03. Why not divide 11.03 by 5 and work with the number read from the pin. 2100 divided by 5 would give me the number read from the pin at zero degrees, or 420 as my starting point. So, I take the reading from the pin and subtract it from 420 then divide the answer by 2.206 to get my degrees in C. From there I convert °C to °F with °F = (°C * 9/5) + 32.

I think It is fairly accurate since it is reading 380 and feels just a little on the chilly side in the room. After all it is mid-January and 2:45 in the morning. It is raining outside. There is a fire going in the other room. I am off to sleep.
– Greg

(the next morning)

I tested the calculation with a piece of ice wrapped in plastic to keep the sensor from getting wet. The reading with the ice held upon the sensor was 426. That calcs to 27°F. So, that is not quite correct. Close but not quite right. I wonder if the correct starting number for 0°C should be 426.

Wouldn’t that be the correct way to do the measuring? Or, perhaps the scientific way? Get a starting number for zero degrees C and another number for a known temperature, perhaps boiling water. Then I could do the correct calculations. I would need to get the range and figure the steps per degree again. But my calculations would be much more accurate.

At boiling I got 212 on the read pin.
On ice I got 426 on the read pin.
Lets do the calculations with those numbers.

426 – 212 = 214
214 / 100 = 2.14
Boiling is 212. Freezing is 426. The difference is 214. From ice to boiling is 100 degrees, or steps. So, each step or degree is 2.14 on the pin read.

The LMT86 Sensor is hooked up
on the Arduino Uno with
+ pin on LMT86 is hooked to 5v
GND pin goes to Arduino GND
Data (center) pin goes to A0


int LMT86Pin = A0;
int TempRead = 0;
float voltage = 0;
float Temperature = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(“LMT86 test!”);
Serial.println(“Testing the Temperature Sensor”);
}

void loop() {
// put your main code here, to run repeatedly:
TempRead = analogRead(LMT86Pin);
Serial.print(“TempRead = “); Serial.println(TempRead);
Temperature = (426 – TempRead) / 2.14; // was 420 and 2.206
Serial.print(“The temperature is “);
Serial.print(int(Temperature));
Serial.println(“°C”);
Temperature = (Temperature * 9.0 / 5.0) + 32;
Serial.print(“The temperature is “);
Serial.print(int(Temperature));
Serial.println(“°F”);
delay(5000);
}