Files
rover-pi/lgpio/pwm-test.py
T
2026-07-02 16:56:44 +00:00

26 lines
679 B
Python
Executable File

#!/usr/bin/python
# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python
import lgpio
import time
# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible)
h = lgpio.gpiochip_open(0)
try:
lgpio.gpio_claim_output(h, 16) # GPIO16 as output (MOTA2)
lgpio.gpio_write(h, 16, 0) # LOW
# Hardware PWM on GPIO18 (pin 12) must be PWM-capable
# Frequency: 1000 Hz, duty cycle: 50%
lgpio.tx_pwm(h, 18, 1000, 50)
time.sleep(2)
# Reduce duty cycle to 20%
lgpio.tx_pwm(h, 18, 1000, 20)
time.sleep(2)
lgpio.tx_pwm(h, 18, 0, 0) # Stop PWM
finally:
lgpio.gpiochip_close(h) # Always close!