Home / Tutorials / ESP8266 Tutorial / How to Use L298N Motor Driver with ESP8266
pcbway
L298N Motor Controller Tutorial

How to Use L298N Motor Driver with ESP8266

Controlling a DC motor with an ESP8266, L298N motor driver module, and Arduino Cloud provides a flexible and efficient way to automate motor control remotely. This tutorial will guide you through the setup, wiring, and code needed to achieve this.

Components Needed

  1. ESP8266 (NodeMCU or D1 Mini recommended)
  2. L298N Motor Driver Module
  3. DC Motor (6V-12V)
  4. External Power Supply (for the motor, such as a 9V battery or a suitable DC adapter)
  5. Jumper Wires
  6. Arduino Cloud account

Set Up Your Arduino Cloud Account

  1. Go to Arduino Cloud and sign in or create an account if you haven’t already.
  2. Once logged in, create a new thing in Arduino Cloud, which represents your device.
  3. Add variables for controlling the motor:
    • motorState (type: Boolean) – to turn the motor ON or OFF
    • motorSpeed (type: Integer, 0-100) – to control the speed of the motor
  4. Go to Devices > Add Device and select Set up an ESP8266 device. Follow the instructions to add your ESP8266 to your Arduino Cloud account. This will link your ESP8266 to the variables in the thing.

Connect the ESP8266 to the L298N Motor Driver and DC Motor

L298N Motor Driver Pinout:

  • IN1 and IN2 control the direction of the motor.
  • ENA controls the speed of the motor.
  • GND and VCC provide power to the L298N module.
  • OUT1 and OUT2 connect to the motor terminals.

Wiring:

  1. Connect the IN1 and IN2 pins on the L298N to D1 and D2 pins on the ESP8266.
  2. Connect the ENA pin on the L298N to D3 on the ESP8266.
  3. Connect OUT1 and OUT2 on the L298N to the motor terminals.
  4. Connect the GND pin on the L298N to the GND pin on the ESP8266.
  5. Provide power to the motor driver module:
    • Connect the +12V input on the L298N to an external 9V or 12V power supply (depending on your motor’s requirement).
    • Connect the GND of the external power supply to the GND of the ESP8266.

Important Note

Ensure that the motor power supply’s GND is shared with the ESP8266’s GND to avoid floating grounds.

Programming the ESP8266 in Arduino Cloud

  1. In Arduino Cloud, open the code editor for your ESP8266.
  2. Replace the generated code with the following code to control the motor through the Arduino Cloud variables.
#include <ArduinoIoTCloud.h> 
#include <Arduino_ConnectionHandler.h>

// Motor control pins
const int IN1 = D1;
const int IN2 = D2;
const int ENA = D3;

// Replace with your network credentials 
const char SSID[] = "your_SSID"; 
const char PASS[] = "your_PASSWORD";

// Initialize connection to Arduino IoT Cloud 
WiFiConnectionManager ArduinoIoTPreferredConnection(SSID, PASS);

// Arduino Cloud variables
bool motorState;
int motorSpeed;

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(115200);
  
  // Configure motor control pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  
  // Arduino Cloud connection
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  // Arduino Cloud loop
  ArduinoCloud.update();
  
  // Update motor control based on cloud variables
  if (motorState) {
    digitalWrite(IN1, HIGH);  // Set motor direction
    digitalWrite(IN2, LOW);
    analogWrite(ENA, map(motorSpeed, 0, 100, 0, 255));  // Adjust speed
  } else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, 0);  // Turn off motor
  }
}

void initProperties() {
  ArduinoCloud.addProperty(motorState, READWRITE, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(motorSpeed, READWRITE, ON_CHANGE, NULL);
}

Upload the Code

  1. Ensure your ESP8266 is connected to your computer and select the correct board and port in Arduino Cloud.
  2. Upload the code from the Arduino Cloud editor.

Test Your Setup

  1. In Arduino Cloud, navigate to the thing dashboard.
  2. You should see the motorState and motorSpeed variables.
  3. Toggle motorState to ON to start the motor.
  4. Adjust motorSpeed (0-100) to change the motor’s speed.

Explanation of the Code

  • motorState variable controls the motor’s ON/OFF state.
  • motorSpeed variable (0-100) is mapped to the range 0-255, which is the PWM range for controlling speed on the ENA pin.
  • The loop() function reads the cloud variables and adjusts the motor’s behavior based on their values.

Additional Tips

  • Motor Direction Control: You can reverse the motor direction by swapping digitalWrite(IN1, HIGH)  and digitalWrite(IN2, LOW) with digitalWrite(IN1, LOW) and digitalWrite(IN2, HIGH).
  • Add Indicators: Connect an LED to the ESP8266 to show when the motor is active or use the serial monitor to debug issues.

Conclusion

With this setup, you can control your DC motor remotely via Arduino Cloud. This project can be expanded with additional features, such as a feedback system using sensors to monitor motor performance.

Check Also

WeMos D1 Mini

WeMos D1 Mini WiFi Server

Now that you’ve set up your WeMos D1 Mini to be programmable using the Arduino …