Troubleshooting
Why Raspberry Pi GPIO inputs read TRUE in CODESYS ladder
Short answer
The ladder is fine: a normally open contact passes power whenever its variable is TRUE, and a Pi input left open with a pull-up sits at logic high, so the variable is TRUE all day. Match the pin's pull to the switch wiring, invert active-low inputs in the rung, and prove the pin voltage actually moves before trusting the online display.
On this page
- Why is the contact TRUE when nothing is wired to the pin?
- Does the normally open contact configure the pin?
- Which pull-up or pull-down should the input use?
- How do you invert an active-low input in the rung?
- Why is the input still TRUE after you set the pull?
- How do you test one input from pin to coil?
- Questions asked
Your rung is doing exactly what IEC 61131-3 requires: the normally open contact conducts whenever its variable is TRUE, and the coil follows the rung. The variable is TRUE because the pin behind it sits at logic high — a Raspberry Pi input with nothing connected takes its level from whatever pull is active, and a pulled-up open pin reads high continuously. Nothing in the contact symbol configures the pin. The fix is to decide the bias on purpose, invert the active-low inputs at the mapping edge, and then prove the chain from pin to coil with a meter. Here is the diagnosis, the two wiring decisions with the arithmetic, and the test order.
Why is the contact TRUE when nothing is wired to the pin? #
Because the open pin has no circuit holding it low: its level comes from the active pull, and a pull-up holds an open input at logic high, so the mapped variable is TRUE and your contact passes power continuously.
A CMOS input with no pull and no external circuit is floating. Leakage and stray coupling set the gate voltage, so the pin can sit high, sit low, or chatter, and touching the wire changes it — which is why 'always TRUE' and 'flickers when touched' are one fault, not two. Wherever the board's default pull or the driver's configuration leaves a pull-up on an open input, that pin reads high and the variable follows it. The ladder is reporting the pin honestly; the pin simply has nothing defined to report.
Does the normally open contact configure the pin? #
No: under IEC 61131-3 a normally open contact only tests a Boolean variable — TRUE passes power flow, FALSE blocks it — and the coil writes the rung result; nothing in the instruction touches pull, direction or level at the physical pin.
The TRUE shown beside the contact in the online view is the runtime's current value of that variable, not a voltmeter on the header. A true contact driving a true coil is correct execution even when the input side is wrong, so swapping the contact type to troubleshoot this chases the wrong layer. Change the bias at the pin and the meaning at the mapping, and the contact then has something real to report.
Which pull-up or pull-down should the input use? #
Use a pull-up with the switch wired from the pin to ground — open reads high, pressed reads low — or a pull-down with the switch from the pin to the logic rail; pick the pull-up for plain switches and make the pressed state the low one.
The internal pulls are weak and their defaults differ pin by pin, so for a switch that lives in a cabinet, add your own resistor. Worked example with a pushbutton: a 10 kΩ resistor from the input to the 3.3 V rail and the button from the input to ground. Released, no current flows through the button and the pin sits at the rail voltage, so the raw variable is TRUE. Pressed, the button grounds the pin, the raw variable is FALSE, and the pull-up passes 3.3 V ÷ 10 000 Ω = 0.33 mA — harmless for both the pin and the button.
| Button state | Pin voltage | Raw variable | Rung with a normally closed contact |
|---|---|---|---|
| Released | 3.3 V | TRUE | no continuity, coil off |
| Pressed | 0 V | FALSE | continuity, coil on |
The second convention suits signals that arrive as a clean high: an opto-isolated interface output, for example, wired from the pin to the rail with a pull-down to ground. Open reads FALSE, active reads TRUE, and the normally open contact needs no inversion. Whichever you choose, make the internal pull agree with the external resistor or disable it, so the two do not fight. The same bias-first diagnosis applies on small microcontrollers; a pull-up resistor fix for a PIC input reading always HIGH shows the identical check on that hardware.
How do you invert an active-low input in the rung? #
Keep the raw variable untouched and derive a pressed-true signal with a NOT — in ladder, a normally closed contact of the raw input — so the wiring stays active-low while the rest of the program runs in positive logic.
In structured text, with example variables of my own: xStartRaw is the variable mapped to the input channel and xStart is what the sequence uses.
xStartRaw : BOOL := FALSE; // mapped to the GPIO input channel
xStart : BOOL := FALSE; // TRUE only while the button is pressed
xStart := NOT xStartRaw; // runs every scan
Prove it on one press. Released, the pin is high, xStartRaw reads TRUE and xStart reads FALSE; pressed, xStartRaw drops to FALSE, xStart goes TRUE, and a rung with a normally open contact of xStart energizes exactly for the duration of the press. Invert once, at that edge, and never scatter negations through the sequence logic.
Why is the input still TRUE after you set the pull? #
Because something above the pin still feeds TRUE: the contact reads a different variable than the mapped one, a force holds it, another routine writes it, the channel is configured as an output, or the online view is not the running application.
Work down this list:
- Numbering: the header's physical pin number and the GPIO identifier are different systems. Find the wired pin in the board pinout and use that same identifier in the device configuration.
- Direction: the channel must be configured as an input; a pin configured as an output drives its level instead of measuring the switch.
- Mapping: exactly one variable sits on the channel, and it is the variable on the contact. The full procedure to map CODESYS I/O for ladder and structured text use covers the tabs involved. A variable that never moves at all points at the mapping itself, the same gap behind why CODESYS variables do not map to Neuron outputs like GPIO.
- Forces and other writers: clear every force, then cross-reference the variable for a second writer or a retained initializer.
- Runtime state: be logged in, application running, no error on the GPIO device; a stopped or offline display proves nothing about the pin.
How do you test one input from pin to coil? #
With the downstream equipment inhibited, operate one switch and watch three things at once: the pin voltage on a meter, the mapped variable online, and the coil — all three must change together, once per press.
- Inhibit or disconnect whatever the coil energizes before changing any input state. The pins are logic-level inputs, not 24 V field inputs; field signals reach them only through an interface, the stage that how discrete PLC I/O modules isolate and switch field signals describes, and the permitted voltage range is in the board documentation.
- Record the wired pin: connector position and GPIO identifier, then confirm both point at the channel in the device configuration.
- Read back the channel: direction input, and the pull you set actually active.
- Meter from the pin to the board's ground. With the pull-up wiring, released sits at the rail and pressed falls to 0 V within millivolts.
- Watch the layers in order — device input indication, mapped variable, contact, coil — and confirm each follows the pin on the same press.
- Read the fault at the first layer that fails to follow: the meter moves but the variable does not means configuration or mapping; the variable moves but the coil does not means rung logic or a second writer; the meter never moves means the signal circuit or the bias.
The decisions, then: a switch to ground with a pull-up and a normally closed contact for plain buttons; a switch to the rail with a pull-down and a normally open contact for interface outputs that idle low and drive high; and in both cases the meter verdict comes before any ladder edit.
Questions asked
Can I wire a 24 V sensor directly to a Pi GPIO input?
No. The pins are 3.3 V logic with no isolation, and 24 V on a pin destroys it; the shared ground also becomes a shock and noise path into the board. Route the sensor through an opto-isolated input interface or a proper digital input module, and let that interface's clean contact or open-collector output drive the pin within its documented limits.
Why does the input flicker when I touch the wire?
Because a floating input is an antenna. Your hand couples mains hum and static into the pin, the voltage wanders across the logic threshold, and the variable chatters between TRUE and FALSE. Enable the pull-up or add the external resistor and the flicker stops; if it still flickers with a defined bias, the signal circuit is missing its reference ground.
Is the internal pull-up enough, or do I need an external resistor?
On the desk, the internal pull is enough to define the level. Inside a cabinet, use an external 10 kOhm resistor: it holds the level against cable leakage and noise, it does not depend on per-pin defaults or on what the driver happens to enable, and a meter can verify it with the power off.
Why did every pin read TRUE instead of a mix of states?
Unconnected pins share whatever bias is left on them, so a whole bank settles into the same state; identical values across every channel are a bias signature, not evidence of a driver fault. Bias one pin properly and press its switch: if that single channel now follows the button, the driver and mapping are fine and the remaining pins only need the same treatment.