Controlling Two Motors with MicroPython
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers. One of the common and exciting applications of MicroPython is controlling motors. In this blog post, we will explore how to move two motors using MicroPython. This can be useful in various projects such as robotics, automated vehicles, and more.
Table of Contents#
- Fundamental Concepts
- Hardware Setup
- Usage Methods
- Common Practices
- Best Practices
- Conclusion
- References
Fundamental Concepts#
Pulse Width Modulation (PWM)#
Pulse Width Modulation is a technique used to control the power delivered to a device by varying the width of the pulses in a pulse train. In the context of motor control, PWM is used to control the speed of the motors. By adjusting the duty cycle (the ratio of the pulse width to the period of the signal), we can control how much power is supplied to the motor, and thus its speed.
Motor Drivers#
Most microcontrollers do not have enough current - driving capabilities to directly power motors. So, we use motor drivers. Motor drivers act as an interface between the microcontroller and the motors, providing the necessary current to drive the motors. Popular motor drivers include the L298N, which can control two DC motors.
Hardware Setup#
For this example, we'll assume we are using an ESP32 microcontroller and an L298N motor driver.
- Connect the ESP32 to the L298N:
- Connect the control pins (e.g., GPIO pins on the ESP32) to the input pins of the L298N. These pins will be used to control the direction and speed of the motors.
- Connect the power supply to the L298N. Make sure to use an appropriate power source for your motors.
- Connect the motors to the output pins of the L298N.
Usage Methods#
Initializing the Pins#
import machine
# Define the pins for motor control
in1 = machine.Pin(2, machine.Pin.OUT)
in2 = machine.Pin(3, machine.Pin.OUT)
in3 = machine.Pin(4, machine.Pin.OUT)
in4 = machine.Pin(5, machine.Pin.OUT)
# Create PWM objects for speed control
pwm1 = machine.PWM(in1)
pwm2 = machine.PWM(in2)
pwm3 = machine.PWM(in3)
pwm4 = machine.PWM(in4)
# Set the frequency of the PWM signals
pwm1.freq(1000)
pwm2.freq(1000)
pwm3.freq(1000)
pwm4.freq(1000)Controlling the Motors#
# Set the duty cycle to control the speed
pwm1.duty(512) # Half speed for motor 1 forward
pwm2.duty(0)
pwm3.duty(512) # Half speed for motor 2 forward
pwm4.duty(0)
# Reverse the direction of motor 1
pwm1.duty(0)
pwm2.duty(512)Stopping the Motors#
pwm1.duty(0)
pwm2.duty(0)
pwm3.duty(0)
pwm4.duty(0)Common Practices#
Using Functions for Motor Control#
import machine
def init_motors():
in1 = machine.Pin(2, machine.Pin.OUT)
in2 = machine.Pin(3, machine.Pin.OUT)
in3 = machine.Pin(4, machine.Pin.OUT)
in4 = machine.Pin(5, machine.Pin.OUT)
pwm1 = machine.PWM(in1)
pwm2 = machine.PWM(in2)
pwm3 = machine.PWM(in3)
pwm4 = machine.PWM(in4)
pwm1.freq(1000)
pwm2.freq(1000)
pwm3.freq(1000)
pwm4.freq(1000)
return pwm1, pwm2, pwm3, pwm4
def move_forward(pwm1, pwm2, pwm3, pwm4, speed=512):
pwm1.duty(speed)
pwm2.duty(0)
pwm3.duty(speed)
pwm4.duty(0)
def stop_motors(pwm1, pwm2, pwm3, pwm4):
pwm1.duty(0)
pwm2.duty(0)
pwm3.duty(0)
pwm4.duty(0)
pwm1, pwm2, pwm3, pwm4 = init_motors()
move_forward(pwm1, pwm2, pwm3, pwm4)
# Do some tasks
stop_motors(pwm1, pwm2, pwm3, pwm4)Error Handling#
When working with motors, there can be issues such as power supply problems or hardware failures. It's a good practice to add some error handling in your code. For example:
try:
pwm1, pwm2, pwm3, pwm4 = init_motors()
move_forward(pwm1, pwm2, pwm3, pwm4)
except Exception as e:
print("An error occurred:", e)
stop_motors(pwm1, pwm2, pwm3, pwm4)Best Practices#
Calibration#
Before using the motors in a project, it's important to calibrate them. This involves finding the minimum and maximum duty cycles that will make the motors move smoothly. You can do this by gradually increasing and decreasing the duty cycle and observing the motor's behavior.
Power Management#
Motors can consume a significant amount of power. Make sure your power supply is capable of providing enough current. Also, when the motors are not in use, turn them off to save power.
Code Modularity#
As shown in the previous examples, use functions to encapsulate motor control logic. This makes the code more readable, maintainable, and easier to reuse.
Conclusion#
Controlling two motors with MicroPython is a powerful and accessible way to add motion to your projects. By understanding the fundamental concepts of PWM and motor drivers, setting up the hardware correctly, and following common and best practices, you can effectively control the speed and direction of two motors. Whether you are building a simple robot or a more complex automated system, MicroPython provides a convenient and efficient way to interact with motors.
References#
- MicroPython official documentation: https://docs.micropython.org/
- L298N motor driver datasheet
- ESP32 official documentation