L298N Motor Driver Board Pyboard 6V DC motors

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

L298N Motor Driver Board Pyboard 6V DC motors

Post by johnbanes » Fri May 24, 2019 2:51 am

Hello, I'm new to python and brand new to micro python. Working on a project with the L298N motor controller board, Pyboard V1.1 and a single 6v 71mA - 470mA 1.92Kgcm tourque DC motor. I have spent the last few hours trying to make the motor turn. This motor controller has three pins to control motor A, an enable pin that also reads the pwm signal for speed control, and two more pins that are in a high or low state to control the rotation direction of the motor. I am using pyBoard pins X1 for pwm and X2 and X3 for high or low . I searched the internet and not able to find a basic way to implement this with the pyboard and L298N motor controller. I am not well understanding how this should be done, is most of the problem. Can anyone help me with the details of making this motor turn?

User avatar
jimmo
Posts: 660
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by jimmo » Fri May 24, 2019 5:42 am

Hi,

The L298N is a dual full-bridge controller. To learn more about this, I'd start with a full-bridge (often called an H-bridge). The basic idea of an H-bridge is that it has two inputs (A and B). To go forward, you turn on A and turn off B, backward turn off A, turn on B.

The L298N extends this by ANDing A and B above with the enable.

Anyway, the simplest thing to test would be to turn on A, turn off B, and turn on EN. (Forget about PWM for now).

Code: Select all

from machine import Pin
en = machine.Pin('X1', machine.Pin.OUT)
a = machine.Pin('X2', machine.Pin.OUT)
b = machine.Pin('X3', machine.Pin.OUT)

a.on()
b.off()
en.on()
Then to go in reverse

Code: Select all

a.off()
b.on()
Next for speed control by PWM-img the enable pin. (This just means you're turning it on/off really fast, which means the motor is only turned on for some percentage of the time.

This should run at 1/4 speed.

Code: Select all

import pyb
pwm_timer = pyb.Timer(2, freq=1000)
pwm_channel = pwm_timer.channel(1, pyb.Timer.PWM, pin=en)
pwm_channel.pulse_width_percent(25)
(Adapted from https://docs.micropython.org/en/latest/ ... ckref.html )

I knew to use Timer channel 2, because X1 is connected to Timer2 (see https://store.micropython.org/product/PYBv1.1#image5)

User avatar
jimmo
Posts: 660
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by jimmo » Fri May 24, 2019 5:48 am

Also, can you link to the board you're using with the L298N? There may be some additional connections you have to make.

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by OutoftheBOTS_ » Fri May 24, 2019 10:46 pm

I play with motors a fair bit as love robotics and I find the L298N a really crap H-Bridge for any smaller motors. The L298N has a huge forward voltage of close to 3v. This means first of all it is super inefficient and gets really hot and drains your batteries very quickly. If you need to power 6v DC motor then you will need a 9v power supply as the H-Bridge chews 3v.

I tend to use the L9110s as it has only about 0.7v forward voltage and after using it on heaps of small DC motors have found it to work very well. Due to how it pins work I basiclly use 1 pin as a direction pin and the other pin as a PWM speed pin, this means you onbly need 2 pins per motor where as the L298N needs 3 pins per motor.

The L9110s is super cheap see https://www.aliexpress.com/wholesale?ca ... ext=L9110s

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by johnbanes » Sat May 25, 2019 12:58 am

Thanks for the info. getting going in the right direction. Here is the board I'm using with the L298N.

https://docs.micropython.org/en/latest/ ... ckref.html

Being powered with a 9.9V 1500mAh battery, I guess is should be fine to turn the 6V motors. Good to know about the inefficient controllers but for this version have to work with what's here. Thanks again!

User avatar
jimmo
Posts: 660
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by jimmo » Sat May 25, 2019 6:34 am

johnbanes wrote:
Sat May 25, 2019 12:58 am
Here is the board I'm using with the L298N.
Sorry, I meant the board that contains the L298N. e.g. https://tronixlabs.com.au/robotics/moto ... australia/

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by johnbanes » Sat May 25, 2019 1:46 pm

This is the motor controller board;
https://www.lelong.com.my/arduino-l298n ... Sale-P.htm

Thanks for the instructions Jimmo, the motor is turning!

User avatar
jimmo
Posts: 660
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by jimmo » Sat May 25, 2019 2:05 pm

johnbanes wrote:
Sat May 25, 2019 1:46 pm
This is the motor controller board;
https://www.lelong.com.my/arduino-l298n ... Sale-P.htm
Great, yep that answers my question. Nothing extra to do.
johnbanes wrote:
Sat May 25, 2019 1:46 pm
Thanks for the instructions Jimmo, the motor is turning!
Excellent, and speed control working too?

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by johnbanes » Sat May 25, 2019 8:01 pm

Yes sir, all good.

johnbanes
Posts: 10
Joined: Fri May 24, 2019 2:32 am

Re: L298N Motor Driver Board Pyboard 6V DC motors

Post by johnbanes » Sun Aug 04, 2019 12:13 am

I'm adding a second motor to the code, do I need to to also add another Timer.PWM for the second motor or can both motors use the same timer channel?

Post Reply