{"id":146195,"date":"2024-05-21T09:21:52","date_gmt":"2024-05-21T09:21:52","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=146195"},"modified":"2024-05-23T13:40:54","modified_gmt":"2024-05-23T13:40:54","slug":"raspberry-pi-pico-dc-motor-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dc-motor-micropython\/","title":{"rendered":"Raspberry Pi Pico: Control DC Motor with L298N Motor Driver (MicroPython)"},"content":{"rendered":"\n<p>Learn how to control a DC Motor speed and direction using the L298N motor driver with the Raspberry Pi Pico programmed with MicroPython. DC motors are popular in electronics for robot projects and beyond. You&#8217;ll understand how to make it move forward, backward, stop, and control its speed by creating your own MicroPython module.<\/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\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Control DC Motor with L298N Motor Driver MicroPython\" class=\"wp-image-155674\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.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\"><strong>New to the Raspberry Pi Pico?<\/strong>&nbsp;Read the following guide:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>Throughout this tutorial, we\u2019ll cover the following contents:<\/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=\"#connect-dc-motor-raspberry-pi-pico\" title=\"\">Wiring the DC Motor and L298N to the Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"#dcmotor-micropython-library\" title=\"\">Creating a MicroPython Module to Control the DC Motor<\/a><\/li>\n\n\n\n<li><a href=\"#control-dc-motor-raspberry-pi-pico-micropython\" title=\"\">Control a DC Motor with the Raspberry Pi Pico &#8211; MicroPython Code<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Our Raspberry Pi Pico eBook<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing, make sure you follow the next prerequisites.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"prerequisites\">MicroPython Firmware<\/h3>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<p>Alternatively, if you like programming using VS Code, you can start with the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-vs-code-micropython\/\">Programming Raspberry Pi Pico with VS Code and MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>You&#8217;ll also 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\/01\/Control-DC-Motor-with-Raspberry-Pi-Pico-L298N.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Control DC Motor with Raspberry Pi Pico\" class=\"wp-image-146217\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Control-DC-Motor-with-Raspberry-Pi-Pico-L298N.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Control-DC-Motor-with-Raspberry-Pi-Pico-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\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/l298n-motor-driver\/\" target=\"_blank\" rel=\"noopener\" title=\"\">L298N motor driver<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mini-dc-motor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Mini DC Motor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ceramic-capacitors-kit\/\" target=\"_blank\" rel=\"noopener\" title=\"\">0.1 \u00b5F ceramic capacitor<\/a><\/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\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/4x-aa-battery-holder\/\" target=\"_blank\" rel=\"noopener\" title=\"\">4x AA Battery Holder<\/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<h1 class=\"wp-block-heading\" id=\"introducing-L298n-motor-driver\">Introducing the L298N Motor Driver<\/h1>\n\n\n\n<p>There are several ways to control DC motors. We\u2019ll use the L298N motor driver, but any similar motor driver should work.<\/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<p>Using the L298N motor driver is suitable for most hobbyist motors that require 6V to 12V to operate. Additionally, it allows you to drive two DC motors simultaneously, perfect if you want to build a robot.<\/p>\n\n\n\n<p><strong>Already familiar with the L298N motor driver?<\/strong> <a href=\"#connect-dc-motor-raspberry-pi-pico\" title=\"\">Skip to this section<\/a>.<\/p>\n\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\"><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 use 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 your 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<p class=\"rntbox rntclblue\"><strong>Related content:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\" title=\"\">Raspberry Pi Pico with MicroPython: Output PWM Signals<\/a>.<\/p>\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<p class=\"rntbox rntclgreen\">You might also like reading: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)<\/a>.<\/p>\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=\"connect-dc-motor-raspberry-pi-pico\">Connecting a DC Motor to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>To follow this tutorial, you need to wire one DC motor to the Raspberry Pi Pico via the L298N motor driver. It\u2019s advisable to power up the motor using an independent power supply. Here we\u2019ll be using 4 AA 1.5V batteries that combined output approximately 6V. You can use any suitable power supply that doesn\u2019t exceed 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=\"Connecting a DC Motor to the Raspberry Pi Pico\" 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<h3 class=\"wp-block-heading\">Wiring the L298N Motor Driver to the Raspberry Pi Pico<\/h3>\n\n\n\n<p>The following table shows the connections we&#8217;ll make between the Raspberry Pi Pico and the L298N Motor Driver.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>L298N Motor Driver<\/strong><\/td><td><strong>Input 1<\/strong><\/td><td><strong>Input 2<\/strong><\/td><td><strong>Input 3<\/strong><\/td><td><strong>GND<\/strong><\/td><\/tr><tr><td><strong>Raspberry Pi Pico<\/strong><\/td><td>GPIO 3<\/td><td>GPIO 4<\/td><td>GPIO 2<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\" rntbox rntclgreen\">You can use any other digital pins. Learn more about the Raspberry Pi Pico GPIOs with our guide: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\" title=\"\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a>.<\/p>\n\n\n\n<p>Use the following diagram as a reference to wire the DC Motor and L298N Motor driver to the Raspberry Pi Pico.<\/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=\"1188\" height=\"1333\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/pico-dc-motor-l298n_wiring.png?resize=1188%2C1333&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DC Motor and L298N Wiring\" class=\"wp-image-146201\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/pico-dc-motor-l298n_wiring.png?w=1188&amp;quality=100&amp;strip=all&amp;ssl=1 1188w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/pico-dc-motor-l298n_wiring.png?resize=267%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 267w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/pico-dc-motor-l298n_wiring.png?resize=913%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 913w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/pico-dc-motor-l298n_wiring.png?resize=768%2C862&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1188px) 100vw, 1188px\" \/><\/figure><\/div>\n\n\n<p><strong>Note: <\/strong> we recommend soldering a 0.1 \u00b5F ceramic capacitor to the positive and negative terminals of each motor, as shown in the figure below to help smooth out any voltage spikes.<\/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=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/dc-motor-ceramic-capacitor.jpg?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"mini dc motor with ceramic capacitor\" class=\"wp-image-146204\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/dc-motor-ceramic-capacitor.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\/dc-motor-ceramic-capacitor.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\/dc-motor-ceramic-capacitor.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/dc-motor-ceramic-capacitor.jpg?w=1430&amp;quality=100&amp;strip=all&amp;ssl=1 1430w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>Additionally, you can solder a slider switch to the red wire that comes from the battery pack. This way, you can turn the power that goes to the motors and motor driver on and off.<\/p>\n\n\n\n<p>The Raspberry Pi Pico should be powered using another power supply (for testing purposes use your computer USB port).<\/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. 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<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\/Raspberry-Pi-Pico-DC-Motor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Creating a MicroPython Module to Control a DC Motor\" class=\"wp-image-146213\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Pico-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\/01\/Raspberry-Pi-Pico-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<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 <span class=\"rnthl rntliteral\">DCMotor<\/span> 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, we want a method called <span class=\"rnthl rntliteral\">backwards()<\/span>:<\/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\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-dc-motor-micropython\/\n\nclass DCMotor:\n    def __init__(self, pin1, pin2, enable_pin, min_duty=15000, max_duty=65535):\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\n\n    def forward(self, speed):\n        self.speed = speed\n        self.enable_pin.duty_u16(self.duty_cycle(self.speed))\n        self.pin1.value(1)\n        self.pin2.value(0)\n\n    def backwards(self, speed):\n        self.speed = speed\n        self.enable_pin.duty_u16(self.duty_cycle(self.speed))\n        self.pin1.value(0)\n        self.pin2.value(1)\n\n    def stop(self):\n        self.enable_pin.duty_u16(0)\n        self.pin1.value(0)\n        self.pin2.value(0)\n\n    def duty_cycle(self, speed):\n        if speed &lt;= 0 or speed &gt; 100:\n            duty_cycle = 0\n        else:\n            duty_cycle = int(self.min_duty + (self.max_duty - self.min_duty) * (speed \/ 100))\n        return duty_cycle\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/dcmotor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Upload that file to your Raspberry Pi Pico 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>Raspeberry Pi Pico<\/strong>.<\/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=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/thonny-ide-save-to-raspberry-pi-pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE, Save to Raspberry Pi Pico\" class=\"wp-image-146207\"\/><\/figure><\/div>\n\n\n<p><strong>3)<\/strong> Save your file as <em><strong>dcmotor.py<\/strong><\/em>.<\/p>\n\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=15000, max_duty=65535):\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 15000 by default. You may need to change this parameter depending on the frequency chosen 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 65535 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>. 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. 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_u16(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 class=\"rntbox rntclgreen\"><strong>Learn more about PWM with the Raspberry Pi Pico:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\">Raspberry Pi Pico: PWM Fading an LED (MicroPython)<\/a>.<\/p>\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_u16(self.duty_cycle(self.speed))\n  self.pin1.value(0)\n  self.pin2.value(1)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">stop()<\/span> method sets the duty cycle, <span class=\"rnthl rntliteral\">pin1<\/span> and <span class=\"rnthl rntliteral\">pin2<\/span> values to 0.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def stop(self):\n  self.enable_pin.duty_u16(0)\n  self.pin1.value(0)\n  self.pin2.value(0)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"control-dc-motor-raspberry-pi-pico-micropython\">Controlling the DC Motor<\/h3>\n\n\n\n<p>Now that we understand how the DC motor module works, 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.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-dc-motor-micropython\/\n\nfrom dcmotor import DCMotor\nfrom machine import Pin, PWM\nfrom time import sleep\n\nfrequency = 1000\n\npin1 = Pin(3, Pin.OUT)\npin2 = Pin(4, Pin.OUT)\nenable = PWM(Pin(2), frequency)\n\ndc_motor = DCMotor(pin1, pin2, enable)\n\n# Set min duty cycle (15000) and max duty cycle (65535) \n#dc_motor = DCMotor(pin1, pin2, enable, 15000, 65535)\n\ntry:\n    print('Forward with speed: 50%')\n    dc_motor.forward(50)\n    sleep(5)\n    dc_motor.stop()\n    sleep(5)\n    print('Backwards with speed: 100%')\n    dc_motor.backwards(100)\n    sleep(5)\n    print('Forward with speed: 5%')\n    dc_motor.forward(5)\n    sleep(5)\n    dc_motor.stop()\n    \nexcept KeyboardInterrupt:\n    print('Keyboard Interrupt')\n    dc_motor.stop()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/Control_DC_Motor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\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<h4 class=\"wp-block-heading\">Setting the frequency<\/h4>\n\n\n\n<p>Set the PWM signal frequency to 1000 Hz. You can choose other frequency values. Note that for lower frequency values, you may need to adjust the minimum duty cycle parameter (<span class=\"rnthl rntliteral\">min_duty<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>frequency = 1000<\/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. The <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(3, Pin.OUT)\npin2 = Pin(4, Pin.OUT)\nenable = PWM(Pin(2), 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, 15000, 65535)<\/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\">Testing the Code<\/h2>\n\n\n\n<p>Run this previous code on your Raspberry Pi Pico. If you&#8217;re using Thonny IDE, you just need to click on the green run button.<\/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=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing the Code Raspberry Pi Pico Control DC Motor with L298N Motor Driver MicroPython\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>If you want to upload this code to your board and run it when it boots, you need to save it as <em>main.py<\/em> Go to <strong>File <\/strong>&gt; <strong>Save as&#8230;<\/strong> &gt; <strong>Raspberry Pi Pico<\/strong>. Save the file with the name <em>main.py<\/em>.<\/p>\n\n\n\n<p>The motor will be spinning in different directions and speeds.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"146215\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/mini-DC-motor-stopped.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"mini DC motor stopped\" class=\"wp-image-146215\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/mini-DC-motor-stopped.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/mini-DC-motor-stopped.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"146214\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/mini-dc-motor-spinning.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Dc motor spinning\" class=\"wp-image-146214\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/mini-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\/mini-dc-motor-spinning.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>You can experiment with the methods and pass different speeds as an argument to see how your DC motor behaves. 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<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to control a DC Motor using the L298N motor driver with the Raspberry Pi Pico programmed using MicroPython. After understanding how the L298N motor driver works, controlling the DC motor is as easy as <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\" title=\"\">controlling digital outputs<\/a> to control the direction of the motors and producing <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\" title=\"\">PWM signals<\/a> to control their speed.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. We have similar guides for other microcontroller boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dc-motor-l298n\/\">MicroPython: ESP32\/ESP8266 Control a DC Motor with the L298N Driver<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-servo-motor-micropython\/\">Raspberry Pi Pico: Control a Servo Motor (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-stepper-motor-micropython\/\">Raspberry Pi Pico: Control a Stepper Motor (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dc-motor-l298n-motor-driver-control-speed-direction\/\" title=\"\">ESP32 with DC Motor and L298N Motor Driver \u2013 Control Speed and Direction (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Finally, if you would like to learn more about Raspberry Pi Pico, make sure you take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\"><strong>Learn Raspberry Pi Pico with MicroPython (eBook)<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">Raspberry Pi Pico Projects and Guides<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to control a DC Motor speed and direction using the L298N motor driver with the Raspberry Pi Pico programmed with MicroPython. DC motors are popular in electronics for &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: Control DC Motor with L298N Motor Driver (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dc-motor-micropython\/#more-146195\" aria-label=\"Read more about Raspberry Pi Pico: Control DC Motor with L298N Motor Driver (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":155674,"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":[324,326],"tags":[],"class_list":["post-146195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/Raspberry-Pi-Pico-DC-Motor-LN298N-MicroPython.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\/146195","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=146195"}],"version-history":[{"count":17,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146195\/revisions"}],"predecessor-version":[{"id":155781,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146195\/revisions\/155781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/155674"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=146195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=146195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=146195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}