Raspberry Pi

How to set a fixed exposure time using libcamera-still on the Raspberry Pi

Use the --shutter argument. If no unit is given, the number represents microseconds of fixed exposure time

Example: --shutter 10000 means: Fixed 10 milliseconds (10000 microseconds) exposure.

Full example

libcamera-still -n 1 --width 4056 --height 3040 -o test.jpg --shutter 400

 

Posted by Uli Köhler in Raspberry Pi

How to enable Custom AWB white balance using libcamera-still on the Raspberry Pi

libcamera-still -n 1 --width 4056 --height 3040 -o test.jpg --awb custom --awbgains 0.9,2.0

In --awbgains 0.9,2.0 , 0.9 is the gain for the red channel and 2.0 is the gain for the blue channel.

In other words, if your image is:

  • too red -> decrease the first number
  • too blue -> decrease the second number
  • too yellow -> increase the second number (blue)
  • too green -> increase both numbers
  • etc
Posted by Uli Köhler in Raspberry Pi

How to use picamera2 to capture high-resolution image with fixed exposure time

This script will capture a single camera frame from a Raspberry Pi HQ camera with its maximum resolution of 4056x3040px and a fixed exposure time of 10.0ms (which appears to be the minimum exposure time the IMX477 sensor is capable of) and a fixed analog gain of 1.0. The resulting image is saved to Exposure10ms.png

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    camera.set_controls({"ExposureTime": 10000, "AnalogueGain": 1.0})
    camera.start()
    camera.capture_file("Exposure10ms.png")
    camera.stop()

It appears to be important to use camera.set_control() after camera.configure().

Posted by Uli Köhler in Raspberry Pi, Video

How to livestream Raspberry Pi camera using libcamera-vid & VLC

On the pi, run the following command

libcamera-vid -t0 --width 1920 --height 1080 --framerate 10 --nopreview --codec h264 --profile high --intra 5 --listen -o tcp://0.0.0.0:8494

On the computer where you want to play the livestream, run the following command

vlc tcp/h264://192.168.1.234:8494

where 192.168.1.234 is the IP address of the raspberry pi.

Internally, this uses a raw H.264 stream over TCP. This works best over the local network, but it will also work over VPN or the internet, if your networking setup allows it.

Original source: Github discussion

Posted by Uli Köhler in Raspberry Pi, Video

How to set exposure time using picamera2

The following code will capture a 640×480 PNG image into Exposure10ms.png, using a fixed exposure of 10ms:

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    camera.set_controls({"ExposureTime": 1000, "AnalogueGain": 1.0})
    camera.start()
    camera.capture_file("Exposure1ms.png")
    camera.stop()

Note that it is important to run camera.start() after camera.set_controls().

Posted by Uli Köhler in Python, Raspberry Pi

How to use picamera2 to capture Raspberry Pi HQ camera (IMX477) high resolution image

The following code will capture a single 4056x3056px image from a Raspberry Pi HQ camera using the IMX477 sensor into either a file or a numpy array. This is the maximum resolution supported by that camera.

Capturing to a file

This will capture a single frame to CameraTest.png. The PNG file will be quite large, around 15-25 Megabytes.

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    # Create high resolution still capture config
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    # Start camera with config
    camera.start()
    # Capture a single frame to CameraTest.png
    camera.capture_file("CameraTest.png")

Capturing to a numpy array

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    # Create high resolution still capture config
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    # Start capture
    camera.start()
    array = camera.capture_array("main")
    # TODO Do something with [array]
    print(array.shape)

Example output:

[0:55:52.878964095] [5768]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:55:52.913906171] [5769]  INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/imx477@1a to Unicam device /dev/media3 and ISP device /dev/media0
[0:55:52.913998855] [5769]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:55:52.921056014] [5768]  INFO Camera camera.cpp:1033 configuring streams: (0) 4056x3040-BGR888 (1) 4056x3040-SBGGR12_CSI2P
[0:55:52.922090144] [5769]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 4056x3040-SBGGR12_1X12 - Selected unicam format: 4056x3040-pBCC
(3040, 4056, 3)

Note that the NumPy array is compatible with Python OpenCV functions.

Posted by Uli Köhler in Python, Raspberry Pi

Raspberry Pi HQ camera (IMX477) maximum supported resolution

The maximum resolution of the Raspberry Pi HQ camera with the Sony IMX477 sensor is 4056x3040px.

You can use the libcamera-hello --list-cameras command in order to list all supported resolutions. The output for the IMX477 sensor is

Available cameras
-----------------
0 : imx477 [4056x3040] (/base/soc/i2c0mux/i2c@1/imx477@1a)
    Modes: 'SRGGB10_CSI2P' : 1332x990 [120.05 fps - (696, 528)/2664x1980 crop]
           'SRGGB12_CSI2P' : 2028x1080 [50.03 fps - (0, 440)/4056x2160 crop]
                             2028x1520 [40.01 fps - (0, 0)/4056x3040 crop]
                             4056x3040 [10.00 fps - (0, 0)/4056x3040 crop]
Posted by Uli Köhler in Raspberry Pi

libcamera-hello output for the Raspberry Pi HQ camera (IMX477 sensor)

The following output of libcamera-hello --list-cameras is for the IMX477 which is the Raspberry Pi HQ camera:

Available cameras
-----------------
0 : imx477 [4056x3040] (/base/soc/i2c0mux/i2c@1/imx477@1a)
    Modes: 'SRGGB10_CSI2P' : 1332x990 [120.05 fps - (696, 528)/2664x1980 crop]
           'SRGGB12_CSI2P' : 2028x1080 [50.03 fps - (0, 440)/4056x2160 crop]
                             2028x1520 [40.01 fps - (0, 0)/4056x3040 crop]
                             4056x3040 [10.00 fps - (0, 0)/4056x3040 crop]

 

Posted by Uli Köhler in Raspberry Pi

How to install picamera2 Python library on Raspberry Pi

You can install picamera2 on Raspberry Pi OS / Raspbian using

sudo apt -y install python3-picamera2

Tested on Raspberry Pi OS bullseye

Posted by Uli Köhler in Raspberry Pi

How to capture image as NumPy array using PiCamera2

This will capture a raspberry pi camera image as numpy array.

The default size that will be used is 640x480px

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    camera.start()
    time.sleep(1)
    array = camera.capture_array("main")
    # TODO Do something with array
    print(array.shape)

Example output:

[0:27:57.224504277] [3117]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:27:57.258472502] [3118]  INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/imx477@1a to Unicam device /dev/media3 and ISP device /dev/media0
[0:27:57.258611296] [3118]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:27:57.264790966] [3117]  INFO Camera camera.cpp:1033 configuring streams: (0) 640x480-XBGR8888 (1) 2028x1520-SBGGR12_CSI2P
[0:27:57.265395993] [3118]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 2028x1520-SBGGR12_1X12 - Selected unicam format: 2028x1520-pBCC
(480, 640, 4)

 

Posted by Uli Köhler in Audio/Video, Raspberry Pi

How to install uvcdynctrl on Raspberry Pi OS Bullseye

Problem

You are trying to install uvcdynctrl on Raspberry Pi OS bullseye version using

sudo apt -y install uvcdynctrl

the system can’t find the package:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package uvcdynctrl

Solution

The uvcdynctrl package is not available in the standard Raspberry Pi OS repository, but only in the bullseye-backports repository.

In order to solve the problem, activate the backports repository:

Add the following lines at the end of /etc/apt/sources.list:

deb http://deb.debian.org/debian bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free

After that, run

sudo apt update

and finally, try to install uvcdynctrl again:

sudo apt -y install uvcdynctrl

 

 

Posted by Uli Köhler in Linux, Raspberry Pi

How to activate backports repository on Raspberry Pi OS (Raspbian)

Add the following lines at the end of /etc/apt/sources.list:

deb http://deb.debian.org/debian bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free

After that, run

sudo apt update

 

Posted by Uli Köhler in Linux, Raspberry Pi

Systemd service to use a DS3231 RTC on the Raspberry Pi

The following systemd service will automatically. See this guide for more information on the setup and ensure sudo i2cdetect -y 1 detects the RTC with address 0x68.

This is an automatic service installation & enable script based on A simple systemd service autoinstall script . This script will automatically enable the service on boot:

#!/bin/bash
# This script installs and enables/starts a systemd service
# It also installs the service file
export NAME=ConfigureRTC

cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}

[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# Enable and start service
systemctl enable --now ${NAME}.service

This is just the systemd service:

[Unit]
Description=ConfigureRTC

[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always

[Install]
WantedBy=multi-user.target

 

Posted by Uli Köhler in Linux, Raspberry Pi

How to enable or disable NTP time synchronization on the Raspberry Pi

Disable NTP:

sudo systemctl disable --now systemd-timesyncd

Enable NTP:

sudo systemctl enable --now systemd-timesyncd

Verifying if NTP is active

You can verify if NTP is active or not by running

timedatectl

Then look for these lines:

System clock synchronized: yes
NTP service: active

System clock synchronized will tell you if the NTP service has successfully synchronized the system time to a NTP time server: yes if synchronized, no if not synchronized.

NTP service will tell you if the NTP service is running, i.e. if it is trying to synchronize the system time to a NTP time server: active if running, inactive when not running

Output with NTP active:

               Local time: Tue 2023-03-14 16:49:28 CET
           Universal time: Tue 2023-03-14 15:49:28 UTC
                 RTC time: Tue 2023-03-14 15:49:28
                Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Output with NTP inactive:

               Local time: Tue 2023-03-14 16:48:01 CET
           Universal time: Tue 2023-03-14 15:48:01 UTC
                 RTC time: Tue 2023-03-14 15:48:01
                Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

 

Posted by Uli Köhler in Linux, Raspberry Pi

How to enable I2C port on the Raspberry Pi using raspi-config

sudo raspi-config nonint do_i2c 0

Now load the relevant modules:

sudo modprobe "i2c-bcm2835"
sudo modprobe "i2c-dev"
sudo modprobe "rtc-ds1307"

and now check if the I2C device file exists using stat /dev/i2c-1:

$ stat /dev/i2c-1
  File: /dev/i2c-1
  Size: 0               Blocks: 0          IO Block: 4096   character special file
Device: 5h/5d   Inode: 169         Links: 1     Device type: 59,1
Access: (0660/crw-rw----)  Uid: (    0/    root)   Gid: (  998/     i2c)
Access: 2023-03-14 16:23:06.643999999 +0100
Modify: 2023-03-14 16:23:06.643999999 +0100
Change: 2023-03-14 16:23:06.643999999 +0100
 Birth: -

If you instead see

ls: cannot access '/dev/i2c-1': No such file or directory

either the driver is not loaded properly or I2C is disabled. Try rebooting the system and repeating the commands above and possibly checking dmesg for any error messages.

Posted by Uli Köhler in Raspberry Pi

How to fix Raspberry Pi i2cdetect: command not found

Problem:

When trying to detect I2C devices on the Raspberry Pi (Raspbian) using i2cdetect, you see the following error:

$ i2cdetect
bash: i2cdetect: command not found

Solution:

Install i2c-tools using

sudo apt -y install i2c-tools

After installing i2c-tools , you can use i2cdetect and other related tools such as i2cget.

Posted by Uli Köhler in Embedded, Linux, Raspberry Pi

VirtualHere USB server & systemd autostart on Raspberry Pi in 15 seconds

The following script will download the VirtualHere USB server for the Raspberry Pi (generic version for ARM) and create a systemd service called vhusbd.service.

In case you are not using the Raspberry Pi, see the VirtualHere server download page to obtain.

#!/bin/bash
# This script installs and enables/starts the VirtualHere USB server
wget -O /usr/bin/vhusbd https://www.virtualhere.com/sites/default/files/usbserver/vhusbdarm
chmod a+x /usr/bin/vhusbd

cat >/etc/systemd/system/vhusbd.service <<EOF
[Unit]
Description=vhusbd
After=network-online.target

[Service]
Restart=always
User=root
Group=root
ExecStart=/usr/bin/vhusbd

[Install]
WantedBy=multi-user.target
EOF

# Enable and start service
systemctl enable --now vhusbd.service

 

Posted by Uli Köhler in Embedded, Raspberry Pi

How to take a remote screenshot on the Raspberry Pi

You can login to your Raspi using ssh -CX pi@IPADDRESS and then run

DISPLAY=:0 scrot screenshot.png

to take a screenshot of the display that is currently attached. After that, use

feh screenshot.png

(due to ssh -CX this will display the image locally on your Linux desktop) or copyscreenshot.png to your local computer using scprsyncWinSCP or any other tool.

This is useful for debugging what is happening on your display.

Posted by Uli Köhler in Raspberry Pi

Raspberry Pi libcamera VLC recording to H.264 (1920×1080)

On the Pi, run

libcamera-vid -t 0 --width 1920 --height 1080 --codec h264 -o out.h264

This will record Full-HD video (1920×1080) to out.h264

Posted by Uli Köhler in Audio/Video, Raspberry Pi

How to list available cameras on Raspberry Pi (libcamera)

Use this command to list all available cameras:

libcamera-still --list-cameras

Example output:

$ libcamera-still --list-cameras
Available cameras
-----------------
0 : imx477 [4056x3040] (/base/soc/i2c0mux/i2c@1/imx477@1a)
    Modes: 'SRGGB10_CSI2P' : 1332x990 [120.05 fps - (696, 528)/2664x1980 crop]
           'SRGGB12_CSI2P' : 2028x1080 [50.03 fps - (0, 440)/4056x2160 crop]
                             2028x1520 [40.01 fps - (0, 0)/4056x3040 crop]
                             4056x3040 [10.00 fps - (0, 0)/4056x3040 crop]

 

Posted by Uli Köhler in Audio/Video, Raspberry Pi