In this example we are going to take a look at Optocouplers.

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.

We hight recommend that you are familiar with how at least transistors work before making use of this guide.  You can check our transistor guide if you are not familiar with transistors.

You should be familiar to working with breadboards before using this guide.

If you don’t know resistor color codings to find correct resistors then you can click this line Resistor color codings

Basic intro to optocouplers:
Optocouplers give you a way to send a signal to a circuit that is completely isolated from electronic point of view. You could think of optocoupler as a sort of a transistor except it operates by sending light and a photo transistor from the other circuit will pick up the signal and open or close the circuit. Making complete isolation from electric point of view.

Use cases are when you need to for example isolate from your main circuit, for example a motor could make spikes and damage your Rapsberry PI, or if you simply needed second power supply since the Raspberry PI will not supply unlimited amount of power.

If we take a look at the optocoupler then it looks like this:


If you look at the picture on left the you will notice a small dot near 1, this you will find on all Optocouplers and it helps you find pin number one. The other pins are arranged as the picture on left shows.

The picture on right shows the insides of the optocoupler.  As you can see the left side, pin 1 and 2, is basically LED diode, that the controlling circuit uses to send a signal to the the other circuit.

Pin 1 is the Anode (+)
Pin 2 is the Cathode (-)

Pin 3 is the emitter of the transistor
Pin 4 is the collector of the transistor.

The optocoupler we will be testing here is called  PC817. You will want to look up the data sheet for the optocoupler to get its stats, first thing you will be interested in is Forward voltage.  For the PC817 this will give 1,2 V and 20 mA which mean this is what you need to be able to send signal from the sending circuit. (Pin 1 and Pin 2).

The 20 mA here will right away ring warning bells as GPIO pin can only supply 16 mA maximum. So a transistor will be needed in-front of the optocoupler to not take too much current from the GPIO pin.

Other thing you should check out in the data sheet for the optocoupler is the collector current. This is how much current the can pass through the optocoupler in the receiving circuit. Note that optocouplers usually take a lot less current than transistors. For this PC817 this value is only 50 mA.

If the current in the optocoupler is to low for your requirements then common solution is to let the optocoupler control a transistor in the receiving circuit to overcome the current limitations.

The test:

The test we will be making here is to let Raspberry PI GPIO pin send signal to optocoupler which will turn on light bulb in different circuit that uses different power source.

Pieces we use are:
  • Raspberry PI 2 (or Raspberry PI 3)
  • Cobbler and Cobbler cable
  • Bread board
  • Red LED (can be any LED you want)
  • PC817 optocoupler -> those are cheap I paid $3.99 including shipping to Iceland for 50 pieces on Ali Express. (Note be careful what you order, I first ordered different ones that were only suitable for soldering and would not work on breadboard)
  • 2N3904 Transistor (This one is common generic use NPN transistor, which you can easily substitute with many other such as BC548 and others). Their really cheap, I paid $1.22 for 100 pieces in store called Electronic Gadget World on Ali Express. This included free shipping to Iceland. Thats 1,2 cents per transistor.
  • Some resistors (types depend on specs of your RGB led and your transistor)
  • Secondary power supply of some sort. (I used a breadboard power supply powered by 12 V transformer, their really cheap I paid $1.05 for it including shipment to Iceland, this really should be one of the first things you should own when experimenting the breadboard power supply can be set to 3,3 or 5 V, I used the 5V – see picture bellow)
    Breadboard powersupply
  • Wiring PI Xojo module from Paul Lefebvre

This here would be the circuit:


If we draw line through the image (see bellow) to show the two distinct electrical circuits then it is easy to see that we have complete isolation between the two circuits, only light is passed between them and no electricity.


The secondary circuit could be running at different voltage, 9 or 12 for example but we just had 5V power source for the experiment so we will be using 5 which is same as in the main circuit.


  • R1 would be calculated as Ohms law, R = V / I
    (5V – 1,2V) / 0,020 A = 190 Ω   (We will use 200 Ω)
  • For R2 then the following applies:
    1. We know the Base of the transistor wants around 0,7 V since that is  what NPN transistors usually want. So the voltage over R2 has to be 3,3V – 0,7V = 2,6V.
    2. For the current then we need to apply the following formula: I(B) = I(C) / β.  (The β will usually be marked as hfc in the transistors specs) You google the spec for your transistor and find the Beta Value, it will have some range depending on current, it will be marked as On characteristics in the spec. Mine has the β spec of 100 to 300 at 10 mA and 60 to 300 at 50 mA. Since we are using 16 mA then we will choose β of 90.
      Which means I(B) will be: 0,016 / 90 = 0,00018 A (This is how little we will be taking from the GPIO pin)
      Now that we have the voltage and current then R2 = 2,6 V / 0,00018 A = 14,4 kΩ  (We will use 15 kΩ resistor for R2)
  • R3 would be calculated as Ohms law, R = V / I
    (5V – 2,2V) / 0,016 A = 175 Ω   (We will use 180 Ω)

Taking it to the breadboard:

On the breadboard it would look something like like bellow, I put it on two boards so there would be no confusion in the guide with the side rails since some full size bread boards let the side rails go all the way while others half way. We don’t want to mix up the two power sources there.

Optocoupler Experiment BB_bb

GPIO Header


When putting the parts on the breadboard you should try to go by the circuit drawing instead of blindly following the breadboard drawing since its easier to make mistakes if not understanding the wiring.


The Xojo code for this one really simple as this was more of electric guide than coding guide

We just had one button on a window with the caption “Toggle LED”.

In the window I added the following property:

Protected ledOn As Boolean = false

The Window Open event was as follows:

The open event of the window:

Sub Open()
End Sub

The button box event:

Sub Action()
if ledOn then 
ledOn = false
ledOn = true
end if
End Sub

If your not happy with undetermined state at startup then you can solve that by adding 1 kΩ pull down resistor as was shown in the bottom of the transistor example.

If you dont feel like making the Xojo code then you can also just simply do from terminal:

sudo gpio mode 21 out
sudo gpio write 21 1
sudo gpio write 21 0

(21 there instead of 5 since when run in terminal it is using different number system)

Leave a Reply

Einhugur technical blog that involves Xojo and Einhugur Xojo plugin related topics

%d bloggers like this: