RGB Leds are equivalent to three leds (red, green and blue) combined. This allows mixing the three colors which results in many different colors.
There are two types of RGB leds: common cathode and common anode:
Connection diagram for a common cathode led:
A common anode led should be connected to the 5V instead of Gnd.
Note that the three pins are connected to PWM supported pins as we are using AnalogWrite to control the level of each "channel".
In this example we will set the color based on the color value from the IDE color picker:
The code converts the 4 bytes number to bytes with RandomAccessFile. Note that it is initialized in big endian mode. Otherwise the channels will be reversed.
There are two types of RGB leds: common cathode and common anode:
Connection diagram for a common cathode led:
A common anode led should be connected to the 5V instead of Gnd.
Note that the three pins are connected to PWM supported pins as we are using AnalogWrite to control the level of each "channel".
In this example we will set the color based on the color value from the IDE color picker:
The code converts the 4 bytes number to bytes with RandomAccessFile. Note that it is initialized in big endian mode. Otherwise the channels will be reversed.
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private rpin, gpin, bpin As Pin
Private const COMMON_ANODE As Boolean = False
Private raf As RandomAccessFile
Private buffer(4) As Byte
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
rpin.Initialize(3, rpin.MODE_OUTPUT)
gpin.Initialize(5, rpin.MODE_OUTPUT)
bpin.Initialize(6, rpin.MODE_OUTPUT)
raf.Initialize(buffer, False) 'big endian
SetHexColor(0xFFF304EA)
End Sub
Private Sub SetHexColor (color As ULong)
raf.WriteULong32(color, 0)
If COMMON_ANODE Then
'common is +
rpin.AnalogWrite(255 - buffer(1))
gpin.AnalogWrite(255 - buffer(2))
bpin.AnalogWrite(255 - buffer(3))
Else
rpin.AnalogWrite(buffer(1))
gpin.AnalogWrite(buffer(2))
bpin.AnalogWrite(buffer(3))
End If
End Sub
Last edited: