Javascript required
Skip to content Skip to sidebar Skip to footer

Adafruit Dc Motor Hat Motor Jumps if Speed Is Set Again

This tutorial shows how to control the direction and speed of a DC motor using an ESP32 and the L298N Motor Driver. First, we'll take a quick await on how the L298N motor commuter works. Then, we'll show y'all an case on how to control the speed and direction of a DC motor using the ESP32 with Arduino IDE and the L298N motor commuter.

Note: there are many ways to command a DC motor. We'll exist using the L298N motor driver. This tutorial is besides compatible with similar motor commuter modules.

To ameliorate understand with this tutorial, you lot may want to accept a expect at the post-obit posts:

  • Getting Started with ESP32 Dev Module
  • Installing the ESP32 Board in Arduino IDE (Windows instructions)
  • Installing the ESP32 Board in Arduino IDE (Mac and Linux instructions)
  • ESP32 Web Server – Arduino IDE

Parts Required

To consummate this tutorial you need the following parts:

  • ESP32 DOIT DEVKIT V1 Board – read ESP32 Evolution Boards Review and Comparing
  • DC motor
  • L298N motor commuter
  • Power source: 4x ane.five AA batteries or Bench power supply
  • 2x 100nF ceramic capacitors (optional)
  • 1x SPDT slide switch (optional)
  • Jumper wires

Yous can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Introducing the L298N Motor Commuter

There are many ways to command a DC motor. The method we'll use here is suitable for most hobbyist motors, that require 6V or 12V to operate.

Nosotros're going to apply the L298N motor commuter that tin can handle up to 3A at 35V. Additionally, it allows u.s.a. to drive two DC motors simultaneously, which is perfect to build a robot.

The L298N motor commuter is shown in the following figure:

L298N Motor Driver pinout

Allow'southward take a look at the L298N motor driver pinout and see how information technology works.

The motor commuter has a two terminal cake in each side for each motor. OUT1 and OUT2 at the left and OUT3 and OUT4 at the right.

  • OUT1: DC motor A + concluding
  • OUT2: DC motor A – concluding
  • OUT3: DC motor B + final
  • OUT4: DC motor B – terminal

At the bottom yous have a three concluding block with +12V, GND, and +5V. The +12V terminal block is used to power upwards the motors. The +5V terminal is used to power up the L298N scrap. However, if the jumper is in place, the chip is powered using the motor'south power supply and you don't demand to supply 5V through the +5V final.

Notation: if y'all supply more than 12V, you lot need to remove the jumper and supply 5V to the +5V terminal.

It'southward important to note that despite the +12V last proper noun, with the setup we'll employ here (with the jumper in identify) you can supply any voltage between 6V and 12V. In this tutorial will be using 4 AA 1.5V batteries that combined output approximately 6V, but you lot can use any other suitable power supply. For example, you can use a bench ability supply to examination this tutorial.

In summary:

  • +12V: The +12V terminal is where you lot should connect your power supply
  • GND: power supply GND
  • +5V: provide 5V if jumper is removed. Acts as a 5V output if jumper is in place
  • Jumper: jumper in place – uses the motors power supply to power upwards the chip. Jumper removed: you need to provide 5V to the +5V terminal. If you supply more than 12V, you should remove the jumper

At the bottom correct y'all have four input pins and two enable terminals. The input pins are used to control the direction of your DC motors, and the enable pins are used to control the speed of each motor.

  • IN1:Input 1 for Motor A
  • IN2: Input 2 for Motor A
  • IN3: Input 1 for Motor B
  • IN4: Input 2 for Motor B
  • EN1: Enable pin for Motor A
  • EN2: Enable pin for Motor B

There are jumper caps on the enable pins by default. You demand to remove those jumper caps to control the speed of your motors.

Control DC motors with the L298N

Now that you're familiar with the L298N Motor Driver, allow's see how to use it to command your DC motors.

Enable pins

The enable pins are similar an ON and OFF switch for your motors. For case:

  • If you ship a HIGH bespeak to the enable 1 pin, motor A is ready to be controlled and at the maximum speed;
  • If you lot send a Depression signal to the enable 1 pivot, motor A turns off;
  • If you transport a PWM indicate, you lot can control the speed of the motor. The motor speed is proportional to the duty bicycle. However, note that for small duty cycles, the motors might not spin, and make a continuous fizz sound.
Bespeak ON THE ENABLE Pin MOTOR State
HIGH Motor enabled
LOW Motor non enabled
PWM Motor enabled: speed proportional to duty cycle

Input pins

The input pins control the management the motors are spinning. Input i and input 2 control motor A, and input 3 and iv control motor B.

  • If you lot apply LOW to input1 and HIGH to input 2, the motor will spin forward;
  • If you use ability the other way around: HIGH to input 1 and LOW to input two, the motor will rotate backwards. Motor B tin be controlled using the same method but applying High or Depression to input 3 and input 4.

Controlling 2 DC Motors – ideal to build a robot

If you want to build a robot automobile using 2 DC motors, these should be rotating in specific directions to make the robot go left, right, forward or backwards.

For example, if you want your robot to move forward, both motors should be rotating frontwards. To brand information technology go backwards, both should be rotating backwards.

To plow the robot in ane direction, you need to spin the contrary motor faster. For example, to brand the robot turn correct, enable the motor at the left, and disable the motor at the right. The post-obit table shows the input pins' state combinations for the robot directions.

DIRECTION INPUT ane INPUT 2 INPUT three INPUT 4
Forward 0 one 0 one
Backward 1 0 1 0
Right 0 1 0 0
Left 0 0 0 one
Stop 0 0 0 0

Recommended reading:Build Robot Auto Chassis Kit for ESP32, ESP8266, Arduino, etc…

Command DC Motor with ESP32 – Speed and Direction

At present that you know how to control a DC motor with the L298N motor driver, let's build a simple example to control the speed and direction of one DC motor.

Schematic

The motor we'll control is connected to the motor A output pins, and so nosotros need to wire the ENABLEA, INPUT1 and INPUT2 pins of the motor driver to the ESP32. Follow the next schematic diagram to wire the DC motor and the L298N motor driver to the ESP32.

The DC motor requires a big jump in current to movement, then the motors should be powered using an external power source from the ESP32. As an instance, nosotros're using 4AA batteries, but you lot can use any other suitable power supply. In this configuration, you tin can use a ability supply with 6V to 12V.

The switch between the battery holder and the motor driver is optional, only it is very handy to cutting and employ power. This way you don't need to constantly connect and then disconnect the wires to salvage power.

We recommend soldering a 0.1uF ceramic capacitor to the positive and negative terminals of the DC motor, every bit shown in the diagram to help smooth out any voltage spikes. (Annotation: the motors also work without the capacitor.)

Preparing the Arduino IDE

There's an add-on for the Arduino IDE allows you lot to programme the ESP32 using the Arduino IDE and its programming language. Follow one of the side by side tutorials to set your Arduino IDE to work with the ESP32, if yous haven't already.

  • Windows instructions – ESP32 Board in Arduino IDE
  • Mac and Linux instructions – ESP32 Board in Arduino IDE

Later making certain yous take the ESP32 add-on installed, you can continue this tutorial.

Uploading lawmaking

The following code controls the speed and management of the DC motor. This code is not useful in the existent world, this is only a elementary instance to improve understand how to control the speed and direction of a DC motor with the ESP32.

          /*********   Rui Santos   Complete projection details at https://randomnerdtutorials.com   *********/  // Motor A int motor1Pin1 = 27;  int motor1Pin2 = 26;  int enable1Pin = 14;   // Setting PWM backdrop const int freq = 30000; const int pwmChannel = 0; const int resolution = eight; int dutyCycle = 200;  void setup() {   // sets the pins as outputs:   pinMode(motor1Pin1, OUTPUT);   pinMode(motor1Pin2, OUTPUT);   pinMode(enable1Pin, OUTPUT);      // configure LED PWM functionalitites   ledcSetup(pwmChannel, freq, resolution);      // attach the aqueduct to the GPIO to be controlled   ledcAttachPin(enable1Pin, pwmChannel);    Serial.begin(115200);    // testing   Serial.print("Testing DC Motor..."); }  void loop() {   // Move the DC motor forrard at maximum speed   Serial.println("Moving Forward");   digitalWrite(motor1Pin1, LOW);   digitalWrite(motor1Pin2, High);    delay(2000);    // Stop the DC motor   Serial.println("Motor stopped");   digitalWrite(motor1Pin1, LOW);   digitalWrite(motor1Pin2, LOW);   filibuster(1000);    // Move DC motor backwards at maximum speed   Series.println("Moving Backwards");   digitalWrite(motor1Pin1, HIGH);   digitalWrite(motor1Pin2, Depression);    delay(2000);    // Cease the DC motor   Series.println("Motor stopped");   digitalWrite(motor1Pin1, Depression);   digitalWrite(motor1Pin2, LOW);   delay(yard);    // Motility DC motor forward with increasing speed   digitalWrite(motor1Pin1, HIGH);   digitalWrite(motor1Pin2, LOW);   while (dutyCycle <= 255){     ledcWrite(pwmChannel, dutyCycle);        Serial.print("Forward with duty bicycle: ");     Serial.println(dutyCycle);     dutyCycle = dutyCycle + v;     delay(500);   }   dutyCycle = 200; }                  

View raw code

Upload the code to your ESP32. Brand certain you have the correct lath and COM port selected. Let's take a await on how the code works.

Declaring motor pins

Kickoff, y'all define the GPIOs the motor pins are connected to. In this case, Input 1 for motor A is connected to GPIO 27, the Input 2 to GPIO 26, and the Enable pivot to GPIO 14.

          int motor1Pin1 = 27;  int motor1Pin2 = 26;  int enable1Pin = 14;        

Setting the PWM  backdrop to command the speed

As we've seen previously, you lot tin can control the DC motor speed by applying a PWM indicate to the enable pin of the L298N motor driver. The speed will be proportional to the duty bicycle. To use PWM with the ESP32, yous need to set the PWM signal properties first.

          const int freq = 30000; const int pwmChannel = 0; const int resolution = viii; int dutyCycle = 200;        

In this instance, nosotros're generating a indicate of 30000 Hz on channel 0 with a 8-fleck resolution. Nosotros first with a duty wheel of 200 (you tin can gear up a duty cycle value from 0 to 255).

For the frequency we're using, when you apply duty cycles smaller than 200, the motor won't move and volition make a weird fizz sound. So, that's why we set a duty cycle of 200 at the start.

Annotation: the PWM properties nosotros're defining here are simply an case. The motor works fine with other frequencies.

setup()

In the setup(), you start by setting the motor pins as outputs.

          pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(enable1Pin, OUTPUT);        

You demand to configure a PWM point with the properties you've divers earlier by using the ledcSetup() function that accepts as arguments, the pwmChannel, the frequency, and the resolution, as follows:

          ledcSetup(pwmChannel, freq, resolution);        

Next, you need to choose the GPIO you'll become the signal from. For that use theledcAttachPin() function that accepts as arguments the GPIO where you want to get the signal, and the channel that is generating the signal. In this case, we'll get the signal in the enable1Pin GPIO, that corresponds to GPIO 14. The channel that generates the betoken is the pwmChannel, that corresponds to channel 0.

          ledcAttachPin(enable1Pin, pwmChannel);        

Moving the DC motor forrard

In the loop() is where the motor moves. The code is well comment on what each part of the lawmaking does. To motion the motor forrad, yous set input 1 pin to Depression and input 2 pint to Loftier. In this example, the motor speeds forward for two seconds (2000 milliseconds).

          // Move the DC motor forrad at maximum speed Serial.println("Moving Forrad"); digitalWrite(motor1Pin1, Low); digitalWrite(motor1Pin2, Loftier);  delay(2000);        

Moving the DC motor backwards

To move the DC motor backwards you employ power to the motor input pins the other fashion around. High to input one and Low to input 2.

          // Movement DC motor backwards at maximum speed Serial.println("Moving Backwards"); digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW);  delay(2000);        

Stop the DC motor

To brand the DC motor finish, yous can either set the enable pivot to LOW, or gear up both input 1 and input ii pins to LOW. In this example we're setting both input pins to LOW.

          // Stop the DC motor Series.println("Motor stopped"); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); filibuster(chiliad);        

Controlling the DC motor speed

To control the DC motor speed, we need to change the PWM betoken duty cycle. For that you use the ledcWrite() office that accepts every bit arguments the PWM channel that is generating the signal (non the output GPIO) and the duty cycle, as follows.

          ledcWrite(pwmChannel, dutyCycle);        

In our example, we have a while loop that increases the duty cycle by 5 in every loop.

          // Move DC motor forwards with increasing speed digitalWrite(motor1Pin1, High); digitalWrite(motor1Pin2, Depression); while (dutyCycle <= 255){   ledcWrite(pwmChannel, dutyCycle);   Serial.print("Frontwards with duty cycle: ");   Series.println(dutyCycle);   dutyCycle = dutyCycle + 5;   delay(500); }        

When the while condition is no longer truthful, we gear up the duty cycle to 200 over again.

          dutyCycle = 200;        

Sentinel the Video Demonstration

Watch the side by side video to see the project in activeness:

Wrapping Up

In this tutorial we've shown you how to control the direction and speed of a DC motor using an ESP32 and the L298N motor driver. In summary:

  • To control the direction the DC motor is spinning yous use the input one and input ii pins;
  • Use LOW to input 1 and HIGH to input 2 to spin the motor forward. Apply power the other style effectually to make it spin backwards;
  • To command the speed of the DC motor, yous use a PWM signal on the enable pin. The speed of the DC motor is proportional to the duty cycle.

We hope you've institute this tutorial useful.

This is an excerpt from our class: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more most it, we recommend enrolling in Learn ESP32 with Arduino IDE class.

You lot might likewise similar reading:

  • Learn ESP32 with Arduino IDE
  • ESP32 Servo Motor Web Server with Arduino IDE
  • Alexa (Repeat) with ESP32 and ESP8266 – Phonation Controlled Relay
  • Build an All-in-I ESP32 Weather condition Station Shield
  • ESP32 Publish Sensor Readings to Google Sheets

Thanks for reading.

casimatydenard.blogspot.com

Source: https://randomnerdtutorials.com/esp32-dc-motor-l298n-motor-driver-control-speed-direction/