This guide provides a detailed explanation of how to interface a DC motor with an ESP32 microcontroller. By connecting the motor to the ESP32 and using the appropriate control techniques, we can control the motor's speed and direction.
The objective of this project is to interface a DC motor with an ESP32 microcontroller and control its speed and direction.
To successfully complete this project, you will need the following components:
ESP32 development board
DC Motor
L298N Motor driver module
Power supply for the motor driver (12 V)
Jumper wires
Follow the circuit diagram below to connect the motor and motor driver with the ESP32 development board:
Connect the motor driver's enable pin (ENB) to the GPIO 4 (D4) of the ESP32.
Connect the motor driver's input pins IN3 and IN4 to GPIO 15 and GPIO 2 on the ESP32, respectively.
Connect the motor driver's output pins (OUT3, and OUT4) to the DC motor's terminals.
And the motor driver's GND and VCC pins to the ground and power supply, respectively.
Now let's understand and write the C code to operate the DC Motor. Follow the steps below:
Open VS Code and create a new sample project with the name dc_motor (Follow previous guides to see how to do it).
Now navigate to the "Main" folder of the project and then open the Main.c file.
Copy and paste the following code into Main.c or download the code from here:
#include <stdio.h>
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define MOTOR_ENB_PIN GPIO_NUM_4
#define MOTOR_IN3_PIN GPIO_NUM_15
#define MOTOR_IN4_PIN GPIO_NUM_2
#define MOTOR_PWM_FREQ 1000
#define MOTOR_PWM_RESOLUTION LEDC_TIMER_10_BIT
#define MOTOR_PWM_CHANNEL LEDC_CHANNEL_0
void motor_init()
{
// Configure motor enable pin as output
gpio_set_direction(MOTOR_ENB_PIN, GPIO_MODE_OUTPUT);
// Configure motor input pins as output
gpio_set_direction(MOTOR_IN3_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(MOTOR_IN4_PIN, GPIO_MODE_OUTPUT);
// Configure LEDC for PWM control
ledc_timer_config_t motor_pwm_timer = {
.duty_resolution = MOTOR_PWM_RESOLUTION,
.freq_hz = MOTOR_PWM_FREQ,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&motor_pwm_timer);
ledc_channel_config_t motor_pwm_channel = {
.channel = MOTOR_PWM_CHANNEL,
.duty = 0,
.gpio_num = MOTOR_ENB_PIN,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0};
ledc_channel_config(&motor_pwm_channel);
}
void motor_set_direction(bool clockwise)
{
if (clockwise)
{
gpio_set_level(MOTOR_IN3_PIN, 1);
gpio_set_level(MOTOR_IN4_PIN, 0);
}
else
{
gpio_set_level(MOTOR_IN3_PIN, 0);
gpio_set_level(MOTOR_IN4_PIN, 1);
}
}
void motor_set_speed(uint32_t speed)
{
ledc_set_duty(LEDC_HIGH_SPEED_MODE, MOTOR_PWM_CHANNEL, speed);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, MOTOR_PWM_CHANNEL);
}
void app_main(void)
{
motor_init();
bool direction = true;
while (1)
{
motor_set_direction(direction); // Set motor direction clockwise
motor_set_speed(500); // Set motor speed to 500 (0-1023 range)
// Wait for some time to observe the motor behavior
vTaskDelay(1000 / portTICK_PERIOD_MS);
motor_set_speed(0); // Stop the motor
motor_set_direction(!direction); // Set motor direction counterclockwise
motor_set_speed(800); // Set motor speed to 800 (0-1023 range)
// Wait for some time to observe the motor behavior
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Now at the bottom navbar of VS Code select the port, flash method and click on the Build, flash and monitor icon.
Note: During the flashing process of your board, you may encounter an error. If this happens, there's no need to worry. Simply flash the board again and when you see the message "connecting..........." appear, press and hold the boot button on your board for 1 or 2 seconds. Keep holding it until the dots on the screen start disappearing or the further flashing process begins. This action will help resolve any issues and ensure a successful flashing of your board.
After uploading the code to the ESP32, the motor rotates in one direction at a specified speed (clockwise at speed 500). After a delay, the motor changes its direction to the opposite direction at a different speed (counterclockwise at speed 800). This behavior repeats indefinitely
By following this guide, you have successfully interfaced a DC motor with an ESP32 and gained the ability to control its speed and direction. This knowledge can be applied to various applications, such as robotics, automation, and motor control projects. Feel free to experiment and extend the functionality to suit your specific project requirements. Ensure proper power supply and safety precautions while working with motors to avoid any damage or injury.