Saturday 24 January 2015

Simple Arduino PWM Example with Cathode RGB LED

Cathode RGB LED



These LEDs have 4 prongs. The longest prong is the cathode, which you should hook up to ground.

The other 3 prongs are attached to separate elements in the head of the diode and these elements produce red green and blue light separately.

Our aim: To manipulate these RGB elements independently to produce different colours.

Digitally setting RGB elements on and off


Create this circuit:



Features of the circuit:
- the red prong of the LED is connected to digital pin 11.
- the blue prong of the LED is connected to digital pin 10.
- the blue prong of the LED is connected to digital pin 9.
- appropriate resistors are in place to ensure the LED doesn't get too many Volts which would cause it to blow. (Choosing the resistor is not covered in this post.)

DigitalWrite()


We can switch on/off the red, green and blue components independently using digitalWrite().



Mixing RGB


My arduino outputs 5V. However, I want to be able to reduce the voltage flowing through my LED to get colours other than the set above. For example to produce yellow, I want a bit of red and lots of green.

This can be achieved using Pulse Width Moderation.

Voltage going through the circuit can be switched on and off extremely quickly to effectively give voltage between zero and 5V.

- the more time it spends in HIGH the greater the effective voltage
- the more time in LOW the lower the effective voltage.

Because we are connected to the three separate pins (9, 10, 11) we can apply this technique independently across R, G and B.

To code this we use analogWrite().

AnalogWrite()

You will notice that the three pins (9, 10, 11) to which we are connected are marked with a ~. This means that it is possible to do PWM when connected to them.

The following code fades the LED from RED - BLUE - RED - ... .




No comments:

Post a Comment

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning&#...