from machine import Pin, PWM
import time
# Pin configuration
motor_pin1 = 26 # Pin connected to motor 1 ESC
# PWM settings for motor 1
ledc_channel1 = 0
ledc_frequency1 = 50
ledc_resolution1 = 10 # Ridotto a 10 per adattarsi alla scala del modulo PWM (0-1023)
min_pulse_width = 1050
max_pulse_width = 1300
# Setup
motor_pin1_obj = PWM(Pin(motor_pin1), freq=ledc_frequency1, duty=0)
# Start with max throttle
motor_pin1_obj.duty(int((max_pulse_width / 20000) * (2 ** ledc_resolution1))) # Conversione in scala PWM
time.sleep(2) # Wait 2 seconds
# Then go to min throttle
motor_pin1_obj.duty(int((min_pulse_width / 20000) * (2 ** ledc_resolution1))) # Conversione in scala PWM
time.sleep(2) # Wait 2 seconds
# Initialize serial communication (print to REPL)
print("Motor setup completed.")
# Loop
while True:
# Increase speed every 1.5 seconds until max
for pulse_width in range(min_pulse_width, max_pulse_width + 1, 50):
motor_pin1_obj.duty(int((pulse_width / 20000) * (2 ** ledc_resolution1))) # Conversione in scala PWM
print("Motor speed:", pulse_width)
time.sleep(1.5)
# Decrease speed every 1.5 seconds until min
for pulse_width in range(max_pulse_width, min_pulse_width - 1, -50):
motor_pin1_obj.duty(int((pulse_width / 20000) * (2 ** ledc_resolution1))) # Conversione in scala PWM
print("Motor speed:", pulse_width)
time.sleep(1.5)