diff --git a/lgpio/motor-test.py b/lgpio/motor-test.py new file mode 100755 index 0000000..c7893fb --- /dev/null +++ b/lgpio/motor-test.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +# This test assumes you are trying to control a motor through a DRV8833 motor +# controller chip. This general approach should work for other motors as well. +import lgpio +import time + +# pin assignments +gpio_pwm = 18 #MOTA1 +gpio_reverse = 16 #MOTA2 +gpio_notsleep = 24 #MSLEEP +# parameters +pwm_freq = 1000 + + +h = lgpio.gpiochip_open(0) +try: + lgpio.gpio_claim_output(h, gpio_reverse) + lgpio.gpio_claim_output(h, gpio_notsleep) + lgpio.gpio_write(h, gpio_reverse, 0) # low on reverse pin to turn off h-bridge MOTA2 + lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to enable both h-bridges + + while True: + for pwm_level in range(0,100): + lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + for pwm_level in range(100,0,-1): + lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + +finally: + lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to disable + lgpio.gpiochip_close(h) # Always close! diff --git a/lgpio/pwm-test.py b/lgpio/pwm-test.py index e44f005..2f05f1d 100755 --- a/lgpio/pwm-test.py +++ b/lgpio/pwm-test.py @@ -5,21 +5,21 @@ import time # Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) h = lgpio.gpiochip_open(0) - +# Hardware PWM on GPIO18 (pin 12) must be PWM-capable +pwm_pin = 18 +pwm_freq = 1000 try: - lgpio.gpio_claim_output(h, 16) # GPIO16 as output (MOTA2) - lgpio.gpio_write(h, 16, 0) # LOW + while True: + for pwm_level in range(0,100): + lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) + for pwm_level in range(100,0,-1): + lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level) + pwm_level += 10 + time.sleep(0.05) - # 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.tx_pwm(h, pwm_pin, 0, 0) # Stop PWM lgpio.gpiochip_close(h) # Always close!