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.
Prerequisites:
We highy recommend that you are familiar with how at least transistors work before making use of this guide. You can check out 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 coding's to find correct resistors then you can click this line: Resistor color coding.
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 Raspberry 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 pin 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:
In our test here then we will be making Raspberry Pi GPIO pin send signal to transistor which then sends signal to optocoupler which will turn on light bulb in different circuit that uses different power source.
Pieces we use:
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.
Calculations:
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.
Note the orientation of the Transistor.
Note the orientation of the Transistor
If you need more information about the GPIO headers on Raspberry Pi or other boards then click bellow.
Raspberry Pi Pin guideOrange Pi One Pin guideOrange Pi Pin guide for most H3 modelsOrange Pi Zero 3 Pin guideOrange Pi 5 Plus Pin guideRock 64 Pin guideIt is good idea hen putting the parts on the breadboard to 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.
Like when learning about how to use transisitors then I added 1K resistor at last moment to act as pull down resistor from ground to the GPIO pin to prevent fluctuating state while the Raspberry Pi boots up.
The Xojo code for this one really simple as this was more of electric guide than coding guide:
The Xojo code for this one is really just the same code as for the Transistor guide.
We just had one checkbox on a window with the caption “LED on”.
In the window I added the following property:
Private mLineRequest As Gpiod.LineRequest
And then Opening event to the Window:
Sub 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, 5, "led control")
catch e as GpiodException
MessageBox(e.Message)
end try
End Sub
Function RequestOutputLine(chipHandle as Gpiod.Chip, lineOffset 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
var lineCfg as new LineConfig()
lineCfg.AddLineSetting(array(lineOffset),setting)
if consumer <> "" then
reqCfg = new RequestConfig()
reqCfg.Consumer = consumer
end if
return chipHandle.RequestLines(reqCfg, lineCfg)
End Function
And finally ValueChanged event for the checkbox:
Sub ValueChanged()
mLineRequest.Value(5) = if(me.Value, Gpiod.LineValue.ACTIVE, Gpiod.LineValue.INACTIVE)
End Sub
You can find this example code at Example code for GPIO guides.