Arduino Giga R1 WiFi PC817 Optocoupler: Monitor 12V/24V DC Input Signal
Machine control panels run on 24V, the Arduino Giga R1 WiFi runs on 3.3V, and a PC817 optocoupler module is the small screw-terminal board that lets those two worlds meet without one destroying the other. The module is what this guide is built around: the PC817 chip, its series resistor and a status LED are already fitted and wired for you, so the whole job becomes landing two field wires on a terminal block and reading one pin. Most of the time the question being asked is a plain one — is that contactor actually pulled in, is that machine actually running right now? — and a PC817 module wired across the device answers it without cutting a single existing wire.
The one thing you have to get right is which module you buy. These modules are sold as three separate variants, and each one is built for a single input voltage.
In this guide, we will learn:
- What a PC817 optocoupler is and how the ready-made module is put together
- How to choose between the 24V, 12V and 3-5V module variants, and what happens if you pick the wrong one
- How to wire the module's IN+, IN-, VCC, GND and OUT terminals to the Arduino Giga R1 WiFi
- How to wire the module in parallel with a contactor coil, lamp or valve to monitor whether that device is on or off
- How to tell in under a minute whether your module reads active-LOW or active-HIGH, and how to flip the code to match
- How to write Arduino Giga R1 WiFi code that reads the isolated input and prints its state
- How to build the same input stage from the bare four-pin chip, if you would rather

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Buy the variant that matches the voltage on the wire you intend to watch. In a machine panel that is almost always the 24V module — pick that one unless you know the circuit is something else.
Overview of PC817 Optocoupler
The PC817 is an optocoupler, also called a photocoupler or an opto-isolator. Inside its plastic body there is an infrared LED facing a phototransistor across an insulating gap: when current flows through the LED, its light switches the transistor on, so the signal crosses as light instead of electricity — which is why the two sides stay electrically separate.
That separation is the whole reason to use one. A 24V PLC output wired straight onto a Giga R1 WiFi pin would destroy the pin instantly. Routed through a PC817, the 24V circuit only ever drives an LED, while the Giga R1 WiFi only ever reads a transistor sitting on its own low-voltage side. The two circuits share no copper at all.
A PC817 optocoupler isolation module is that chip already mounted on a small board with the parts it needs: a series resistor for the LED, a screw terminal block for the field wires, a pin header for the board side, and a status LED that lights when the input is energised. The resistor is the important detail — it is fitted at the factory and sized for one input voltage, which is exactly why the modules come in three versions.
| Specification | Value |
|---|---|
| Package | 4-pin DIP, pin 1 marked by a dot or a notch |
| LED forward voltage (Vf) | about 1.2V typical, 1.4V max |
| LED forward current (If) | 5-20mA normal range, 20mA continuous max, 10mA is a good target |
| Collector-emitter voltage (VCEO) | 35V max |
| Collector current (IC) | 50mA max |
| Collector power dissipation | 150mW |
| Isolation voltage | 5000Vrms for 1 minute |
| Current transfer ratio (CTR) | 50% to 600% depending on rank (A 80-160%, B 130-260%, C 200-400%, D 300-600%) |
| Switching speed | about 4us rise time, about 3us fall time |
| Operating temperature | -30°C to +100°C |
Pinout
A single-channel module brings out five connections: two on the isolated field side, three on the Arduino Giga R1 WiFi side, and nothing crosses between them.
- IN+ : positive of the isolated input. Connect to the 24V (or 12V, or logic-level) signal you want to watch.
- IN- : negative of the isolated input. Connect to the ground of that circuit, never to the Arduino Giga R1 WiFi GND.
- VCC : module supply on the board side. Connect to the Arduino Giga R1 WiFi 3.3V pin.
- GND : board-side ground. Connect to the Arduino Giga R1 WiFi GND.
- OUT : isolated output. Connect to the Arduino Giga R1 WiFi digital input pin.
How It Works
The board side needs a pull-up so the pin has a defined level when the module's phototransistor is off. The Giga R1 WiFi has an internal pull-up on every digital pin, so no extra part is needed — a 10 kohm resistor up to 3.3V does the same job if you prefer an external one.
With the pull-up in place, an energised input turns the internal LED on, the phototransistor conducts, and the pin is pulled toward 0V. That is why the reading is usually inverted: a live 24V line does not give you a HIGH, it gives you a LOW. Some manufacturers invert it again on the module, so this is worth checking on your own part rather than assuming — there is a one-minute test for it further down this guide.
WARNING
Do NOT connect the ground of the 12V/24V circuit to the Arduino Giga R1 WiFi GND. The module's IN- terminal and its GND pin must stay on separate circuits — that separation is the entire protection. If you tie them together the module still switches and the code still works, so nothing looks wrong, but the isolation is gone and a fault on the industrial side now has a direct path into your board.
Which Optocoupler Module Do I Need?
There is no one module that covers every input voltage. The series resistor is already soldered on, and it is chosen for that variant's voltage, so the variant *is* the sizing decision. Measure what is on the wire, then read across:
| Signal On The Wire | Module To Buy |
|---|---|
| 24V DC panel signal - contactor coil, PLC output, fault lamp, field switch | 24V module |
| 12V DC signal - vehicle circuit, alarm output, 12V solenoid or lamp | 12V module |
| 5V or 3.3V logic signal - another microcontroller, a sensor board, a logic-level output | 3-5V module |
WARNING
A 12V module on a 24V line will be damaged. Doubling the voltage across a resistor sized for 12V pushes roughly double the intended current through the internal LED, and it will not survive that. This is the mistake to watch for in a real cabinet, because panels routinely carry 24V control circuits and 12V auxiliary circuits side by side, in identical-looking wiring, sometimes on the same terminal rail. Label your modules and confirm the voltage at the tap point with a meter before you land the wires — the modules themselves look the same from across the bench.
The opposite mistake is quieter but just as annoying: a 24V module fed from a 12V line underdrives its LED, so the channel may switch late, switch unreliably, or never switch at all. Nothing burns, and the wiring looks perfect, which is why it can cost an afternoon.
A 5V or 3.3V logic signal is worth isolating too, even though nothing on it can hurt a pin. Isolation is about breaking the ground path, not only about surviving a high voltage: two boards on separate supplies, sitting at opposite ends of a machine or with a motor drive between them, will not agree on where 0V is, and a long cable run through a noisy plant makes that disagreement worse. The low-voltage control board inside a mains-powered appliance is the same story on a smaller scale.
It is also worth being clear that current, not voltage, is what the PC817 input reacts to. A 24V supply able to deliver 20A is no harder on the module than one able to deliver 20mA, because the on-board resistor fixes the LED current either way. What the big supply changes is how much damage a wiring mistake elsewhere can do — which is itself another argument for keeping the two sides apart.
Wiring Diagram
Power the module from 3.3V, never from 5V. The Giga R1 WiFi is a 3.3V board and its GPIO pins are not 5V tolerant, so anything that can feed the input pin has to come from the 3.3V rail — the module's VCC terminal, and any external pull-up resistor you fit. Set it up that way and the pin never sees more than 3.3V, whether the isolated side is sitting at 12V or at 24V.
Wiring The PC817 Optocoupler Module
The field wires go into the screw terminals, the board side goes onto a header. There is no resistor to calculate and no maths to redo, because the variant you bought already carries the right one.

This image is created using Fritzing. Click to enlarge image
| Module Terminal | Connects To |
|---|---|
| IN+ | 24V/12V signal wire (or logic signal, on a 3-5V module) |
| IN- | Ground of that isolated circuit |
| VCC | Arduino Giga R1 WiFi 3.3V |
| GND | Arduino Giga R1 WiFi GND |
| OUT | Arduino Giga R1 WiFi Digital Pin 2 |
Digital pin 2 is a plain general-purpose pin on the Giga R1 WiFi — it is clear of the SPI bus on D11-D13, the I2C bus on D20/D21 and the Serial1 pins on D0/D1, so nothing else competes for it, and it carries the internal pull-up this circuit depends on. Any free digital pin works just as well; change one line in the code if you move it.
The module's on-board LED is genuinely useful here: it tells you the field side is energised before you have opened the Serial Monitor, which separates a wiring problem from a code problem in a couple of seconds.
Wiring The Module In Parallel With The Device To Monitor Whether It Is On Or Off
Panel work is rarely about reading a spare signal wire. It is about answering "is that contactor actually pulled in right now?" — and to answer that, the module's input goes across the device, in parallel with it, sitting on the same voltage the device itself sees.
WARNING
Never put the module in series with the device you are monitoring. In series, the full load current of the device — hundreds of milliamps for a contactor coil — would be forced through an internal LED rated for 20mA, and the module would be destroyed the instant the coil pulled in.
Take a 24V contactor coil in a machine panel as the worked example, with a 24V module on the bench. Every wire already in the cabinet stays exactly where it is, and you add two of your own: from the coil's switched positive terminal to IN+, and from the coil's 0V return terminal to IN-. Nothing is cut, unlanded or rerouted, and you fit no resistor of your own — the module already has it.
| Tap Point | Connects To |
|---|---|
| Coil switched (+) terminal | Module IN+ |
| Coil 0V return terminal | Module IN- |
| Clamp diode (inductive loads) | Reverse-parallel across IN+ and IN-, cathode to IN+ |
| Bleeder resistor (leaky outputs) | 4.7 kohm to 10 kohm across IN+ and IN- |
That tap draws about 10mA. Beside a contactor coil pulling several hundred milliamps — or a lamp, a valve or a small motor — it is nothing, so the device pulls in and drops out exactly as it always did. This is a read-only measurement of the panel, not a change to it.
Four things decide whether the tap behaves in a real cabinet:
- Polarity matters. The input side is an LED and conducts one way only. Swap IN+ and IN- and the channel simply never comes on, with no other symptom to go on. Check which side of the tap is positive before you land the wires.
- Inductive loads need a clamp diode. A contactor coil, relay, solenoid, valve or motor throws a large reverse-voltage spike every time it drops out, and the PC817 input LED is rated for only 6V reverse, so that spike can kill it. Fit an ordinary diode in reverse-parallel directly across the input terminals — diode cathode to IN+, diode anode to IN-. In normal operation it is reverse-biased and does nothing at all; on a spike it conducts and clamps the reverse voltage to well under a volt. A 1N4148 suits a small tap, a 1N4007 a heavier one. On a coil at the far end of a long run in a noisy plant this is not an optional refinement.
- Leaky solid-state outputs cause false readings. Plenty of PLC transistor outputs, triac outputs and LED-lamp drivers pass a small current even when they are switched off. Enough of that leakage partially lights the module's internal LED, and the board then reports the device as ON while it is plainly OFF — the symptom is a channel that never returns to its inactive state. The fix is a bleeder resistor of about 4.7 kohm to 10 kohm fitted in parallel with the module input, so the leakage runs through it instead of through the LED. On a panel full of solid-state outputs it is worth fitting one on every channel from the start rather than chasing a stuck reading later.
- DC only. The PC817 input is a single LED, so it responds to one polarity, and it is the wrong part for sensing an AC line — that needs an optocoupler whose input stage carries two back-to-back LEDs.
Because the tap sits across the coil rather than in the command path, the reading follows what the device is really doing. It picks up the moment a hardwired safety relay, a hand-off-auto switch or another controller drops the coil out behind your back, and just as usefully it shows you when a coil that was commanded on never energised at all. That is what turns this into run-time logging, fault alerting and a straight answer to "did it actually start?".
One honest limit: this tells you the device is energised, not that it is working. A seized motor or a burnt-out heater element still draws power from its coil circuit and will still read as ON.
Is Your Optocoupler Module Active HIGH Or Active LOW?
With the bare chip wired as this guide describes, the answer is settled physics: the pin has a pull-up, an energised input turns the phototransistor on, and the transistor drags the pin down. Signal present = LOW.
On a module, you have to check. Some manufacturers add an inverting stage on the board so the output follows the input instead — signal present = HIGH — and both kinds are sold under the same "PC817 optocoupler isolation module" description. Do not assume; the check takes less time than reading the listing.
The one-minute test:
- Wire the module as in the table above and upload the sketch from this guide, which prints the raw pin value alongside the decoded state.
- With nothing on IN+ and IN-, note the raw value printed.
- Apply the signal to the input terminals and note the raw value again.
- Whichever value appears while the signal is present is your module's active level. If that value is 0, your module is active-LOW; if it is 1, it is active-HIGH.
- Cross-check against the module's own status LED. That LED sits on the input side, so it lights whenever the field circuit is energised — if the LED is lit and the sketch still says OPEN, it is the polarity constant that is wrong, not your wiring.
The fix is one line. The sketch has a single constant, MODULE_ACTIVE_LOW, set to true by default because most modules behave that way. If your test says otherwise, change it to false and nothing else needs touching.
Optional: Building It From The Bare PC817 Chip
If you would rather build the channel from the raw four-pin chip, the only extra work is fitting the series resistor yourself — the bare chip has none inside.

- Pin 1 - Anode (A): to the signal, through the series resistor.
- Pin 2 - Cathode (K): to the ground of the isolated circuit, never to the Giga R1 WiFi GND.
- Pin 3 - Emitter (E): to the Arduino Giga R1 WiFi GND.
- Pin 4 - Collector (C): to the Arduino Giga R1 WiFi digital pin 2.
Size it with R = (Vin - 1.2) / 0.01, for about 10mA through the LED: 3.3V - 220 ohm, 5V - 390 ohm, 12V - 1 kohm, 24V - 2.2 kohm (use a 1/2W part at 24V; a 1/4W part is fine below that).
The parallel tap, the clamp diode, the bleeder resistor and the separate grounds all apply unchanged. The bare chip is always active-LOW, so leave MODULE_ACTIVE_LOW set to true — and re-size the resistor by hand if that channel ever moves from 12V to 24V.
How To Program
No library is needed for this project — it is plain digital input reading.
Set the pin on Arduino Giga R1 WiFi that connects to the module's OUT terminal:
Declare which way round your module switches:
Enable the internal pull-up:
Read the pin:
Decode the reading with that one constant:
Print the result:
Arduino Giga R1 WiFi Code - PC817 Optocoupler
The sketch below watches one isolated channel — in this example a 24V machine fault-lamp line through a 24V module — and prints the raw pin value together with the decoded state twice a second, with a millisecond timestamp. The raw value is what you use to run the active-HIGH / active-LOW test above.
Detailed Instructions
Follow these instructions step by step:
For initial Arduino Giga R1 WiFi setup, refer to the Arduino Giga R1 WiFi Getting Started guide before proceeding.
- Check that your module's variant matches the voltage you are about to feed it.
- Wire the components according to the provided diagram.
- Launch the Arduino IDE on your computer.
- Select the appropriate board and COM port.
- Copy the code above into a new sketch and upload it to the Arduino Giga R1 WiFi.
- Open the Serial Monitor and set it to 9600 baud.
- With the external signal off, check that the line reports OPEN.
- Energise the external signal and check that the report flips to ENERGISED.
- If it reports the opposite of what the module's status LED shows, set MODULE_ACTIVE_LOW to false and upload again.
Serial Monitor Output
If the output is stuck on ENERGISED with nothing on the input terminals, either something else is pulling the pin LOW — check GND and that INPUT_PULLUP was set — or your module is an active-HIGH one and the constant needs flipping. If it never leaves OPEN while the external line is clearly live, look at the module's status LED: unlit means the input side is not conducting, so check the variant against the line voltage and check that IN+ and IN- are not swapped.
Application Ideas
24V PLC output mirroring: Tap a digital output of an existing PLC with a 24V module and mirror it into the Giga R1 WiFi without disturbing the PLC's own wiring, then publish that state over the board's WiFi connection for remote monitoring.
Machine fault-lamp logging: Watch a 24V lamp line on a production machine and record when that fault indicator lights and for how long. Timestamped over a shift, that record often shows which fault comes first and which ones are only knock-on effects.
Contactor coil sensing: Detect whether a 24V contactor or solenoid coil is actually energised, rather than assuming it followed the command that was sent to it. Comparing commanded state against sensed state is a simple, effective way to catch a welded or failed contactor.
Long-run switch monitoring: Read a 24V switch at the far end of a long cable run in an electrically noisy plant. The higher signal voltage rides through induced noise that would swamp a 3.3V logic line, and the module converts it cleanly at the board end — a far more robust approach than stretching a limit switch input across the same distance.
Vehicle and generator circuit sensing: With a 12V module, monitor whether a 12V ignition, alarm output or generator run signal is live, while keeping the Giga R1 WiFi fully separated from an electrical system that produces heavy voltage spikes.
Logic-level handshake between boards: With a 3-5V module, watch a 5V output from another controller elsewhere on the machine without tying the two grounds together.
Challenge Yourself
Challenge: Change the printed labels to match your own equipment, and light the Giga R1 WiFi's built-in LED whenever the isolated input is active.
Challenge: Count how many times the external signal turns on, and measure how long it stays on each time, then print a running total on demand.
Challenge: Add software debouncing so a chattering contactor does not produce a burst of events, using the same debouncing idea as the button tutorial.
Challenge: Turn the sketch into a run-hour meter for one machine: accumulate the total time the contactor coil stays energised, and raise an alert if the coil is commanded on but never reads ENERGISED within two seconds.
Challenge: Make the sketch work out MODULE_ACTIVE_LOW for itself: sample the pin while you switch the field signal by hand a few times, then print which polarity it decided on.
Challenge: Use the isolated input to gate a relay output, so the Giga R1 WiFi only drives its own load while the monitored 24V circuit is live.
Challenge: Publish the isolated input state over the Giga R1 WiFi's wireless connection so a browser on the plant network can see the panel status live.