#### DISCLAIMER

We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry Pi.

Pieces we use:

• Raspberry Pi 2, 3, 4 or 5.
• Cobbler and Cobbler cable.
• 4 pin RGB LED.
• Some resistors (types depend on spec of your RGB led).
• We are going to be using digital multi-meter to verify the spec of the LED and to verify our calculations. But if you know the specs of your LED for sure then you will not be needing this. (Digital multimeter is really one of the first things you should have if you are going to be playing around with electronics. Their cheap you can get
good one for around \$15 to \$20. Mine was bought in Kiel in Germany right at the time when the endless lines of Trabants started to flow from East Germany to the West, so they really can last you for long time). (Edit I have replaced the multimeter by now when this guide was updated).
• Gpiod Plugin for Xojo to use libpgiod in Xojo. The plugin you can get download here.  Or if you are interested in its source code then you can go here.

#### If you don’t know resistor color coding's to find correct resistors then you can click the link bellow

First thing is to understand the basics of LED.

You need to know the spec of your LED, important numbers are Vf and how much maximum current they can handle.

The Vf is the forward voltage of the LED.  This is important when you put it into the Ohms equations.

The GPIO pin on Raspberry PI will have around 3.3 V and the absolute maximum you may take from it is 16 mA, peek usage, meaning the pins were designed for about 3 mA if all are in use.
So to calculate the resistor needed then you need to know Vr which is the Voltage on the circuit minus forward voltage of the LED (Vf). Then we can apply the Ohms law:

I = V / R
which can be re-written as:

R = V / I

Where V is Voltage of the circuit – Vf of the LED and I is desired current.

We are in this example going to aim for 5 mA.

Wiring of the RGB 4 pin led:

If we first look at normal LED in the picture bellow then you can see that you have two ways to identify the + and – or anode vs cathode.
Its the length of the pins where the longer wire is the anode and the shorter is the cathode. If wires have been trimmed then you can usually also see it on the LED it self the flat side on the edge is the cathode or – as is shown on the picture to right here.

For RGB led then things get a bit more complicated. RGB LED's usually share common cathode (black which would be the longest wire or you can see it inside the glass by the shape there if the wire has been shortened), and then three anodes, red on the left then greed right of the cathode and then blue on far right.

(To complicate it a bit then I have seen RGB led where the anode was shared and there were three cathodes)

I thought the spec of the led that I used was around 2 Vf for Red with
max 50mA current. and 3 Vf for green and blue with max of 30 mA

(Check specs on your LED if you have data before calculating) (See bellow, where I was incorrect on the spec)

We apply this spec to the equations shown above where we decided on taking 3 mA from the pin.

R(red) = (3.3 – 2.0) / 0.005 = 433 Ω => 440 Ω
R(green) = 3.3-3.0) / 0.003 =100Ω
R(blue) = (3.3-3.0) / 0.003 = 100 Ω

Unless you got 5 band resistor that is 433 Ω then use normal 4 band one that is   440 Ω. We went with 5 band 433 Ω

Now I was not  sure of the spec I had for the RGB LED as I had just guessed it from fairly normal values but I didn't know, so I proceeded with caution, instead of going for GPIO’s right away then I decided to first just connect those resistors and the LED to the 3,3 V output on the Raspberry Pi along with a Digital Multimeter in between to measure the current I would draw with this setup.

So I just connected 3,3 V output and GND, on the LED with a Digital multimeter to measure the current draw.

The connection was done as shown in the drawing above, connecting to one resistor at a time to check their current draw.

I had the LED on mini bread board because of this middle step it was easier to deal with it.
Note that the 3,3 V output on Raspberry Pi has maximum draw of 50 mA so you cannot do tests like this on just anything.
It was soon obvious that the LED I had had totally different spec than expected, I ended with 820 Ω for the red and 220Ω for the others, then I had values I was confortable putting on the GPIO.

After the calibration with the multi-meter then we had the following resistors:

R(red) =  820Ω
R(green) =220Ω
R(blue) = 220 Ω

(If only having calculating values then its not bad idea to go with a little higher value resistors than you calculated to protect the pins just in case).

If wanting led to shine at its full brightness using maximum current draw without damaging the GPIO pin on your Raspberry Pi then see our guide about putting LED behind transistor.

Once I was satisfied with the values read from the multi-meter and convinced that it would not draw to much from the Raspberry GPIO pin then I proceeded in wiring the GPIO pins to the LED.

I selected GPIO 4, for Red anode, GPIO  5 for Green anode, GPIO 6 for Blue anode.

The Xojo code for this one really simple:

We just had three checkboxes on a window with the caption “Red LED On”, "Green LED On", "Blue LED On".

In the window I added the following property:

``````Private mLineRequest As Gpiod.LineRequest
``````

And then Opening event to the Window:

``````Sub Opening() Handles Opening
try
using Gpiod

var chip as Chip = Chip.Open("/dev/gpiochip0")

// We call helper function to set up pin number 5 in output mode.
mLineRequest = RequestOutputLine(chip, array(ctype(4, uint32), ctype(5, uint32), ctype(6, uint32)), "rgb led control")

catch e as GpiodException
MessageBox(e.Message)
end try
End Sub
``````

Notice that since all three pins have same settings then one line request is enough.

``````Public Function RequestOutputLine(chipHandle as Gpiod.Chip, lineOffsets() as UInt32, consumer as String) As Gpiod.LineRequest
// Helper function to set up output pin.

using Gpiod

// Exceptions will be caught by the caller method

var reqCfg as RequestConfig = nil

var setting as new LineSetting()
setting.Direction = Gpiod.Direction.OUTPUT
setting.OutputValue = Gpiod.LineValue.INACTIVE
setting.Bias = Gpiod.LineBias.PULL_DOWN // We ask for pull down resistor to get default off state.

var lineCfg as new LineConfig()

if consumer <> "" then
reqCfg = new RequestConfig()
reqCfg.Consumer = consumer
end if

return chipHandle.RequestLines(reqCfg, lineCfg)
End Function
``````

And finally ValueChanged events for the checkboxes:

``````Sub ValueChanged() Handles ValueChanged
mLineRequest.Value(4) = if(me.Value, Gpiod.LineValue.ACTIVE, Gpiod.LineValue.INACTIVE)
End Sub
``````
``````Sub ValueChanged() Handles ValueChanged
mLineRequest.Value(6) = if(me.Value, Gpiod.LineValue.ACTIVE, Gpiod.LineValue.INACTIVE)
End Sub
``````
``````Sub ValueChanged() Handles ValueChanged
mLineRequest.Value(5) = if(me.Value, Gpiod.LineValue.ACTIVE, Gpiod.LineValue.INACTIVE)
End Sub
``````

#### Getting the code

You can find this example code at Example code for GPIO guides.

You might ask why the led is on when the Raspberry Pi boots up, before you start your Xojo program.

The reason for that is that even if we requested in the code pull down resistor on the pin then before our program runs then pins are in undetermined state.

To solve this then you would put 3 pieces of 1k Resistors going from Ground, to GPIO 4, GPIO 5 and GPIO 6, this would give them hardware pull down resistor, meaning their default state would be off even at boot time.