{"id":146170,"date":"2024-02-21T15:54:24","date_gmt":"2024-02-21T15:54:24","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=146170"},"modified":"2024-02-22T11:27:50","modified_gmt":"2024-02-22T11:27:50","slug":"micropython-esp32-esp8266-dc-motor-l298n","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dc-motor-l298n\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 Control a DC Motor with the L298N Driver"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to control a DC Motor with the ESP32 and ESP8266 NodeMCU boards programmed with MicroPython. You\u2019ll learn how to make it go forward, backward, stop, and control its speed by creating your own MicroPython module. We&#8217;ll use the L298N motor driver, but other similar motor drivers should also be compatible.<\/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\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP32 ESP8266 NodeMCU Control a DC Motor with the L298N Driver\" class=\"wp-image-146188\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.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 class=\"rntbox rntclblue\">We have a similar tutorial using <strong>Arduino IDE<\/strong>: <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<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-esp32-esp8266-speed-direction\" title=\"\">Control DC Motor with ESP32 or ESP8266 \u2013 Speed and Direction<\/a><\/li>\n\n\n\n<li><a href=\"#wire-dc-motor-esp32\" title=\"\">Wiring a DC Motor to the ESP32 (L298N Motor Driver)<\/a><\/li>\n\n\n\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=\"#dcmotor-micropython-library\" title=\"\">Creating a Library to Control the DC Motor<\/a><\/li>\n\n\n\n<li><a href=\"#control-dc-motor-esp32-esp8266-micropython\" title=\"\">Control a DC Motor with the ESP32 or ESP8266 &#8211; MicroPython Code<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding with this tutorial, make sure you check the following prerequisites<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MicroPython Firmware<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-thumbnail\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"micorpython logo\" class=\"wp-image-148797\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?w=250&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/a><\/figure><\/div>\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Thonny IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>uPyCraft IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li>\n\n\n\n<li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>If you like to program using VS Code, there&#8217;s also an option: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-vs-code-pymakr\/\">MicroPython: Program ESP32\/ESP8266 using VS Code and Pymakr<\/a><\/p>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about MicroPython:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/p>\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=\"497\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/ESP32-dc-motor-wired.jpg?resize=750%2C497&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control DC Motor ESP32 - Parts required\" class=\"wp-image-146163\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/ESP32-dc-motor-wired.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/05\/ESP32-dc-motor-wired.jpg?resize=300%2C199&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\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266<\/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&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\">100nF 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.<\/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\" loading=\"lazy\" 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 + terminal<\/li>\n\n\n\n<li><strong>OUT2<\/strong>: DC motor A &#8211; terminal<\/li>\n\n\n\n<li><strong>OUT3<\/strong>: DC motor B + terminal<\/li>\n\n\n\n<li><strong>OUT4<\/strong>: DC motor B &#8211; 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 jumper is removed. Acts as a 5V output if 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\u2019re familiar with the L298N Motor Driver, let\u2019s see how to use it to control your DC motors.<\/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 your 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<h2 class=\"wp-block-heading\" id=\"dc-motor-esp32-esp8266-speed-direction\">Control DC Motor with ESP32 or ESP8266 &#8211; Speed and Direction<\/h2>\n\n\n\n<p>Now that you know how to control a DC motor with the L298N motor driver, let&#8217;s build a simple example to control the speed and direction of one DC motor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wire-dc-motor-esp32\">Wiring a DC Motor to the ESP32 (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 ESP32. Follow the next schematic diagram to wire the DC motor and the L298N motor driver to the ESP32.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1005\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-DC-Motor-Wiring-1.png?resize=1024%2C1005&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 wiring DC Motor L298N driver circuit diagram\" class=\"wp-image-146176\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-DC-Motor-Wiring-1.png?resize=1024%2C1005&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-DC-Motor-Wiring-1.png?resize=300%2C295&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-DC-Motor-Wiring-1.png?resize=768%2C754&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-DC-Motor-Wiring-1.png?w=1383&amp;quality=100&amp;strip=all&amp;ssl=1 1383w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\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>ESP32<\/strong><\/td><td>GPIO 12<\/td><td>GPIO 14<\/td><td>GPIO 13<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We&#8217;re using the GPIOs on the previous table to connect to the motor driver. You can use any other suitable GPIOs as long as you modify the code accordingly. Learn more about the ESP32 GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" title=\"\">ESP32 Pinout Reference Guide<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wire-dc-motor-esp8266\">Wiring a DC Motor to the ESP8266 NodeMCU (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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"1024\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/esp8266-dc-motor-l298n-wiring.png?resize=902%2C1024&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU DC Motor with L298N Wiring driver circuit diagram\" class=\"wp-image-146177\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/esp8266-dc-motor-l298n-wiring.png?resize=902%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 902w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/esp8266-dc-motor-l298n-wiring.png?resize=264%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 264w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/esp8266-dc-motor-l298n-wiring.png?resize=768%2C872&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/esp8266-dc-motor-l298n-wiring.png?w=1170&amp;quality=100&amp;strip=all&amp;ssl=1 1170w\" sizes=\"(max-width: 902px) 100vw, 902px\" \/><\/figure><\/div>\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>ESP32<\/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>We&#8217;re using the GPIOs on the previous table to connect to the motor driver. You can use any other suitable GPIOs as long as you modify the code accordingly. Learn more about the ESP8266 GPIOs: <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<h3 class=\"wp-block-heading\">Powering the LN298N Motor Driver<\/h3>\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 from the ESP32. 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 with 6V to 12V.<\/p>\n\n\n\n<p>The switch between the battery holder and the motor driver is optional, but it is very handy to cut and apply power. This way you don&#8217;t need to constantly connect and then disconnect the wires to save power.<\/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\n<h2 class=\"wp-block-heading\" id=\"dcmotor-micropython-library\">Creating a MicroPython Module to Control a DC Motor<\/h2>\n\n\n\n<p>Writing the code to control the DC motor is not difficult, but it requires some repetitive commands. It would be nice to have a module (library) that would allow us to control the motor speed and direction with a single command.<\/p>\n\n\n\n<p>With that in mind, we\u2019ll create a simple Python module with the basic commands to initialize a motor and control its speed and direction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the <em>dcmotor<\/em> Module<\/h3>\n\n\n\n<p>Before writing the code for this module, it\u2019s important to write down its functionalities:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The library will be called <em>dcmotor.py,<\/em> and it will have a single class called <em>DCMotor <\/em>with several methods.<\/li>\n\n\n\n<li>We should be able to initialize the motor using input pin 1, input pin 2, and the enable pin as follows:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motor1 = DCMotor(Pin1, Pin2, enable)<\/code><\/pre>\n\n\n\n<p><span class=\"rnthl rntliteral\">Pin1<\/span> and <span class=\"rnthl rntliteral\">Pin2<\/span> should be initialized as output pins, and the enable should be initialized as a PWM pin.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To move the motor forward, we want to use a method called <span class=\"rnthl rntliteral\">forward()<\/span> on the <span class=\"rnthl rntliteral\">DCMotor<\/span> object that accepts the speed as a parameter. For example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motor1.forward(speed)<\/code><\/pre>\n\n\n\n<p>The speed should be an integer number between 0 and 100, in which 0 corresponds to the motor stopped and 100 to maximum speed.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Similarly, to make the motor go backwards:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motor1.backwards(speed)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is also handy to have a command to make the motor stop, as follows:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>motor1.stop()&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Importing the <em>dcmotor.py<\/em> Module<\/h3>\n\n\n\n<p>Create a new file called <em>dcmotor.py<\/em> and copy the following code into that file.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Created by https:\/\/RandomNerdTutorials.com\/micropython-esp32-esp8266-dc-motor-l298n\/\r\n#This file includes a class to control DC motors\r\n\r\nclass DCMotor:\r\n  #the min_duty and max_duty are defined for 15000Hz frequency\r\n  #you can pass as arguments\r\n  def __init__(self, pin1, pin2, enable_pin, min_duty=750, max_duty=1023):\r\n    self.pin1 = pin1\r\n    self.pin2= pin2\r\n    self.enable_pin = enable_pin\r\n    self.min_duty = min_duty\r\n    self.max_duty = max_duty\r\n  \r\n  #speed value can be between 0 and 100\r\n  def forward(self, speed):\r\n    self.speed = speed\r\n    self.enable_pin.duty(self.duty_cycle(self.speed))\r\n    self.pin1.value(1)\r\n    self.pin2.value(0)\r\n\r\n  def backwards(self, speed):\r\n    self.speed = speed\r\n    self.enable_pin.duty(self.duty_cycle(self.speed))\r\n    self.pin1.value(0)\r\n    self.pin2.value(1)\r\n\r\n  def stop(self):\r\n    self.enable_pin.duty(0)\r\n    self.pin1.value(0)\r\n    self.pin2.value(0)\r\n        \r\n  def duty_cycle(self, speed):\r\n    if self.speed &lt;= 0 or self.speed &gt; 100:\r\n      duty_cycle = 0\r\n    else:\r\n      duty_cycle = int (self.min_duty + (self.max_duty - self.min_duty)*((self.speed - 1)\/(100-1)))\r\n    return duty_cycle<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/dcmotor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Upload that file to your ESP32 or ESP8266 board with the following name: <em>dcmotor.py<\/em>.<\/p>\n\n\n\n<p>If you&#8217;re using Thonny IDE, you can follow the next steps:<\/p>\n\n\n\n<p><strong>1)<\/strong> Copy the code provided to a new file on Thonny IDE.<\/p>\n\n\n\n<p><strong>2)<\/strong> Upload the code to your board. If you&#8217;re using Thonny IDE, go to <strong>File <\/strong>&gt; <strong>Save as&#8230;<\/strong> and then select <strong>MicroPython <\/strong>device.<\/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=\"220\" height=\"202\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/save-micropython-device-thonny-ide.png?resize=220%2C202&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Save to MicroPython Device\" class=\"wp-image-109022\"\/><\/figure><\/div>\n\n\n<p><strong>3)<\/strong> Save your file as <em><strong>dcmotor.py<\/strong><\/em>. There should be a file called <span class=\"rnthl rntliteral\">boot.py<\/span>. That file is created by default when you burn MicroPython firmware.<\/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=\"553\" height=\"409\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/save-main-py-thonny-device.png?resize=553%2C409&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save Script to MicroPython device ESP32 ESP8266\" class=\"wp-image-109023\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/save-main-py-thonny-device.png?w=553&amp;quality=100&amp;strip=all&amp;ssl=1 553w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/save-main-py-thonny-device.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\n\n\n<p><strong>4)<\/strong>&nbsp;Finally, click&nbsp;<strong>OK&nbsp;<\/strong>to proceed. The <em>dcmotor.py<\/em> module should have been uploaded successfully to your board.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the <em>dcmotor<\/em> Module Works<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">dcmotor<\/span> module contains a single class called <span class=\"rnthl rntliteral\">DCMotor<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>class DCMotor:<\/code><\/pre>\n\n\n\n<p>We use the constructor method <span class=\"rnthl rntliteral\">(__init__)<\/span> to initiate the data as soon as an object of the DCMotor class is instantiated.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def __init__(self, pin1, pin2, enable_pin, min_duty=750, max_duty=1023):\n  self.pin1 = pin1\n  self.pin2= pin2\n  self.enable_pin = enable_pin\n  self.min_duty = min_duty\n  self.max_duty = max_duty<\/code><\/pre>\n\n\n\n<p>A <span class=\"rnthl rntliteral\">DCMotor<\/span> object accepts as parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pin1<\/strong>: GPIO (output) that connects to L298N input 1 pin.<\/li>\n\n\n\n<li><strong>pin2<\/strong>: GPIO (output) that connects to L298N input 2 pin.<\/li>\n\n\n\n<li><strong>enable_pin<\/strong>: GPIO (PWM pin) that connects to L298N enable 1 pin.<\/li>\n\n\n\n<li><strong>min_duty<\/strong>: minimum duty cycle to make the motor move. This parameter is optional, and it\u2019s set to 750 by default. You may need to change this parameter depending on the frequency required to control your DC motor.<\/li>\n\n\n\n<li><strong>max_duty<\/strong>: maximum duty cycle to make the motor move. This parameter is set to 1023 by default.<\/li>\n<\/ul>\n\n\n\n<p>Then, create several methods to control the DC motor: <span class=\"rnthl rntliteral\">forward()<\/span>, <span class=\"rnthl rntliteral\">backwards()<\/span>, and <span class=\"rnthl rntliteral\">stop()<\/span>.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">forward()<\/span> and <span class=\"rnthl rntliteral\">backwards()<\/span> methods make the motor spin forward and backwards, respectively. The <span class=\"rnthl rntliteral\">stop()<\/span> method stops the motor.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">forward()<\/span> and <span class=\"rnthl rntliteral\">backwards()<\/span> methods accept the speed as a parameter. The speed should be an integer number between 0 and 100.<\/p>\n\n\n\n<p>Let\u2019s take a closer look at the <span class=\"rnthl rntliteral\">forward()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def forward(self, speed):\n  self.speed = speed\n  self.enable_pin.duty(self.duty_cycle(self.speed))\n  self.pin1.value(1)\n  self.pin2.value(0)<\/code><\/pre>\n\n\n\n<p>To make the motor move forward, set pin1 to 1 and pin2 to 0. The enable pin\u2019s duty cycle is set to the corresponding speed. The speed is an integer number between 0 and 100, but the duty cycle should be a number between the <span class=\"rnthl rntliteral\">max_duty<\/span> and <span class=\"rnthl rntliteral\">min_duty<\/span>. So, we have another method at the end of the code, called <span class=\"rnthl rntliteral\">duty_cycle()<\/span> that calculates the corresponding duty cycle value based on the speed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def duty_cycle(self, speed):\n  if self.speed &lt;= 0 or self.speed &gt; 100:\n    duty_cycle = 0\n  else:\n    duty_cycle = int (self.min_duty + (self.max_duty - self.min_duty)*((self.speed - 1)\/(100-1)))\n  return duty_cycle<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">backwards()<\/span> method works similarly, but pin1 is set to 0, and pin2 is set to 1.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def backwards(self, speed):\n  self.speed = speed\n  self.enable_pin.duty(self.duty_cycle(self.speed))\n  self.pin1.value(0)\n  self.pin2.value(1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"control-dc-motor-esp32-esp8266-micropython\">Controlling the DC Motor<\/h3>\n\n\n\n<p>Now that we understand how the <em>dcmotor.py<\/em> module works and that we&#8217;ve already imported it to your ESP32 or ESP8266 board, we can create a new script to control the DC motor using the library functionalities.<\/p>\n\n\n\n<p>The following code demonstrates how to use the library\u2019s functionalities to control the DC motor. The code is compatible with both the ESP32 and ESP8266 boards.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-esp32-esp8266-dc-motor-l298n\/\r\n\r\nfrom dcmotor import DCMotor\r\nfrom machine import Pin, PWM\r\nfrom time import sleep\r\n\r\nfrequency = 15000\r\n\r\npin1 = Pin(12, Pin.OUT)\r\npin2 = Pin(14, Pin.OUT)\r\nenable = PWM(Pin(13), frequency)\r\n\r\ndc_motor = DCMotor(pin1, pin2, enable)\r\n#Set min duty cycle (350) and max duty cycle (1023)\r\n#dc_motor = DCMotor(pin1, pin2, enable, 350, 1023)\r\n\r\ndc_motor.forward(50)\r\nsleep(2)\r\ndc_motor.stop()\r\nsleep(3)\r\ndc_motor.backwards(100)\r\nsleep(2)\r\ndc_motor.forward(5)\r\nsleep(5)\r\ndc_motor.stop()<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_dcmotor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing libraries<\/h3>\n\n\n\n<p>To use the library\u2019s functionalities, you need to import the library into your code. We import the <span class=\"rnthl rntliteral\">DCMotor<\/span> class from the <span class=\"rnthl rntliteral\">dcmotor<\/span> library we\u2019ve created previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from dcmotor import DCMotor<\/code><\/pre>\n\n\n\n<p>You also need to import the Pin and PWM classes to control the GPIOs and the <span class=\"rnthl rntliteral\">sleep()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, PWM\nfrom time import sleep<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting the frequency<\/h3>\n\n\n\n<p>Set the PWM signal frequency to 15000 Hz. You can choose other frequency values. Note that for lower frequency values, the motor may not spin and make a weird beep sound.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>frequency = 15000<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Initializing GPIOs<\/h3>\n\n\n\n<p>We create three variables that refer to the motor driver\u2019s pin1, pin2, and enable pins. These are connected to GPIO 12, 14, and 13, respectively. <span class=\"rnthl rntliteral\">pin1<\/span> and <span class=\"rnthl rntliteral\">pin2<\/span> are output pins, and the <span class=\"rnthl rntliteral\">enable<\/span> is a PWM pin.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pin1 = Pin(12, Pin.OUT)\npin2 = Pin(14, Pin.OUT)\nenable = PWM(Pin(13), frequency)<\/code><\/pre>\n\n\n\n<p>Then, initialize a DCMotor object with the <span class=\"rnthl rntliteral\">pin1<\/span>, <span class=\"rnthl rntliteral\">pin2<\/span>, and <span class=\"rnthl rntliteral\">enable<\/span> defined earlier:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor = DCMotor(pin1, pin2, enable)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> you may need to pass the <span class=\"rnthl rntliteral\">min_duty<\/span> and <span class=\"rnthl rntliteral\">max_duty<\/span> parameters depending on the motor and board you\u2019re using. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor = DCMotor(pin1, pin2, enable, 350, 1023)<\/code><\/pre>\n\n\n\n<p>Now you have a <span class=\"rnthl rntliteral\">DCMotor<\/span> object called <span class=\"rnthl rntliteral\">dc_motor<\/span>. You can use the methods to control the motor. Make the motor move forward at 50% speed:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor.forward(50)<\/code><\/pre>\n\n\n\n<p>Stop the motor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor.stop()<\/code><\/pre>\n\n\n\n<p>Move the motor backwards at maximum speed:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor.backwards(<strong>100<\/strong>)<\/code><\/pre>\n\n\n\n<p>Move the motor forward at 5% speed:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>dc_motor.forward(<strong>5<\/strong>)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Run the previous code on your ESP32 or ESP8266 board. The motor will move forward at 50% speed, backward at 100% speed, and then forward again at 5% speed. You can experiment with the methods and pass different speeds as an argument to see how your DC motor behaves.<\/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\/01\/DC-Motor-Spinning.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mini DC Motor Spinning\" class=\"wp-image-146189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/DC-Motor-Spinning.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/DC-Motor-Spinning.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<h1 class=\"wp-block-heading\">Wrapping Up<\/h1>\n\n\n\n<p>In this tutorial, you\u2019ve learned how to control a DC motor using the L298N motor driver with the ESP32 or ESP8266 programmed with MicroPython. We\u2019ve created a Python module with methods to easily control the motor: the d<em>cmotor.py<\/em> library. <\/p>\n\n\n\n<p>Now, you can use this library in your projects to create <span class=\"rnthl rntliteral\">DCMotor<\/span> objects and use the library methods to control the motor: <span class=\"rnthl rntliteral\">forward()<\/span>, <span class=\"rnthl rntliteral\">backwards()<\/span> and <span class=\"rnthl rntliteral\">stop()<\/span>.<\/p>\n\n\n\n<p>If you prefer programming using Arduino IDE, take a look at the following tutorial instead:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><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><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about MicroPython with the ESP32 and ESP8266 boards, check out:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" title=\"\">MicroPython Programming with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\" title=\"\">Free ESP32\/ESP8266 MicroPython Tutorials and Guides<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to control a DC Motor with the ESP32 and ESP8266 NodeMCU boards programmed with MicroPython. You\u2019ll learn how to make it go forward, backward, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 Control a DC Motor with the L298N Driver\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dc-motor-l298n\/#more-146170\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 Control a DC Motor with the L298N Driver\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":146188,"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":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[310,309,264],"tags":[],"class_list":["post-146170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/ESP32-ESP8266-DC-Motor-MicroPython-L298N.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\/146170","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=146170"}],"version-history":[{"count":10,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146170\/revisions"}],"predecessor-version":[{"id":148853,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146170\/revisions\/148853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/146188"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=146170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=146170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=146170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}