{"id":148573,"date":"2024-02-22T11:42:47","date_gmt":"2024-02-22T11:42:47","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=148573"},"modified":"2025-03-17T10:51:17","modified_gmt":"2025-03-17T10:51:17","slug":"esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction\/","title":{"rendered":"ESP8266 NodeMCU with DC Motor and L298N Motor Driver \u2013 Control Speed and Direction (Arduino IDE)"},"content":{"rendered":"\n<p>Learn how to control the direction and speed of a DC motor using an ESP8266 NodeMCU board and the&nbsp;L298N Motor Driver. We&#8217;ll start by explaining how the L298N motor driver works. Then, we&#8217;ll show you how to control the speed and direction of a DC motor with the L298N motor driver using the ESP8266 programmed with Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU with DC Motor and L298N Motor Driver Control Speed and Direction\" class=\"wp-image-148579\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Note<\/strong>: there are many ways to control a DC motor. We&#8217;ll be using the L298N motor driver. This tutorial is also compatible with similar motor driver modules.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>This tutorial covers the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#introducing-L298n-motor-driver\" title=\"\">Introducing the L298N Motor Driver<\/a><\/li>\n\n\n\n<li><a href=\"#l298n-motor-driver-pinout\" title=\"\">L298N Motor Driver Pinout<\/a><\/li>\n\n\n\n<li><a href=\"#control-dc-motor-l298n\" title=\"\">Control DC Motors with the L298N<\/a><\/li>\n\n\n\n<li><a href=\"#dc-motor-esp8266-speed-direction\" title=\"\">Control DC Motor with ESP8266 &#8211; Speed and Direction<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#wire-dc-motor-esp8266\" title=\"\">Wiring a DC Motor to the ESP8266 (L298N Motor Driver)<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-dc-motor-code\" title=\"\">Code: ESP8266 with a DC Motor &#8211; Control Speed and Direction <\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Are you using an ESP32?<\/strong> Follow this tutorial instead: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-dc-motor-l298n-motor-driver-control-speed-direction\/\">ESP32 with DC Motor and L298N Motor Driver \u2013 Control Speed and Direction<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding, make sure you check the following prerequisites:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP8266 with Arduino IDE<\/h3>\n\n\n\n<p>We&#8217;ll program the ESP8266 using Arduino IDE. So, make sure you have the ESP8266 board installed. Follow the next tutorial if you haven\u2019t already:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Installing ESP8266 NodeMCU Board in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Alternatively, you may also want to program the ESP8266 using VS Code and the platformIO extension:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/vs-code-platformio-ide-esp32-esp8266-arduino\/\">Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>To complete this tutorial you need the following parts:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-control-DC-Motor-L298N.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 control DC Motor with L298N motor driver\" class=\"wp-image-148720\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-control-DC-Motor-L298N.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-control-DC-Motor-L298N.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266 Board<\/a> &#8211; read <a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266 Boards Comparison<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mini-dc-motor\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mini DC motor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/l298n-motor-driver\/\" target=\"_blank\" rel=\"noreferrer noopener\">L298N motor driver<\/a><\/li>\n\n\n\n<li>Power source:&nbsp;4x 1.5 AA batteries + <a href=\"https:\/\/makeradvisor.com\/tools\/4x-aa-battery-holder\/\" target=\"_blank\" rel=\"noopener\" title=\"\">battery holder<\/a>,&nbsp;or&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-bench-power-supply\/\" target=\"_blank\" rel=\"noreferrer noopener\">Bench power supply<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ceramic-capacitors-kit\/\" target=\"_blank\" rel=\"noreferrer noopener\">0.1uF ceramic capacitor<\/a>&nbsp;(optional)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/spdt-slide-switch\/\" target=\"_blank\" rel=\"noreferrer noopener\">1x SPDT slide switch<\/a>&nbsp;(optional)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introducing-L298n-motor-driver\">Introducing the L298N Motor Driver<\/h2>\n\n\n\n<p>There are several ways to control a DC motor. The method we\u2019ll use here is suitable for most hobbyist motors, that require 6V or 12V to operate.<\/p>\n\n\n\n<p>We\u2019re going to use the L298N motor driver that can handle up to 3A at 35V. Additionally, it allows us to drive two DC motors simultaneously, which is perfect for building a robot. If you&#8217;re using a different motor driver, don&#8217;t worry, most of those modules work similarly and have the same input and output pins.<\/p>\n\n\n\n<p>The L298N motor driver is shown in the following figure:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/298n-motor-driver.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"298N Motor Driver\" class=\"wp-image-146161\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/298n-motor-driver.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/298n-motor-driver.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"l298n-motor-driver-pinout\">L298N Motor Driver pinout<\/h3>\n\n\n\n<p>Let\u2019s take a look at the L298N motor driver pinout and see how it works.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/L298N-label.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"L298N Motor Driver pinout\" class=\"wp-image-61999\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/L298N-label.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/L298N-label.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The motor driver has a two-terminal block on each side for each motor. <strong>OUT1<\/strong> and <strong>OUT2<\/strong> at the left and <strong>OUT3<\/strong> and <strong>OUT4<\/strong> at the right.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>OUT1<\/strong>: DC motor A <strong>+<\/strong> terminal<\/li>\n\n\n\n<li><strong>OUT2<\/strong>: DC motor A <strong>&#8211;<\/strong> terminal<\/li>\n\n\n\n<li><strong>OUT3<\/strong>: DC motor B <strong>+<\/strong> terminal<\/li>\n\n\n\n<li><strong>OUT4<\/strong>: DC motor B <strong>&#8211;<\/strong> terminal<\/li>\n<\/ul>\n\n\n\n<p>At the bottom, you have a three-terminal block with <span class=\"rnthl rntcred\">+12V<\/span>, <span class=\"rnthl rntcblack\">GND<\/span>, and <span class=\"rnthl rntcorange\">+5V<\/span>. The <span class=\"rnthl rntcred\">+12V<\/span> terminal block is used to power up the motors. The <span class=\"rnthl rntcorange\">+5V<\/span> terminal is used to power up the L298N chip. However, if the jumper is in place, the chip is powered using the motor\u2019s power supply and you don\u2019t need to supply 5V through the <span class=\"rnthl rntcorange\">+5V<\/span> terminal.<\/p>\n\n\n\n<p><strong>Important: despite the&nbsp;+12V&nbsp;terminal name, you can supply any voltage between 5V and 35V (but 6V to 12V is the recommended range).<\/strong><\/p>\n\n\n\n<p><strong>Note:<\/strong> if you supply more than 12V, you need to remove the jumper and supply 5V to the +5V terminal.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll be using 4 AA 1.5V batteries that combined output approximately 6V, but you can use any other suitable power supply. For example, you can use a <a href=\"https:\/\/makeradvisor.com\/best-bench-power-supply\/\" target=\"_blank\" rel=\"noreferrer noopener\">bench power supply<\/a> to test this tutorial.<\/p>\n\n\n\n<p><strong>In summary:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>+12V<\/strong>: The +12V terminal is where you should connect the motor&#8217;s power supply<\/li>\n\n\n\n<li><strong>GND<\/strong>: power supply GND<\/li>\n\n\n\n<li><strong>+5V<\/strong>: provide 5V if the jumper is removed. Acts as a 5V output if the jumper is in place<\/li>\n\n\n\n<li><strong>Jumper<\/strong>: jumper in place \u2013 uses the motor power supply to power up the chip. Jumper removed: you need to provide 5V to the +5V terminal. If you supply more than 12V, you should remove the jumper<\/li>\n<\/ul>\n\n\n\n<p>At the bottom right you 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.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IN1:&nbsp;<\/strong>Input 1 for Motor A<\/li>\n\n\n\n<li><strong>IN2<\/strong>:&nbsp;Input 2 for Motor A<\/li>\n\n\n\n<li><strong>IN3<\/strong>:&nbsp;Input 1 for Motor B<\/li>\n\n\n\n<li><strong>IN4<\/strong>:&nbsp;Input 2 for Motor B<\/li>\n\n\n\n<li><strong>EN1<\/strong>:&nbsp;Enable pin for Motor A<\/li>\n\n\n\n<li><strong>EN2<\/strong>:&nbsp;Enable pin for Motor B<\/li>\n<\/ul>\n\n\n\n<p>There are jumper caps on the enable pins by default. You need to remove those jumper caps to control the speed of your motors. Otherwise, they will either be stopped or spinning at the maximum speed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"control-dc-motor-l298n\">Control DC motors with the L298N Motor Driver<\/h2>\n\n\n\n<p>Now that you understand how the motor driver works, let\u2019s see how to use it to control a DC motor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enable pins<\/h3>\n\n\n\n<p>The enable pins are like an ON and OFF switch for the motors. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you send a <strong>HIGH signal<\/strong> to the enable 1 pin, motor A is ready to be controlled and at the maximum speed;<\/li>\n\n\n\n<li>If you send a<strong> LOW signal<\/strong> to the enable 1 pin, motor A turns off;<\/li>\n\n\n\n<li>If you send a <strong>PWM signal<\/strong>, you can control the speed of the motor. The motor speed is proportional to the duty cycle. However, note that for small duty cycles, the motors might not spin, and make a continuous buzz sound.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>SIGNAL ON THE ENABLE PIN<\/th><th>MOTOR STATE<\/th><\/tr><\/thead><tbody><tr><td>HIGH<\/td><td>Motor enabled<\/td><\/tr><tr><td>LOW<\/td><td>Motor not enabled<\/td><\/tr><tr><td>PWM<\/td><td>Motor enabled: speed proportional to the duty cycle<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Input pins<\/h3>\n\n\n\n<p>The input pins control the direction the motors are spinning. Input 1 and input 2 control motor A, and input 3 and 4 control motor B.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you apply LOW to input1 and HIGH to input 2, the motor will spin forward;<\/li>\n\n\n\n<li>If you apply power the other way around: HIGH to input 1 and LOW to input 2, the motor will rotate backwards. Motor B can be controlled using the same method but applying HIGH or LOW to input 3 and input 4.<\/li>\n<\/ul>\n\n\n\n<p>For example, for motor A, this is the logic:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Direction<\/strong><\/td><td><strong>Input 1<\/strong><\/td><td><strong>Input 2<\/strong><\/td><td><strong>Enable 1<\/strong><\/td><\/tr><tr><td><strong>Forward<\/strong><\/td><td>0<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td><strong>Backwards<\/strong><\/td><td>1<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td><strong>Stop<\/strong><\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Controlling 2 DC Motors &#8211; ideal to build a robot<\/h3>\n\n\n\n<p>If you want to <a href=\"https:\/\/randomnerdtutorials.com\/build-robot-car-chassis-kit-arduino\/\">build a robot car<\/a> using 2 DC motors, these should be rotating in specific directions to make the robot go left, right, forward, or backward.<\/p>\n\n\n\n<p>For example, if you want your robot to move forward, both motors should be rotating forward. To make it go backward, both should be rotating backward.<\/p>\n\n\n\n<p>To turn the robot in one direction, you need to spin the opposite motor faster. For example, to make the robot turn right, enable the motor at the left, and disable the motor at the right. The following table shows the input pins&#8217; state combinations for the robot directions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>DIRECTION<\/th><th>INPUT 1<\/th><th>INPUT 2<\/th><th>INPUT 3<\/th><th>INPUT 4<\/th><\/tr><\/thead><tbody><tr><td>Forward<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>Backward<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>Right<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>Left<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>Stop<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Recommended reading:&nbsp;<\/strong><a href=\"https:\/\/randomnerdtutorials.com\/build-robot-car-chassis-kit-arduino\/\">Build Robot Car Chassis Kit for ESP32, ESP8266, Arduino,&nbsp;etc&#8230;<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dc-motor-esp8266-speed-direction\">Control DC Motor with ESP8266 NodeMCU &#8211; Speed and Direction<\/h2>\n\n\n\n<p>Now that you know how an L298N motor driver works and how to control a DC motor, let&#8217;s build a simple example sketch to control the speed and direction of one DC motor with the ESP8266.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wire-dc-motor-esp8266\">Wiring a DC Motor to the ESP8266 (L298N Motor Driver)<\/h3>\n\n\n\n<p>The motor we&#8217;ll control is connected to the motor A output pins, so we need to wire the ENABLEA, INPUT1, and INPUT2 pins of the motor driver to the ESP8266. Follow the next schematic diagram to wire the DC motor and the L298N motor driver to the ESP8266 NodeMCU board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1165\" height=\"1322\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Circuit-DC-Motor-L298N.jpg?resize=1165%2C1322&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU with DC Motor L298N Schematic Diagram\" class=\"wp-image-148580\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Circuit-DC-Motor-L298N.jpg?w=1165&amp;quality=100&amp;strip=all&amp;ssl=1 1165w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Circuit-DC-Motor-L298N.jpg?resize=264%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 264w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Circuit-DC-Motor-L298N.jpg?resize=902%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 902w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Circuit-DC-Motor-L298N.jpg?resize=768%2C871&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1165px) 100vw, 1165px\" \/><\/figure><\/div>\n\n\n<p>We&#8217;re using the GPIOs in the following table to connect to the motor driver. You can use any other suitable GPIOs as long as you modify the code accordingly.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>LN298N Motor Driver<\/strong><\/td><td><strong>Input 1<\/strong><\/td><td><strong>Input 2<\/strong><\/td><td><strong>Enable<\/strong><\/td><td><strong>GND<\/strong><\/td><\/tr><tr><td><strong>ESP8266<\/strong><\/td><td>GPIO 12 (D6)<\/td><td>GPIO 14 (D5)<\/td><td>GPIO 13 (D7)<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\" title=\"\">ESP8266 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Powering the LN298N Motor Driver<\/h4>\n\n\n\n<p>The DC motor requires a big jump in current to move, so the motors should be powered using an external power source. As an example, we&#8217;re using 4AA batteries, but you can use any other suitable power supply. In this configuration, you can use a power supply in the range of 6V to 12V.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"499\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/4-AA-batteries.jpg?resize=750%2C499&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Powering the LN298N Motor Driver\" class=\"wp-image-146198\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/4-AA-batteries.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/4-AA-batteries.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>On the schematic diagram, the switch between the battery holder and the motor driver is optional, but it&#8217;s handy to cut and apply power. This way you don&#8217;t need to constantly connect and then disconnect the wires to save power. There are also battery holders that already come with a button<\/p>\n\n\n\n<p>We recommend soldering a 0.1uF ceramic capacitor to the positive and negative terminals of the DC motor, as shown in the diagram to help smooth out any voltage spikes. (Note: the motors also work without the capacitor.)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/DC-Motor-with-capacitor.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/DC-Motor-with-capacitor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring a DC Motor to L298N Motor Driver\" class=\"wp-image-148589\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/DC-Motor-with-capacitor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/DC-Motor-with-capacitor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"esp8266-dc-motor-code\">Code: ESP8266 with a DC Motor &#8211; Control Speed and Direction<\/h3>\n\n\n\n<p>The following code controls the speed and direction of the DC motor. This code might not have a practical application in the real world but is a great example to understand how to control the speed and direction of a DC motor with the ESP8266.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction\/\n*********\/\n\n\/\/ Motor A\nint motor1Pin1 = 12; \nint motor1Pin2 = 14; \nint enable1Pin = 13; \n\n\/\/ Setting minimum duty cycle\nint dutyCycle = 60;\n\nvoid setup() {\n  \/\/ sets the pins as outputs:\n  pinMode(motor1Pin1, OUTPUT);\n  pinMode(motor1Pin2, OUTPUT);\n  pinMode(enable1Pin, OUTPUT);\n\n  Serial.begin(115200);\n\n  \/\/ testing\n  Serial.print(&quot;Testing DC Motor...&quot;);\n}\n\nvoid loop() {\n\n  \/\/Apply power to spin at maximum speed\n  digitalWrite(enable1Pin, HIGH);\n\n  \/\/ Move the DC motor forward at maximum speed\n  Serial.println(&quot;Moving Forward&quot;);\n  digitalWrite(motor1Pin1, LOW);\n  digitalWrite(motor1Pin2, HIGH); \n  delay(2000);\n\n  \/\/ Stop the DC motor\n  Serial.println(&quot;Motor stopped&quot;);\n  digitalWrite(motor1Pin1, LOW);\n  digitalWrite(motor1Pin2, LOW);\n  delay(1000);\n\n  \/\/ Move DC motor backwards at maximum speed\n  Serial.println(&quot;Moving Backwards&quot;);\n  digitalWrite(motor1Pin1, HIGH);\n  digitalWrite(motor1Pin2, LOW); \n  delay(2000);\n\n  \/\/ Stop the DC motor\n  Serial.println(&quot;Motor stopped&quot;);\n  digitalWrite(motor1Pin1, LOW);\n  digitalWrite(motor1Pin2, LOW);\n  delay(1000);\n\n  \/\/ Move DC motor forward with increasing speed\n  digitalWrite(motor1Pin1, HIGH);\n  digitalWrite(motor1Pin2, LOW);\n  while (dutyCycle &lt;= 255){\n    analogWrite(enable1Pin, dutyCycle);   \n    Serial.print(&quot;Forward with duty cycle: &quot;);\n    Serial.println(dutyCycle);\n    dutyCycle = dutyCycle + 5;\n    delay(500);\n  }\n  dutyCycle = 60;\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_DC_Motor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Upload the code to your ESP8266. Make sure you have the right board and COM port selected. <\/p>\n\n\n\n<p>Continue reading to learn how the code works or skip to the <a href=\"#demonstration\" title=\"\">demonstration <\/a>section.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring motor pins<\/h4>\n\n\n\n<p>First, you define the GPIOs connected to the motor driver. In this case, Input 1 for motor A is connected to GPIO 12, Input 2 to GPIO 14, and the Enable pin to GPIO 13.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int motor1Pin1 = 12; \nint motor1Pin2 = 14; \nint enable1Pin = 13; <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting minimum duty cycle<\/h4>\n\n\n\n<p>We&#8217;ll control the speed of the motor by adjusting the duty cycle of the PWM signal. We&#8217;ll set the minimum duty cycle to 60 because for smaller values the motor won&#8217;t move and will make a weird buzz sound.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int dutyCycle = 60;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, you start by setting the motor pins as outputs.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(motor1Pin1, OUTPUT);\npinMode(motor1Pin2, OUTPUT);\npinMode(enable1Pin, OUTPUT);<\/code><\/pre>\n\n\n\n<p>We initialize the Serial Monitor for debugging purposes:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Moving the DC motor forward<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where the motor moves. The code is well commented on what each part of the code does. <\/p>\n\n\n\n<p>To supply power to the motors, and move them at the maximum speed, we need to set the enable pin to <span class=\"rnthl rntliteral\">HIGH<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(enable1Pin, HIGH);<\/code><\/pre>\n\n\n\n<p>To move the motor forward, you set input 1 pin to LOW and input 2 pint to HIGH. In this example, the motor speeds forward for 2 seconds (2000 milliseconds).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Move the DC motor forward at maximum speed\nSerial.println(\"Moving Forward\");\ndigitalWrite(motor1Pin1, LOW);\ndigitalWrite(motor1Pin2, HIGH); \ndelay(2000);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Moving the DC motor backwards<\/h4>\n\n\n\n<p>To move the DC motor backwards you apply power to the motor input pins the other way around. HIGH to input 1 and LOW to input 2.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Move DC motor backwards at maximum speed\nSerial.println(\"Moving Backwards\");\ndigitalWrite(motor1Pin1, HIGH);\ndigitalWrite(motor1Pin2, LOW); \ndelay(2000);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Stop the DC motor<\/h4>\n\n\n\n<p>To make the DC motor stop, you can either set the enable pin to LOW, or set both input 1 and input 2 pins to LOW. In this example, we&#8217;re setting both input pins to LOW.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Stop the DC motor\nSerial.println(\"Motor stopped\");\ndigitalWrite(motor1Pin1, LOW);\ndigitalWrite(motor1Pin2, LOW);\ndelay(1000);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Controlling the DC motor speed<\/h4>\n\n\n\n<p>To control the DC motor speed, we need to change the PWM signal duty cycle. For that, you use the <span class=\"rnthl rntliteral\">analogWrite()<\/span> function that accepts as arguments the GPIO pin and the duty cycle, as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>analogWrite(enable1Pin, dutyCycle);<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pwm-arduino-ide\/\" title=\"\">ESP8266 NodeMCU PWM with Arduino IDE (Analog Output)<\/a><\/p>\n\n\n\n<p>In our example, we have a while loop that increases the duty cycle by 5 in every loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Move DC motor forward with increasing speed\ndigitalWrite(motor1Pin1, HIGH);\ndigitalWrite(motor1Pin2, LOW);\n\nwhile (dutyCycle &lt;= 255){\n  analogWrite(enable1Pin, dutyCycle);   \n  Serial.print(\"Forward with duty cycle: \");\n  Serial.println(dutyCycle);\n  dutyCycle = dutyCycle + 5;\n  delay(500);\n}<\/code><\/pre>\n\n\n\n<p>When the while condition is no longer true, we set the duty cycle to 60 again.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dutyCycle = 60;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>&nbsp;After uploading the code to the ESP8266, apply power to the motor driver.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor-ESP8266-L298N-motor-driver.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control DC Motor with the ESP8266 and L298N motor driver\" class=\"wp-image-148721\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor-ESP8266-L298N-motor-driver.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor-ESP8266-L298N-motor-driver.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The motor will start spinning in different directions and speeds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control DC Motor with ESP8266 Demonstration\" class=\"wp-image-148723\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/Control-DC-Motor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial,l we&#8217;ve shown you how to control the direction and speed of a DC motor using an ESP8266 and the L298N motor driver. In summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To control the direction the DC motor is spinning you use the <strong>input 1<\/strong> and <strong>input 2<\/strong> pins;<\/li>\n\n\n\n<li>Apply LOW to input 1 and HIGH to input 2 to spin the motor forward. Apply power the other way around to make it spin backward;<\/li>\n\n\n\n<li>To control the speed of the DC motor, you send a PWM signal to the enable pin. The speed of the DC motor is proportional to the duty cycle.<\/li>\n<\/ul>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful.<\/p>\n\n\n\n<p><strong>Learn more about the ESP8266 with our resources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\" title=\"\"><strong>Home Automation Using ESP8266<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\">SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to control the direction and speed of a DC motor using an ESP8266 NodeMCU board and the&nbsp;L298N Motor Driver. We&#8217;ll start by explaining how the L298N motor driver &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU with DC Motor and L298N Motor Driver \u2013 Control Speed and Direction (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-dc-motor-l298n-motor-driver-control-speed-direction\/#more-148573\" aria-label=\"Read more about ESP8266 NodeMCU with DC Motor and L298N Motor Driver \u2013 Control Speed and Direction (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":148579,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,240,300,264],"tags":[],"class_list":["post-148573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-esp8266-projects","category-0-esp8266","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/ESP8266-Control-DC-Motor-Arduino-IDE-Speed-Direction.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/148573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=148573"}],"version-history":[{"count":19,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/148573\/revisions"}],"predecessor-version":[{"id":149681,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/148573\/revisions\/149681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/148579"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=148573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=148573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=148573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}