Raspberry Pi Pico(10): L298N module control motor

This article will use the L298N module to connect two N20 motors, and use Pi Pico to control the steering of the motor, so that the two wheels are forward or backward at the same time, while the wheels rotate forward and the other reverse, so that the direction of travel of the car will turn left and right.

[L298N]

L298N is a high-voltage, high-current motor driver chip produced by ST. It contains two H-bridge high-voltage and high-current full-bridge drivers, which can be used to drive inductive loads such as DC motors, stepping motors, and relay coils; Two Enable enable control terminals, under the condition of not being affected by the input signal, allow or prohibit only one logic power input terminal, so that the internal logic circuit part works at low voltage; using the L298N chip to drive the motor, the chip can drive one A two-phase stepper motor or a four-phase stepper motor can also drive two DC motors
L298N. The specifications are as follows:
  • Main control chip: L298N
  • Voltage: 5V
  • Drive voltage: 5V~35V
  • Current: 0mA~36mA
  • Drive current: 2A
  • Working temperature: -20℃~135℃
  • Maximum power: 25W

The L298N module is a 2-way H-bridge driver, which can drive two motors at the same time. After ENA and ENB are enabled (Enable), PWM signals can be input from IN1 and IN2 to drive the speed and direction of motor 1 from IN3 and IN2 respectively. IN4 inputs the PWM signal to drive the speed and direction of motor 2. The following table shows the action of the motor when the two input potentials of IN1 and IN2 (or IN3 and IN4) are high or low.
IN1(IN3)IN2(IN4)旋轉方向
Low (0)Low (0)Motor stop
High(1)Low (0)Forward
High(0)Low (1)Reverse
Low (1)Low (1)Motor stop

[Material]

  • Raspberry Pi Pico x1
  • L298N module x1
  • N20 motor x2
  • Wheels x2
  • Breadboard x1
  • Breadboard power module x1
  • wires x N

[Wiring diagram]

The 5V power supplied by the breadboard is directly connected to the positive (+5V) and GND poles of the L298N module. The positive and negative wires of the two motors are connected to the left and right sides of the L298N module. Please refer to the following wiring diagram. The other pins are as follows:
Pi PicoL298N
Pin 1(GP0)IN4
Pin 2(GP1)IN3
Pin 4(GP2)IN2
Pin 5(GP3)IN1
Pin 8(GND)GND


[Code]

import utime
import machine

motor1a = machine.Pin(0, machine.Pin.OUT)
motor1b = machine.Pin(1, machine.Pin.OUT)
motor2a = machine.Pin(2, machine.Pin.OUT)
motor2b = machine.Pin(3, machine.Pin.OUT)

led = machine.Pin(25, machine.Pin.OUT)   #Set the LED on the development board (Pin25) 
led.value(0)
utime.sleep(0.5)
led.value(1)
utime.sleep(0.5)
led.value(0)

#functions
def forward():   #forward
    led.value(1)
    motor1a.high()
    motor1b.low()
    motor2a.low()
    motor2b.high()    
    
def backward():   #backward
    led.value(1)
    motor1a.low()
    motor1b.high()
    motor2a.high()
    motor2b.low()    

def left():      #left turn
    led.value(1)
    motor1a.high()
    motor1b.low()
    motor2a.high()
    motor2b.low()    

def right():     #right turn
    led.value(1)
    motor1a.low()
    motor1b.high()
    motor2a.low()
    motor2b.high()    

def stop():     #stop
    led.value(0)
    motor1a.low()
    motor1b.low()
    motor2a.low()
    motor2b.low()    
    
while True:       
    forward()      #forward  
    utime.sleep(2)
    stop()
    utime.sleep(1)

    backward()     #backward
    utime.sleep(2)
    stop()
    utime.sleep(1)
    
    left()        #left turn
    utime.sleep(2)
    stop()
    utime.sleep(1)
    
    right()       #right turn
    utime.sleep(2)
    stop()
    utime.sleep(1)

[Result]

When the motor rotates, the LED light on the Pi Pico development board will be on. When the motor stops rotating, the LED light will go out. The following execution results make the two motors rotate forward for 2 seconds and stop for 1 second, then reverse for 2 seconds and stop for 1 second. , And then, while rotating forward and reverse for 2 seconds, stop twice for 1 second to make the wheels turn left and right.

[Reference]

Post a Comment

Previous Post Next Post