Raspberry Pi 5 Camera Control
This section describes how to run the camera control demo script for the Sony-EV9520L camera on the Raspberry Pi 5. The control script communicates with the camera over I2C via the SC16IS752 UART bridge and allows sending serial commands (VISCA protocol) during live video streaming.
Download the Control Script
Download the control_ev9520l.py script on your Raspberry Pi 5 or copy the full code below.
Download Link: control_ev9520l.py
Note:
If file is not able to download, go through Full Script section below.
Install Dependencies on Raspberry Pi 5
Before running the script, install the required libraries. Since Debian 13 (Trixie) enforces a PEP 668 externally managed environment, you can install the packages using apt or use the --break-system-packages flag with pip3.
Update Package List
sudo apt updateInstall pip3
sudo apt install python3-pipDependency 1 — smbus2
pip3 install smbus2 --break-system-packages # for i2c communicationDependency 2 — pyserial
pip3 install pyserial --break-system-packages # for serial communicationI2C Interface Enable
Open the Raspberry Pi configuration utility:
sudo raspi-configPlease follow the following steps to enable I2C interface:
Interface Options -> I2C -> Yes -> OK -> Finish
Run the Control Script
Once the dependencies are installed, navigate to the directory where the script was copied and execute it using Python3 in terminal
python3 control_ev9520l.pyNote
The camera is connected to CAM0 (CSI0), which uses I2C bus 6 (
smbus2.SMBus(6)).
Script Overview
| Function | Description |
|---|---|
| I²C Initialization | Opens I²C bus 6 (CAM0), connects to SC16IS752 at address 0x48 (A0=A1=0) |
| UART Configuration | Configures UART channel A — 9600 baud, 8N1 format, FIFO enabled (14.7456 MHz crystal) |
| TX Ready Check | Polls Line Status Register (LSR) until Transmit Holding Register (THR) is empty and ready |
| Zoom In Command | Sends 6-byte frame: [0x81, 0x01, 0x04, 0x07, 0x27, 0xFF] to camera via UART |
| Delay | Waits 4 seconds between the two command frames |
| Zoom Out Command | Sends second 6-byte frame: [0x81, 0x01, 0x04, 0x07, 0x37, 0xFF] to camera via UART |
Full Script
Save the following code as control_ev9520l.py on your Raspberry Pi:
import smbus2
import time
bus = smbus2.SMBus(6)
I2C_ADDR = 0x48 # A0=A1=0
def write_reg(subaddr, data, channel=0):
# Channel A: subaddr << 3 | 0x00
bus.write_byte_data(I2C_ADDR, (subaddr << 3) | 0x00, data)
def read_reg(subaddr, channel=0):
return bus.read_byte_data(I2C_ADDR, (subaddr << 3) | 0x00)
# Initialize UART A
write_reg(0x03, 0x80) # LCR: Enable divisor
write_reg(0x00, 0x60) # DLL: 91 (9600 baud, 14.7456MHz crystal)
write_reg(0x01, 0x00) # DLH: 0
write_reg(0x03, 0x03) # LCR: 8N1
write_reg(0x02, 0x01) # FCR: Enable FIFO
write_reg(0x01, 0x03) # IER: Enable TX holding
# Verify LSR (Line Status Register) for TX ready
while not (read_reg(0x05) & 0x20): # Check THR empty
time.sleep(0.01)
# Send data
data = [0x81, 0x01, 0x04, 0x07, 0x27, 0xFF]
data1 = [0x81, 0x01, 0x04, 0x07, 0x37, 0xFF]
for b in data:
write_reg(0x00, b) # THR: Write byte
time.sleep(0.01) # Small delay for stability
time.sleep(4)
for b in data1:
write_reg(0x00, b) # THR: Write byte
time.sleep(0.01) # Small delay for stability