Error: 'PWM' Object Has No Attribute 'duty' in Motor Control Code Using MicroPython

Am working on a smart car automation system using MicroPython, the system include various functionalities such as obstacle detection, line following, speed control, and environmental monitoring. Am currently trying to implement basic motor control using PWM signals and adjust motor speed using a potentiometer or set values, am working with the motor driver module L298N But am getting the error prompt PWM object has no attribute 'duty' here's my code
from machine import Pin, PWM
import time

motor1_pwm = PWM(Pin(14), freq=1000)

motor1_in1 = Pin(12, Pin.OUT)

motor1_in2 = Pin(13, Pin.OUT)

def forward(speed):
motor1_in1.on()
motor1_in2.off()
motor1_pwm.duty(int(speed * 1023))

def reverse(speed):
motor1_in1.off()
motor1_in2.on()
motor1_pwm.duty(int(speed * 1023))

def stop():
motor1_in1.off()
motor1_in2.off()
motor1_pwm.duty(0)

while True:
forward(0.7)
time.sleep(2)
reverse(0.5)
time.sleep(2)
stop()
time.sleep(2)
from machine import Pin, PWM
import time

motor1_pwm = PWM(Pin(14), freq=1000)

motor1_in1 = Pin(12, Pin.OUT)

motor1_in2 = Pin(13, Pin.OUT)

def forward(speed):
motor1_in1.on()
motor1_in2.off()
motor1_pwm.duty(int(speed * 1023))

def reverse(speed):
motor1_in1.off()
motor1_in2.on()
motor1_pwm.duty(int(speed * 1023))

def stop():
motor1_in1.off()
motor1_in2.off()
motor1_pwm.duty(0)

while True:
forward(0.7)
time.sleep(2)
reverse(0.5)
time.sleep(2)
stop()
time.sleep(2)
1 Reply
wafa_ath
wafa_ath3mo ago
MicroPython uses duty_u16() instead of duty(). Replace motor1_pwm.duty() with motor1_pwm.duty_u16() in your code
Want results from more Discord servers?
Add your server