B4R Tutorial RGB Leds

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:

SS-2016-04-19_12.57.36.png


Connection diagram for a common cathode led:

SS-2016-04-19_14.41.15.png


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:

SS-2016-04-19_13.03.44.png


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:

tufanv

Expert
Licensed User
Longtime User
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:

SS-2016-04-19_12.57.36.png


Connection diagram for a common cathode led:

SS-2016-04-19_12.58.50.png


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:

SS-2016-04-19_13.03.44.png


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

Thanks for the great tutorial !
combining this tutorial to ble tutorial can we send the data for:
SetHexColor(0xFFF304EA)

from a b4i app and set the color remotely ?
 

tufanv

Expert
Licensed User
Longtime User
Yes. The current example sends two bytes. You need to modify it and send 3 or 4 bytes.
ty,
I have a problem. I select a red color hex 0xFFFF1600
but what i see on rgb led is stg like blue. Is it because the connection , I have to change the gnd to 5v ? Or another problen ?
 

Beja

Expert
Licensed User
Longtime User
I am glad to see the Electronics Engineer Erel
 
Top