-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlow_level_i2c_test.py
More file actions
41 lines (34 loc) · 1.05 KB
/
Copy pathlow_level_i2c_test.py
File metadata and controls
41 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import lgpio
import time
# Define the I2C address and bus number
I2C_BUS = 1
I2C_ADDRESS = 0x40
# PCA9685 Registers
MODE1 = 0x00
PRESCALE = 0xFE
LED0_ON_L = 0x06
LED0_OFF_L = 0x08
# Set PWM frequency to 50 Hz
prescale_value = int(25000000.0 / (4096 * 50) - 1)
try:
h = lgpio.i2c_open(I2C_BUS, I2C_ADDRESS)
print(f"Successfully opened I2C connection to address 0x{I2C_ADDRESS:X}")
# Set PCA9685 mode
lgpio.i2c_write_device(h, [MODE1, 0x00])
time.sleep(0.1)
lgpio.i2c_write_device(h, [MODE1, 0x10])
time.sleep(0.1)
lgpio.i2c_write_device(h, [PRESCALE, prescale_value])
time.sleep(0.1)
lgpio.i2c_write_device(h, [MODE1, 0x00])
time.sleep(0.1)
lgpio.i2c_write_device(h, [MODE1, 0xa1])
time.sleep(0.1)
# Set PWM for channel 0 (example values)
lgpio.i2c_write_device(h, [LED0_ON_L, 0x00, 0x00])
lgpio.i2c_write_device(h, [LED0_OFF_L, 0x10, 0x00])
lgpio.i2c_close(h)
except lgpio.error as e:
print(f"I2C communication error: {e}")
except Exception as e:
print(f"Failed to open I2C connection: {e}")