B4R Tutorial RGB Leds

Discussion in 'B4R Tutorials' started by Erel, Apr 19, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed 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:

    [​IMG]

    Connection diagram for a common cathode led:

    [​IMG]

    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:

    [​IMG]

    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.

    Code:
    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(4As 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: Apr 19, 2016
  2. tufanv

    tufanv Expert Licensed User

    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 ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes. The current example sends two bytes. You need to modify it and send 3 or 4 bytes.
     
    tufanv likes this.
  4. tufanv

    tufanv Expert Licensed User

    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 ?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    There was a mistake in the diagram. I've updated it. Pin 3 should be connected to the R leg, 5 to the G leg and 6 to the B leg.
     
    tufanv likes this.
  6. tufanv

    tufanv Expert Licensed User

    Yes, works fine now. Thanks for the correction
     
  7. abhishek007p

    abhishek007p Active Member Licensed User

    how you made the breadboard diagram above ?
     
  8. inakigarm

    inakigarm Well-Known Member Licensed User

    Download fritzing program here: fritzing.org/download
     
    JordiCP likes this.
  9. Beja

    Beja Expert Licensed User

    I am glad to see the Electronics Engineer Erel
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice