IMDHMIControls LibraryShop main store

How-to

Trigger a Raspberry Pi camera capture from CODESYS

PLC programming Published 5 min read

Short answer

Trigger the Pi camera from CODESYS by starting from the shipped Camera.project example, passing only the arguments (no raspistill prefix), and resetting the trigger Boolean on the done feedback. The JPEG lands in /root/ and comes out through the CODESYS file browser; stop the camera stream first, because the camera is a single-owner device.

On this page
  1. Start from the camera example in the package
  2. Wire the trigger into your own program
  3. Reset the trigger on the done feedback
  4. Example: why an unreset trigger yields one capture from two pulses
  5. Get the JPEG out through the CODESYS file browser
  6. Free the camera when the stream is holding it
  7. Questions asked

The mechanism already ships with your package: Camera.project in the CODESYS Control for Raspberry PI folder contains a working camera trigger, so start from it instead of writing the call from scratch. Pass only the arguments to the command block — the example supplies the program name itself — and reset the trigger Boolean when the block reports done, or you get exactly one capture and never another. The JPEG lands in /root/ on the Pi, a directory the runtime writes into and that only the CODESYS file browser shows without root access at the shell. This is how to wire the trigger into your program, recover the file to your PC, and free the camera when the stream example is holding it.

Start from the camera example in the package #

The package delivery drops its examples on the engineering PC, not on the Pi. Look under your Windows user folder in C:\Users\ for the folder CODESYS Control for Raspberry PI\2.1.0.0\ — the version in the folder name follows your package, so open the one your installation created. Inside you find Camera.project for still capture, CameraStream.project for the live stream, and RaspberryPI_en.pdf, which documents the stream setup.

Open Camera.project first and read how the command reaches the Linux side: note which block issues it, and whether the executable name and the arguments are separate inputs or one string. That reading answers the only structural question in the whole build; everything else follows the example's shape.

Wire the trigger into your own program #

  1. Copy the command block from Camera.project into your application unchanged.
  2. In the argument input, supply only the arguments, for example -o cam.jpg. The one deviation in the source case was typing raspistill ahead of the file name; when the block takes arguments only, that prefix breaks the call. If your block takes a single full command line instead, the prefix is correct — the example tells you which case you have.
  3. Keep the output name relative, as in the example. A relative name lands in the runtime working directory, /root/.
  4. Drive the trigger Boolean from process logic or an operator bit in your program.
  5. Download and start the application; connecting to the runtime and downloading follow the same steps as in Editing a CODESYS Raspberry Pi PROFINET controller project.

Keep this trigger out of any safety function: the Pi runtime is not a safety-rated controller, and the camera call has no deterministic response time.

Reset the trigger on the done feedback #

The capture call starts on the trigger. Left TRUE, it either produces one capture and then nothing — if the input is edge-sensitive — or relaunches the call every scan, piling up processes that contend for the camera. Check which input the example uses, then reset the Boolean when the block reports done.

Reset on the done or busy-cleared feedback, not on a timer: capture duration moves with the load on the Pi, so a fixed count of scans is not a deterministic reset. Resetting on done makes each pulse of the trigger produce exactly one image.

Example: why an unreset trigger yields one capture from two pulses #

Inputs, all from the source case: two pulses of the trigger Boolean from your process logic; the edge-triggered command block copied from the example; the done feedback that resets the trigger. Declare your own illustrative variables like this, all BOOL except the edge block, all initialized FALSE:

xCaptureTrigger : BOOL := FALSE;   // my illustrative name
xCaptureDone    : BOOL := FALSE;   // my illustrative name for the done feedback
etriggerPulse   : R_TRIG;          // my illustrative edge detector

The rule: a rising-edge input fires only on the FALSE-to-TRUE transition. With the reset in place, pulse one rises and the block fires — 1 capture start — then done returns TRUE and xCaptureTrigger drops to FALSE, so pulse two rises again and the block fires again. That is 2 pulses x 1 rising edge each = 2 capture starts.

Without the reset, pulse one fires — 1 capture start — and leaves xCaptureTrigger TRUE. Pulse two then arrives into an input that is already TRUE, so there is no FALSE-to-TRUE transition: 2 pulses − 1 missing FALSE transition = 1 capture start.

Result: the reset version captures twice; the unreset version captures once and reads as "worked after download, never again." The test that proves it on your machine: force xCaptureTrigger TRUE for one task cycle, watch xCaptureDone come back TRUE, and confirm the timestamp on cam.jpg in /root/ updates; repeat the pulse and confirm a second update. Decision: if your program captures exactly once, the trigger is stuck TRUE — reset it on the done feedback before changing anything else.

Get the JPEG out through the CODESYS file browser #

The runtime runs as root and writes relative file names into /root/. The CODESYS file browser lists that directory and shows cam.jpg there. A shell search such as find / -name cam.jpg 2>/dev/null can come back empty while the browser shows the file: /root/ is readable only by root, so an unprivileged find cannot descend into it, and the 2>/dev/null redirect discards the permission messages that would say so. Confirm with sudo find / -name cam.jpg, then ls -l /root/cam.jpg.

The file transfer dialog has two panes — PLC runtime on the right, your PC on the left — with the << and >> buttons between them. The arrows stay greyed until both ends hold a valid selection. In the right pane, open /root/ and single-click cam.jpg; a double-click does nothing. In the left pane, select a directory you can write to on the Windows machine; the root of C: is enough, and a user folder avoids permission problems on locked-down PCs. << then moves the file from the Pi to the PC, and >> goes the other way.

Free the camera when the stream is holding it #

The Pi camera is a single-owner device. CameraStream.project uses it continuously, and while the stream runs, raspistill reports an error — so the PLC still-capture trigger fails too until the stream releases the camera. Stop the stream from its web page at http://piIPAddress, or from the Pi shell:

cd RPi_Cam_Web_Interface/
./RPi_Cam_Web_Interface_Installer.sh stop

Run the same installer script with start to resume streaming. The stream example also needs extra packages installed on the Linux side; RaspberryPI_en.pdf lists them.

Never start the stream and a still capture at the same time from PLC logic. If the machine needs both in one cycle, sequence them: stop the stream, capture, restart the stream.

Questions asked

Can I retrieve the capture with an FTP client instead of the CODESYS file browser?

An FTP client logged in as a non-root user cannot see /root/, because that directory is readable only by root, which is the same restriction that hides the file from an unprivileged shell search. The CODESYS file browser reaches it because it goes through the runtime, which runs as root. To use FTP, run the session with root credentials or copy the file out of /root/ on the Pi first.

Does every capture overwrite the previous cam.jpg?

Yes. The example writes to the same relative name, so each capture replaces the last image in /root/. When you need a history, build a unique file name in the PLC before each trigger, from a counter or a timestamp, and pass it in the output argument instead of cam.jpg.

raspistill is missing on my newer Raspberry Pi OS build. What now?

Newer Raspberry Pi OS releases retire raspistill in favor of rpicam-still and rpicam-jpeg, which emulate many of the original tool's features. Check which command your OS provides and which name the example's command block actually passes. The fix is usually just substituting the new tool name in the argument input while keeping the rest of the call identical.

Keep reading

  • Explainer

    Editing a CODESYS Raspberry Pi PROFINET controller project

    A Raspberry Pi running CODESYS Control executes the compiled application you download to it; it cannot import GSDML, rebuild the project, or safely change I/O or OPC UA mappings on its own. Keep the CODESYS Development System on Windows as the editing tool, deploy builds to the Pi, and reserve the runtime on the controller for execution.

    PLC programming

  • How-to

    How to build ladder logic practice projects in STEP 7

    Get STEP 7 ladder practice projects from the vendor's own support portal and from the samples the installer already placed on your PC, not from a copy of a book's bundled disk. Then rebuild three circuits yourself: a start/stop latch with overload interlock, an on-delay timer sequence, and an up/down counter.

    PLC programming

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming