In my time interfacing with 24v bullshit hardware I have found it useful to implement optoisolators to protect my nice electronics from the nasty outside world. Also, it makes a convenient inverter or level shifter.
This is what I’m going to call the most basic version. You can see it’s from a previous project as well. What’s going on here? Well, the left side is taking a signal and using it to light an LED. That’s all it is. You take whatever signal you have and light an LED. In this case it’s taking 5v DC pulses and we’re current limiting it and those pulses are producing pulses of light. On the right side we have a phototransistor. That seems a bit complicated so imagine it’s a button. Whenever the LED lights, but button connects pins 3 and 4. That means when no light shines pin 8 is pulled up to 5v with the 10k resistor. When the light turns on pin 8 is pulled down to ground (and some current flows through the 10k resistor, but not much). With that we can see that this is that’s known as an ‘inverting’ configuration. When 5v is present on the left side, the right side is grounded.
This is very similar, but if I have enough voltage and I want to know if the optoisolator is on, I can add another LED in series. In this case I have 24v, so 2 LEDs is no problem and the current is regulated with the resistor. The right side is the same as before.
This is a little different too, on the left side you can see we no longer have an LED in series, it’s in parallel with its own current limiting resistor. This lets us use a lower voltage but still have that visual indicator. On the right side we have something new. There’s a 24v source voltage, but when the LED is not shining the pin is pulled down to ground (and has a small capacitor to filter out spikes). When the LED is lit the 24v flows to the pin (the pi in this case is a raspberry pi with a 24v input module on it). This reverses the polarity from what we saw before. A voltage on the left means we get a voltage on the right.
Here’s that implementation but with slightly different optoisolators (these have 2 LEDs inside so they work with both polarities) and we have an additional resistor on the output.
I added those resistors in this case so I could drive things like LEDs that needed their own series resistor, but I didn’t want to hand-wire them in off the board. In most cases those can be 0 ohm resistors as I labeled.
This is the generic version of a pretty standard configuration for an optoisiolator, I even added some notes for why I chose those values and omitted the output pullup.
If you try you can pack those in pretty tight (especially if you use both sides of the board).
Well, that’s my quick overview of optoisolators, just remember to use the output for some DC source and make sure the polarity is right and they’re even easier to use than regular transistors! I have the first small diagram on github here, the rest are part of other projects.