This project introduces controlling the speed and direction of a DC motor using an L298N motor driver. Students will learn about motor drivers and Pulse Width Modulation (PWM).
Connect the L298N motor driver to the ESP32. Connect the VCC and GND pins of the L298N logic power to a 5V and GND pin on the ESP32. Connect the external motor power supply to the motor power terminals on the L298N (usually labeled +12V and GND, but connect your motor voltage and GND). Connect the two terminals of the DC motor to one of the motor output channels on the L298N (e.g., OUT1 and OUT2). Connect the IN1 and IN2 (or IN3 and IN4 for the second channel) control pins of the L298N to digital GPIO pins on the ESP32. Connect the Enable pin (ENA or ENB) for the chosen motor channel to a PWM-capable GPIO pin on the ESP32.
The MicroPython code will configure the IN pins as digital outputs to control the motor direction (high/low combinations for forward/reverse). The PWM-capable pin connected to the Enable pin will be configured for PWM output to control the motor speed by varying the duty cycle. The code will demonstrate setting the motor to run forward at a certain speed, then reverse at a different speed, and stopping the motor.
A web interface could provide buttons to control the motor direction (Forward, Reverse, Stop) and a slider or input field to adjust the motor speed.
# This script demonstrates controlling the speed and direction of a DC motor # using an L298N motor driver and the ESP32's PWM capabilities. from machine import Pin, PWM import time # Define the GPIO pin numbers connected to the L298N motor driver. # Replace these with the actual GPIO pins you are using. # IN1 and IN2 control the direction of Motor A. in1_pin = 17 in2_pin = 16 # ENA controls the speed of Motor A using PWM. ena_pin = 18 # Must be a PWM-capable pin # Create Pin objects for the direction control pins. in1 = Pin(in1_pin, Pin.OUT) in2 = Pin(in2_pin, Pin.OUT) # Create a PWM object for the enable pin to control speed. # Set the frequency for the PWM signal (e.g., 1000 Hz). pwm_ena = PWM(Pin(ena_pin), freq=1000) # Function to set the motor speed. # speed should be a value between 0 and 1023 (for 10-bit PWM resolution, common on ESP32). # 0 is stopped, 1023 is maximum speed. def set_motor_speed(speed): # Ensure speed is within the valid range. speed = max(0, min(1023, speed)) # Set the PWM duty cycle. pwm_ena.duty(speed) # Function to set the motor direction. # direction can be "forward", "reverse", or "stop". def set_motor_direction(direction): if direction == "forward": in1.value(1) # Set IN1 high in2.value(0) # Set IN2 low print("Motor Direction: Forward") elif direction == "reverse": in1.value(0) # Set IN1 low in2.value(1) # Set IN2 high print("Motor Direction: Reverse") else: # direction == "stop" or any other value in1.value(0) # Set IN1 low in2.value(0) # Set IN2 low (brakes the motor) print("Motor Direction: Stop") # Alternatively, you could set speed to 0 to stop without braking: # set_motor_speed(0) print("Starting DC motor control script...") # Example usage: # Run motor forward at medium speed. set_motor_direction("forward") set_motor_speed(512) # Medium speed (approx 50% duty cycle) time.sleep(3) # Run for 3 seconds # Stop the motor. set_motor_direction("stop") set_motor_speed(0) # Set speed to 0 explicitly to stop PWM output time.sleep(2) # Wait for 2 seconds # Run motor reverse at a different speed. set_motor_direction("reverse") set_motor_speed(768) # Higher speed (approx 75% duty cycle) time.sleep(3) # Run for 3 seconds # Stop the motor. set_motor_direction("stop") set_motor_speed(0) time.sleep(2) print("Motor control demonstration finished.") # You can add a while loop here to continuously control the motor # based on sensor input or other logic. # For example: # while True: # distance = measure_distance() # Assuming you have the ultrasonic sensor function # if distance < 20: # set_motor_direction("stop") # set_motor_speed(0) # else: # set_motor_direction("forward") # set_motor_speed(800) # time.sleep(0.1)
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.