• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Control two DC motors with the L298N

Q&A Forum › Category: ESP32 › Control two DC motors with the L298N
0 Vote Up Vote Down
info8 asked 5 years ago

I want to control two DC motors with two L298N.

I want to use the class DCmotor.

How do I have to adapt this sample code so that I can control two DC motors?

from dcmotor import DCMotor
from machine import Pin, PWM
from time import sleep
frequency = 15000
pin1 = Pin(12, Pin.OUT)
pin2 = Pin(14, Pin.OUT)
enable = PWM(Pin(13), frequency)
dc_motor = DCMotor(pin1, pin2, enable)
#Set min duty cycle (350) and max duty cycle (1023)
#dc_motor = DCMotor(pin1, pin2, enable, 350, 1023)
dc_motor.forward(50)
sleep(5)
dc_motor.stop()
sleep(5)
dc_motor.backwards(100)
sleep(5)
dc_motor.forward(5)
sleep(5)
dc_motor.stop()
Question Tags: MicroPython
2 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
First, you need to declare pins for the second motor. For example:

motor2_pin1 = Pin(25, Pin.OUT)
motor2_pin2 = Pin(26, Pin.OUT)
motor2_enable = PWM(Pin(27), frequency)

You can use any other pins. I’m just showing an example.
Then, you need to create another DCMotor object using those properties. For example:

dc_motor2 = DCMotor(motor2_pin1, motor2_pin2, enable)

Then, to control this motor, you just need to use the forward() and stop() methods on this new DCMotor object.
For example:

dc_motor2.forward(20)
dc_motor2.stop()

I hope this is clear.

Regards,
Sara

0 Vote Up Vote Down
info8 answered 5 years ago

Thanks Sara, that helps me further.
Regards,
Konrad

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.