ESP32 Tutorial 49/55 - Control DC Motor Over The internet using Adafruit IoT | SunFounder's ESP32 kit

ESP32 Tutorial 49/55 - Control DC Motor Over The internet using Adafruit IoT | SunFounder's ESP32 kit

In this tutorial, we will explore how to control a DC motor over the internet using the ESP32 and the Adafruit IO MQTT service. The DC motor's speed and direction can be manipulated remotely, allowing for efficient control from anywhere with an internet connection. This project demonstrates the capabilities of the ESP32 microcontroller, which features built-in Wi-Fi, making it ideal for Internet of Things (IoT) applications.

esp32-49-DC-motor-mqtt-main

We will be implementing a system where the motor can be started, stopped, and its speed adjusted via a web interface connected to Adafruit IO. Users can subscribe to specific topics for motor control and adjust parameters accordingly. To better understand the process, be sure to check out the video accompanying this tutorial (in video at 00:00).

Hardware Explained

For this project, we will utilize the ESP32 microcontroller, which is the heart of our system. The ESP32 is capable of handling Wi-Fi communications, making it perfect for our IoT application. It connects to the Adafruit IO platform, allowing us to send and receive messages via MQTT protocol.

Additionally, we will be using the L293D motor driver, which is essential for controlling the DC motor. The L293D can drive two DC motors and allows for control of both direction and speed through Pulse Width Modulation (PWM). It essentially acts as an interface between the ESP32 and the motor, managing the higher current required by the motor while isolating the ESP32 from any potentially damaging back-emf signals.

Datasheet Details

Manufacturer Texas Instruments
Part number L293D
Logic/IO voltage 4.5 - 36 V
Supply voltage 4.5 - 36 V
Output current (per channel) 600 mA
Peak current (per channel) 1.2 A
PWM frequency guidance 10 kHz (typ.)
Input logic thresholds 0.8 V (high), 2.0 V (low)
Voltage drop / RDS(on) / saturation 1.5 V max
Thermal limits 150 °C
Package DIP-16
Notes / variants Quadruple high-current half-H driver

 

  • Ensure proper heat sinking for continuous operation.
  • Use PWM to control motor speed effectively.
  • Observe input voltage limits to prevent damage.
  • Check for proper ground connections between all components.
  • Be cautious of back-emf; use diodes if necessary.
  • Double-check wiring as polarity can affect motor direction.
  • Test with a lower voltage before full operation.
  • Watch for overheating during extended use.
  • Make sure to debounce mechanical switches if used.
  • Ensure the ESP32 is not overloaded by the motor's current.

Wiring Instructions

ESP32-15_L293D_motor_schematic

Begin by connecting the power supply. Connect the positive terminal of your external power source to the VCC pin of the L293D (pin 8) and the ground to the GND pin (pin 4). Ensure the ESP32 is powered separately if required, typically through a micro USB connection.

Next, connect the motor to the L293D. One terminal of the motor should be connected to output pin 3 (pin 2 of the L293D) and the other terminal to output pin 6 (pin 7 of the L293D). For control signals, connect pin 13 of the ESP32 to input pin 2 (pin 1 of the L293D) and pin 14 to input pin 7 (pin 2 of the L293D). The enable pin (pin 1) should also be connected to the 5V supply to activate the driver. Lastly, make sure the ESP32 ground is connected to the L293D ground for a common reference.

Code Examples & Walkthrough

The provided code initializes the necessary libraries and sets up the Wi-Fi and MQTT client. Key identifiers include motorSpeed, motorDirection, and motorStart, which manage the motor's operation based on the commands received from Adafruit IO.

bool debug = false;
#define motor1A 13
#define motor2A 14
int motorSpeed = 0;
int motorDirection = 1;
int motorStart = 1;

In this excerpt, the motor pins are defined along with initial variables for controlling speed, direction, and the start/stop state of the motor. The variable motorSpeed will be adjusted based on the input from Adafruit IO.

void setup() {
  Serial.begin(115200);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  delay(2000);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

In the setup function, the serial communication is initiated, and the ESP32 connects to the specified Wi-Fi network. This connection is crucial for enabling MQTT communication.

void loop() {
  MQTT_connect();
  mqtt.processPackets(500);
  runMotor();
}

This loop function establishes the MQTT connection and processes incoming packets to control the motor based on the latest commands received. The function runMotor() is called to apply the current settings to the motor.

Demonstration / What to Expect

When the setup is complete and the code is uploaded, you should be able to control the motor through the Adafruit IO dashboard. You can adjust the motor speed using a slider and change its direction with a toggle switch. If everything is wired correctly, the motor will respond to these commands in real-time, showcasing the low latency of the system (in video at 00:00).

Common issues include reversed motor direction due to incorrect wiring, so double-check connections if the motor does not behave as expected. Additionally, ensure that your MQTT topics are correctly set up in Adafruit IO to match the code.

Video Timestamps

  • 00:00 Start
  • 2:21 Introduction to the project
  • 4:20 How DC Motor is controlled
  • 6:39 L293D Motor Driver
  • 11:42 What is MQTT?
  • 15:03 Adafruit IO setup
  • 19:17 Wiring Explained
  • 22:42 Code explained
  • 35:28 Project Demonstration

Images

ESP32-15_L293D_motor_schematic
ESP32-15_L293D_motor_schematic
esp32-49-DC-motor-mqtt-main
esp32-49-DC-motor-mqtt-main
851-ESP32 Tutorial 49/55- Arduino code using ESP32 to DC Motor using MQTT service of Adafruit
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.