Easy to connect cameras even on an industrial Raspberry Pi.

Many people may imagine a large machine and budget when they hear of the introduction of an image-based identification device for industrial use.
Aside from custom programs, if the hardware is an industrial Raspi such as PL-R4, it can be built inexpensively by utilizing a general-purpose camera and various sensors.
It does not seem unreasonable for small-scale applications.

The product PL-R4 ComputeModule4 (“RPI CM4”) being tested is almost identical to the hobby-use Raspberry Pi 4 (“Pi 4”). We were able to try it with the usual USB-connected webcam and a camera module that is also available from official sources.

We connected a camera to see the difference, including settings unique to industrial Raspi.

Cameras that can be connected

Cameras that can be connected

Like a regular Raspi, the industrial Raspi can be fitted with a variety of cameras to capture still and moving images.

Cameras that connect to the CSI port on the board are sold as camera modules.
A generic webcam with USB connection can also be used.
I can choose from several products according to the quality I need.

  • CSI
  • USB

The camera module is officially available from the Raspberry Pi Foundation.

Among them, the HQ camera can use telephoto and wide-angle lenses and has very high image quality for a camera that can be connected to the Raspberry Pi. (12.9 megapixels)
It would be especially valuable for industrial applications.

HQ Camera
Camera Module

The cameras tested in this case were, from left to right, a high quality HQ camera (official, tripod sold separately), a Camera Module V3 (official, case sold separately), and a Logitech C270n webcam.

この他にも多種多様なカメラが接続できます。(赤外線カメラ、熱感知用途、屋外用途など)
使用環境に適したカメラが選べると思います。

Easy-to-plug-in webcam

The handy USB-connected webcam has probably been used by many people since the Corona disaster.
Just like Windows and Mac, a USB-connected camera can be used on the Raspberry Pi without modification. This is just like a standard PC in this area.

This is also true for industrial Raspi.
Even PL-R4 can be operated in the desktop environment (GUI) and command environment (CUI) simply by connecting to USB.

All you have to do is select the software to use the camera with the Raspberry Pi OS.

Guvcview

Guvcview is recommended in a GUI environment.

It is divided into two windows, one for control and one for display.
Both still and moving images work effortlessly with the touch of a button.

Gnvcview

I just installed Guvcview and it recognized the webcam.
Nothing is set.

sudo apt install guvcview

As with the Pi 4 in general, Guvcview was easy on the RPI CM4 raspi if you use a USB-connected camera.

USB-connected webcams for use with Raspberry Pi are more likely to work with less modern products.
The C270n we tested is a model released in 2019; it would be useful to have one.

Camera Module Connection

Any official Raspberry Pi camera module will be recognized by simply plugging in the CSI cable, just like a USB-connected webcam.
This also works with the libcamera command without any special settings.

However, third-party camera modules are not automatically recognized, so the configuration file must be rewritten a little.

In addition to rewriting the configuration file, the PiLink Corporation’s PL-R4 used in this trial also requires configuration on the hardware side to use the CSI port.

  • Switch changes in hardware
  • Edit config.txt
  • Prepare dt-blob.bin file to be loaded at startup
connect CSI cable

Note the orientation of the flat cable that connects to the CSI port. In the photo, the back side is the blue strip and the front side is the terminal side.

The product we tested is a USB type (IP20), which has a slit in the panel to pull out the camera cable.

slit for CSI calbe

Handle the flat cable and the terminal part at the end with care to avoid disconnection.

Hardware configuration changes

In industrial Raspi, as much as you make it multifunctional, you need to switch settings as hardware.

There are several switches on the board, and these DIP switches need to be set appropriately.

This area differs from hobby use.
Please note that the settings are different for regular Pi 4 and Pi 5.

I will explain with the PL-R4 that we use.

DIP switch

In addition to the hardware changes, make the necessary modifications to /boot/config.txt.

For home use Pi 4, for example, if you are using an official camera module, it will be recognized simply by connecting it without any modification.
config.txtThe display_auto_detect=1is valid for official camera modules.

Third-party camera modules and industrial Raspi like this one require commenting out automatic recognition and specifying the camera type.

/boot/config.txtExample description of

# Automatically load overlays for detected DSI displays
#display_auto_detect=1
[all]
dtoverlay=imx708

Note that the name to be specified varies depending on the camera module.

Name by image sensor:

| Official Camera Module | Model | Name |
| :——–: | :-: | :—-: |
| | V1 | OV5647 |
| | V2 | IMX219 |
| | HQ | IMX477 |
| | GS | IMX296 |
| | V3 | IMX708 |
This time we used official V3 and also official HQ camera So, IMX708 and IMX477 are specified respectively.

他にもサードパーティー製カメラモジュールもあり、IMX290、IMX327、IMX378、IMX519、OV9281と分けられています。
CSI接続カメラの製品詳細などで、イメージセンサー名として記載されています。指定する名称は説明書や購入先で確認してください。

In addition to this, PL-R4 will need to modify more than just the name of the model, and will eventually change the following areas

sudo nano /boot/config.txt
# 自動認識をコメントアウト
# Automatically load overlays for detected cameras
#camera_auto_detect=1

#ディスプレイの自動認識をコメントアウト
# Automatically load overlays for detected DSI displays
#display_auto_detect=1

# ドライバの有効化
# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2

#WD/CSIに関する記述をコメントアウト
#dtparam=i2c0_baudrate=400000
#dtoverlay=i2c0,pins_0_1
#WD/CSI select
#dtparam=i2c1_baudrate=400000
#dtoverlay=i2c1,pins_44_45

# Camera Module
dtoverlay=imx708

Each description is pre-existing. It is almost always just a matter of enabling/disabling the comment.

In addition to this, if watchdog is set by the model, comment out the relevant section.

sudo nano /etc/rc.local
# ウォッチドッグコメントアウト
#WD
#python /home/pi/wdrst.py

In bookworm, which is handled by Pi 5, the location of config.txtis different to begin with. (bookworm: /boot/firmware/config.txt)
This article is written in the bullseye environment of the Pi 4 series /boot/config.txt.

Correction of dt-blob.bin

For PL-R4 with RPI CM4 and I/O board, further modifications are required.

You may not be familiar with it, but it requires a modification of dt-blob.binused by GPUs.

The reason for this is that the pin numbers must be changed to assign the devices connected to the GPIOs to other functions as well.

Please refer to the official documentation for more information.
(Official document: https://www.raspberrypi.com/documentation/computers/compute-module.html#device-tree)

Here are the steps to correct the problem for PL-R4 users.

minimal-cm-dt-blob.dtsfrom the official documentation above.
This will be defined by editing it.

Edit from line 271:

          pin_define@ID_SDA {
             type = "internal";
             number = <44>;
          };
          pin_define@ID_SCL {
             type = "internal";
             number = <45>;
          };
          pin_define@CAMERA_0_I2C_PORT {
             type = "internal";
             number = <1>;
          }; 

Then compile. The compiler is already installed from the beginning. (It was ok even if I got a warning)

sudo dtc -I dts -O dtb -o minimal-cm-dt-blob.dtb minimal-cm-dt-blob.dts

Finally, copy and place it in the /boot directory with the file name dt-blob.binand you are done. It will take effect after reboot.

sudo cp minimal-cm-dt-blob.dtb /boot/dt-blob.bin

This area is a bit of a more difficult task. It can be said that the role can be changed for the sake of it.

As mentioned above, three main modifications were made.

  • Switching as hardware
  • Modification of dt-blob.bin
  • Modification of config.txt

After setting up, try the following commands to check after rebooting.

Use by command (CSI connection camera module)

Camera modules connected to the CSI port can use the libcamera command and the Picamera2 module handled by Python. These two modules are preinstalled in bullseye or later.

We are talking about the OS version bullseye, in Pi 5 bookworm it is the rpicam command. It’s a bit complicated.
Be careful when dealing with camera modules on earlier Raspi models and OS.

raspi-configThe Legacy camera mode, which can be set from the ” Legacy camera mode ” menu, can be left disabled.

To explain somewhat zacchae, this is an option required for Pi 3 to handle the raspistill command.
The relationship is as follows

  • Pi3 series (buster) raspistill command
  • Pi4 series (bullseye) libcamera command
  • Pi5 series (bookworm) rpicam command

Raspberry Pi and camera modules have a bit of a complicated history.

Raspberry Pi OS Bookworm has renamed the camera capture application from libcamera-* to rpicam-*. The name libcamera-* will continue to be used in Raspberry Pi OS versions before Bookworm.

The official information is also basically explained in a Pi 5 and bookworm environment, with slightly earlier information valid for bullseye and below.
There is a lot of information available on the internet, but information around 2022 would be helpful.

Command to check connection

Once the camera module is connected, check to see if the camera is recognized.

Use the libcamera command to display the list of cameras.
If there is an error here, check if the CSI connection cable is not plugged in properly or if there is a mistake in the section you added to config.txt, etc.

libcamera-hello --list-cameras

If it is recognized, it should appear as follows.

Available cameras
-----------------
0 : imx708 [4608x2592] (/base/soc/i2c0mux/i2c@1/imx708@1a)
    Modes: 'SRGGB10_CSI2P' : 1536x864 [120.13 fps - (768, 432)/3072x1728 crop]
                             2304x1296 [56.03 fps - (0, 0)/4608x2592 crop]
                             4608x2592 [14.35 fps - (0, 0)/4608x2592 crop]

You can also try looking for errors at imx*, which is the name of the camera. (e.g. imx219)

dmesg | grep imx*

If it is not working at this point, check the flat cable plugged into the CSI port. It may not be plugged in properly.

libcamera command

The libcamera command has many options and parameters.

If you simply want to take a still image, do the following

libcamera-jpeg -o test1.jpg -t 2000 --width 640 --height 480

If you want to display the image as if it were in live view, do the following

libcamera-hello -t 0

The commands are divided into still image and video. See --helpfor options, respectively.

libcamera-jpeg
libcamera-raw
libcamera-still
libcamera-vid 

It can also be displayed via VNC.

Camera modules handled in Python

The camera module is Picamera2 in Python, which is already installed if the OS is bullseye or higher.

Sample program:

from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(2)
picam2.capture_file("test.jpg")

Official website: https://www.raspberrypi.com/documentation/computers/camera_software.html#rpicam-apps

Use on command (USB-connected webcam)

The USB-connected webcam uses an application called fswebcam that allows command-based control.

sudo apt install fswebcam

インストールしてから、ユーザーをvideoグループに加えます。今ログインしているユーザー名に置き換えて読んでください。
これでsudo権限のエラーは出なくなります。

sudo usermod -a -G video ユーザ名

The basic format is this.

fswebcam test.jpg

The default resolution is a small 352×288.

Let’s run it again, optionally specifying the resolution.

fswebcam -r 1280x720 test2.jpg

By default, a bar appears at the bottom of the captured image and the date and time are composited in the lower right corner.
If this is garbled, installing Japanese fonts (noto-fonts) will resolve the problem.

sudo apt install fonts-noto-cjk

If you want to stop the bar-like output at the bottom of the image, use the --no-banneroption to make it disappear.
See fswebcam --helpfor options.

Date File Name

fswebcam does not provide file naming functions such as date or sequential numbering. The official version introduces a shell script to use instead.

Shell scripts are simple and require only three lines of code.
Create a new sh file in the appropriate location as follows, add three lines, and give execution permission at the end to use it.

nano webcam.sh

The script example is to run the command in fswebcam with a resolution of 1280×720, with the date in the filename.

#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H-%M-%S")
fswebcam -r 1280x720 --no-banner $DATE.jpg

Execution rights are granted by chmod.

chmod +x webcam.sh

If you are moving to the same directory, you can execute the following shell command to take a still image

./webcam.sh

Reading the terminal output after execution confirms that the file name is now the date and time.

--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Disabling banner.
Writing JPEG image to '2024-09-23_13-27-18.jpg'.

A standard webcam with a USB connection can be easily used by simply plugging it in as long as an application is prepared.

Use ffmpeg for USB-connected webcams

Next is a video.
With Guvcview, as mentioned earlier, you only need a single button on the GUI screen, but to execute the command requires a separate application.

A CSI-connected camera module will also have a preinstalled command to record video, but a USB-connected webcam can be substituted by using the ffmpeg command.

Before executing a command, there are a few things to check.
The command requires parameters to be specified in the options, so be sure to know the range supported by the camera you are using.

This is to avoid wrong parameters, since different cameras have different performance.

For the camera C270 I used, I found that frame rate 30 is only compatible with YUYV (YUYV 4:2:2) format, which is 640x or smaller. It is a bit small.

The MJPG (Motion-JPEG, compressed) format also supported 1270x.

v4l2-ctl -d /dev/video0 --list-formats-ext

‘YUYV’ (YUYV 4:2:2)
Size: Discrete 640×360
Interval: Discrete 0.033s (30.000 fps)
‘MJPG’ (Motion-JPEG, compressed)
Size: Discrete 1280×720
Interval Interval: Discrete 0.033s (30.000 fps)

You can change this setting with the v4l2-ctl command.
You can change it by specifying a value for the c option. (e.g. -c <ctrl>=<val> )

For more information, please check the list of available controls.

v4l2-ctl -d /dev/video0 --list-ctrls-menu

Video recording with ffmpeg command

ffmpegを使うには、様々なオプションを付けて実行することになります。
ffmpegはラズパイだけに限った話ではありません。詳しくは別途、ffmpegを調べてください。

The following commands are the options that could be recorded with C270.

The camera has a microphone attached to it, so it can acquire audio together with the camera. The sensitivity is high and the recording is good.
The output file name, out.mkv, is specified at the end of the options.

ffmpeg -f alsa -i pulse -f v4l2 -input_format mjpeg -video_size 1280x720 -framerate 30 -i /dev/video0 -c:v copy -c:a libopus -b:a 192k "out.mkv"

I won’t go into the details, but since it depends on the camera, adjust the parameters according to the video size and frame rate supported by the camera.
The options are also specified differently depending on the presence or absence of a microphone.

Ctrl + C to stop recording.

A USB-connected webcam could be used on an industrial Raspi in the same way as for hobby use.
This is easy.

CSI camera module and USB connection camera for industrial applications

Industrial Raspi can also connect a camera via USB or use a camera module with a CSI port.
The need for cameras along with various sensors is also increasing in the industrial sites where they are used.

The following are some common examples.

  • I want to detect and identify people.
  • I want them to detect heat changes.
  • I want to be able to determine the shape of the produce.
  • I want to load a QR code.
  • I want to acquire simple surveillance camera footage.

The industrial Raspi PL-R4 required I/O board switching and configuration changes, unlike the home-use Pi 4.
Otherwise, it can be used in the same way as Pi 4.

The industrial Raspi is a low-cost device that can be deployed as hardware.
Unlike home use, low power remains the same even when equipped with robust cases and connectors.

Since the system can be multifunctional, including cameras and sensors and system linkage, there are advantages to implementing this system regardless of its scale.


Article contributed by Raspida

Raspberry Pi information site that even non-engineers can enjoy using raspida.com a Raspberry Pi information site that even non-engineers can enjoy and handle. He contributes to the PiLink site about the Raspberry Pi for industrial use.