Control DC Motor-ESP32

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Control DC Motor-ESP32

Post by Mohamed » Sat Jun 13, 2020 5:38 pm

Hello, I have an esp32, 2 dc motor and L298N driver. when I run this code The motor doesn't move.

Code: Select all

from machine import Pin,PWM
from time import sleep
class Movement:
    def __init__(self,en,IN1,IN2):
        self.RM_ENA = Pin(en,Pin.OUT)
        self.RM_IN1 = Pin(IN1,Pin.OUT)
        self.RM_IN2 = Pin(IN2,Pin.OUT)
        self.RM_Speed = 1000
        self.freq = 1000

    def RightMotor_control(self,Direction):
       #move forward
        if (Direction ==1):
            self.RM_IN1.on()
            self.RM_IN2.off()
            #self.RM_ENA.on()
            en_pwm = PWM(self.RM_ENA,freq=self.freq,duty=self.RM_Speed)
         #move backward
        elif (Direction ==-1):
            self.RM_IN1.off()
            self.RM_IN2.on()
            #self.RM_ENA.on()
            en_pwm = PWM(self.RM_ENA,freq=self.freq,duty=self.RM_Speed)
        #stop
        else:
            self.RM_IN1.off()
            self.RM_IN1.off()
            self.RM_ENA.off()
But if I uncomment "#self.RM_ENA.on()", the motor moves and I can't control the speed.
How could I solve this problem,please?
Thanks in advance

User avatar
OlivierLenoir
Posts: 35
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Control DC Motor-ESP32

Post by OlivierLenoir » Sun Jun 14, 2020 8:09 am

The En pin is used to enable the H-bridge.

Image

Forward: Pin In1 = 1 and Pin In2 = 0
Reverse: Pin In1 = 0 and Pin In2 = 1
Off: Pin In1 = 0 and Pin In2 = 0

Code: Select all

from machine import Pin, PWM


class LM298(object):
    def __init__(self, en, in1, in2, freq=1000):
        self.freq = freq
        self.speed = 0
        self.p_en = PWM(Pin(en), freq=self.freq, duty=self.speed)
        self.p_in1 = Pin(in1, Pin.OUT)
        self.p_in2 = Pin(in2.Pin.OUT)
        self.p_in1(0)
        self.p_in2(0)

    def stop(self):
        self.p_en.duty(0)
        self.p_in1(0)
        self.p_in2(0)

    def forward(self):
        self.p_in2(0)
        self.p_en.duty(self.speed)
        self.p_in1(1)

    def reverse(self):
        self.p_in1(0)
        self.p_en.duty(self.speed)
        self.p_in2(1)

    def set_speed(self, speed):
        self.speed = min(1023, max(0, speed))


if __name__ == '__main__':
    from utime import sleep
    motor = LM298(2, 4, 5)
    motor.set_speed(500)
    motor.forward()
    sleep(2)
    motor.set_speed(200)
    motor.forward()
    sleep(2)
    motor.stop()
    motor.reverse()
    sleep(2)
    motor.stop()
Last edited by OlivierLenoir on Sun Jun 14, 2020 7:05 pm, edited 1 time in total.
Olivier Lenoir

Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Re: Control DC Motor-ESP32

Post by Mohamed » Sun Jun 14, 2020 3:49 pm

Thank you so much for your reply! It works for me but for init of pwm pin, I had to remove Pin.OUT
Like this

Code: Select all


self.p_en = PWM(Pin(en), freq=self.freq, duty=self.speed)
Now it works fine.
Thanks again

User avatar
OlivierLenoir
Posts: 35
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Control DC Motor-ESP32

Post by OlivierLenoir » Sun Jun 14, 2020 7:06 pm

You are right ;-).
My mistake. I've corrected my post.
Good luck.
Olivier Lenoir

Post Reply