Uncategorized

# l298n + n20 notes

Q: Consistent Motor control – specifically 180 degrees
Justin PriedeI am struggling to get my motors to perform the same on each run. The first time I run the program, they will perform the same movement about 8/10 times. The second time I start the program, the movement does not perform the same as the last. It is significantly different. How can I get my motors…

raspbian gpio python pi-3b+ dc-motor

tlfong01
tlfong01
6962
(1) It is not clear what do you mean by “the same exact movement”. You need to be more precise. For example, can you tolerate a couple of degrees for a 180 degree movement, (2) It is not clear what do you mean by “decent speed and high torque”? (3) You need to specify more precisely. For example move 180 degrees in one second, or 0.1 second. (4) About the torques, you need to give a spec. Example, is a toy servo Towerpro MG996’s of 10kgf.cm good enough for you? Ref: html.alldatasheet.com/html-pdf/1131873/ETC2/MG996R/110/1/…

Justin Priede
Justin Priede
@tlfong01 I can spare a couple degrees but not much room for that… I need the action to be done in about a second and as for torque I am lifting about 50 grams. I am new to this so adding proper detail is harder for me.

tlfong01
tlfong01
6962
(5) Ah, your updated spec is good enough to start doing experiments. So, let us say, for positioning, the tolerance is 5 degrees for a 180 degree movement, (6) Your updated spec of 50grams is also good, but you need to specify the diameter of the wheel or pulley lefting up the 50g load. For example, if the wheel is 10cm, then radius is 5cm, and the torque is 50gf.5cm, which can be handled by a toy servo or DC motor. (7) The speed of 1 second to do the movement is also OK for toy motors.
(8) Your motor seems to be for high speed applications, such as drilling or racing cards. If you don’t need high speed, you can consider a gear motor, which decreases speed, but increases torque.
You might like to skim my answers to motor questions below, to see how I am playing with DC, BLDC, and stepper motors. (9) DC Motor Speed Control using PWM electronics.stackexchange.com/questions/510755/… (10) BLDC Motor Control electronics.stackexchange.com/questions/590373/… (11) DC Motor with encoder electronics.stackexchange.com/questions/566586/…
(12) You might also like to let us know more details of your application, a sketch or a photo would be helpful. Then I can start compiling an answer and upload here.
@Justin Priede: (13) Some more references you might find useful: Always stop the motor at the same position (a) electronics.stackexchange.com/questions/608578/… Always stop the motor at the same position (b) electronics.stackexchange.com/questions/610405/… ESP32 Motor Control Pulse Width Modulator (MCPWM) docs.espressif.com/projects/esp-idf/en/latest/esp32/…
@CoderMike: (14) Yes, so I tried it with a step motor: youtu.be/sJkf4uP4J6c. (15) More details at the end of this chat about using tmc2225 stepping motor driver: chat.stackexchange.com/rooms/134387/….
Now I am using a toy spring balance, and a bag of nuts as load/force to calibrate the stalled torque of the stepper motor I have been testing. My testing plan is summarized below: (a) write a program to move the motor forward and backward between 90 degrees and 45 degrees. Gradually increase the loading, ie, bag of nuts, until the motor can no longer move to the 90 degrees limit. Then do the calculation to get the stalled torque. Below is the video illustrating my plan.: youtu.be/mryE3ayH27o
And by trial and error, I found that the loading of about 88 gram is the loading that the motor can just be able to move to the 90 degrees point. For the weight bigger than that the motor cannot move to the 90 degrees, perhaps 80 or 70 degrees. The radius of the wheel is about 4.5cm. So the stalled torque is 80gf.4.5cm ~= 360gf.cm. I will compile an answer after the OP makes comments or suggestions.

Justin Priede
Justin Priede
359
Wow@tlfong01 I cannot thank you enough for the time you are putting into this! I appreciate it more than you know. 50 grams might be causing some misdirection here. That was a guess but it could be fine in terms of experimenting. I have a attachment to my motor that connects to a rectangle of posterboard/cardboard. I am trying to move that flap a tad more than 90 degrees, does not have to be a full 180 degrees to fold a shirt. I am automating this process here: amazon.com/BoxLegend-folding-Clothes-Laundry-flipfold/dp/…
@Milliways the code I have provided works, it is connected to my two motors and interacts with them however I tell them.
I did realize these motors are meant for high speed rather than slow specific movements but I would like to use these if possible rather than buying separate more motors.

tlfong01
tlfong01
6962
@Justin Priede, I agree with you of using the high speed motors in hand. I do have those kind of motor in my workshop. Perhaps I can test them later.
@NomadMaker: Yes, I very much with you that those 9V batteries are no good for testing motors, which might have a big starting current, causing the battery running out very soon.
@Justin Priede: (15) Now I am doing a feasibility study of your DC12V 12000rpm motor, checking out if it can do what my stepper motor can do – moving 180 degrees forward and backward, with a precision of a couple of degrees, using L298N DC motor driver, if possible. (16) My first impression of your AutoToolHome 12V motor, is (a) It is a popular model, (b) well designed, and (c) well documented, / to continue, …
(17) Some references: (17a) AutoToolHome 6-12V Mini DC Motor High Torque Gear for Traxxas R/C and Power Wheels PCB DIY Electric Drill amazon.com/dp/B01M58POHF (17b) Best Permanent Magnet Motors Buying Guide – GistGear Last Updated: 03/02/2022 gistgear.com/homeimprovement/electrical/permanent-magnet-mot‌​ors
(18) GistGear’s long list of motors actually include cheapy stepper motors 28BYJ48 (#13 on the list) which we might also use as a benchmark to compare and contrast with your DC motor.
Your shirt folder project is interesting. So I am googling for a spec: (19) How to Make a T Shirt Folder (Video) – WikiHow 2020sep23 youtube.com/watch?v=7opPALiCqLs&t=3s (20) How to Make a T Shirt Folder (Tutorial) – WikiHow 2021 oct21 wikihow.com/Make-a-T-Shirt-Folder. (21) Ah, locking down lunch time. See you later.
@Justin Priede: “I am trying to move that flap a tad more than 90 degrees,”.Actually it is easy to move the flap any degrees: youtu.be/Agk5eDSzpIE.
So I now know how to use a stepper motor to move a flap 180 degrees. However I found the stepper motor is too bulky, and seems not strong because it has no gear. A better choice might be the geared stepper 28Byj48. However, 28Byj48 is unipolar and I need to modify it to become bopolar for my stepper driver. After some consideration, I think I might use a DC motor, as suggested by the OP, but a small version, the geared N20 DC motor. I will also use the L298N, also suggested by the OP.
(22) I am using the N20 gear encoder motor for experiments. If you would also like to try a similar one, I would recommend the following: JGY-370GB Worm Gear DC Encoder Motor 12V 5-128RPM Cover Dustproof Self-Lock Reversible For Automatic Curtain Machinery Parts US16 aliexpress.com/item/….
(23) This worm gear motor is self locking and good for auto home curtain app, but I am not sure if also good for the T shirt folder.

Justin Priede
Justin Priede
359
Fri 10:14
@tlfong01 Again, I cannot thank you enough for the time you are putting in to help me… as a novice, it is very insightful to see your troubleshooting process and understand how you debug these sort of things.
I have access to a much bigger power supply today during a class which will hopefully give me more reliable results and might be the road to take… I will keep you updated with details of said power supply and how it impacts my code and motors.
If I understand, you are saying DC motors might be able to work for this project? Are you still testing different motors to see which works best? In doing research before I started, I also came across this video which accomplishes the same but in a different way than I would like to do. youtube.com/watch?v=rhWaHSUVGco
He uses a DC motor that seems as powerful as mine… it might provide some useful insight.

tlfong01
tlfong01
Ah, your comments and references are very good. I need to watch the video again before I can give some comments. See you later.

4 hours later…

tlfong01
tlfong01
6962
Fri 14:39
I am doing some googling on shirt folder. I skimmed through them and found your youtube video is the most useful.
(24a) Automatic clothes folding machine – Sherry Huang 2019jun13, 12,191 views, 40 subscribers

(24b) How to make a T shirt folding machine – 2016mar21, 359,386 views

(24c) Clothes Folders – BoxLegend, 2020jul17, 41,670 views

(24d) T-SHIRT Folding and Packaging Machine – Speedy-T, 2016sep20 785,022 views

(24e) How to Fold a Shirt – TM Lewin, 2010apr23, 1,637,846 views
(see full text)
I have no idea on which DC motor to use. For this prototyping stage, I will start feasibility study with DC motors with gear and encoder. I am not playing with BLDC motors, because I too little experience (and already fried a couple of those a bit expensive ones).

tlfong01
tlfong01
6962
Fri 15:01
And we need to look carefully your motor spec, to get a rough idea of how powerful it is:
user image

tlfong01
tlfong01
6962
Fri 15:18
Your motor only weighs 90g and the shaft diameter is only 2.5 mm. My big motors have shaft diameters of 3mm, 4mm, 5mm, and even 6 mm. So I would classify your motor as toy motor grade. Of course it can use a gear box to drive heavy loads, but only slowly.
user image

tlfong01
tlfong01
6962
Fri 15:42
I forgot how to use the L298N. So I am reading old posts to refresh my memory.
How to use motor drivers with H-bridge and PWM input, to control direction and speed of DC motors? – EESE 2020jul16 Viewed 2k times
electronics.stackexchange.com/…
(removed)

tlfong01
tlfong01
6962
Fri 15:56
user image
user image
user image

2 hours later…

tlfong01
tlfong01
6962
Fri 18:13
Now I am assembling the N20 + L298N module.
user image

tlfong01
tlfong01
6962
Fri 18:27
And I found the Last Minutes Engineers have a very newbie friendly tutorial and pinout.
user image

2 hours later…

tlfong01
tlfong01
6962
Fri 20:39
Now I am using a scope to display the N20 quadrature encoder output signals A, B, when motor power is 12VDC. I found motor speed is about 56 rpm (3,333pps).
user image

2 hours later…

Justin Priede
Justin Priede
Fri 22:29
I also noticed we can use PWM – Pulse width modulation to control speeds and perhaps influence torque. I am not sure if it is needed

tlfong01
tlfong01
6962
Fri 22:41
Ah, (1) I am sure that for DC motor using L298N, PWM frequency does not affect speed, but duty cycle does, because more current flows each cycle. On the other hand, for stepper motor and stepper motor controller, step pulse frequency does affect speed, higher frequency means higher speed.
(2) But I have not been paying much attention to the relation between torque and frequency, because I know that if I don’t have enough torgue, I would get a stronger motor, or increase gear ratio, sacrificing speed, which is not critical in my Micky Mouse projects. Ah, bed time. See you tomorrow.

4 hours later…

Justin Priede
Justin Priede
yst 2:22
That all makes sense… Hope you sleep well. I know I have thanked you a few times but I wanted to thank you again. I cannot believe someone would give so much time to a stranger. You are helping me out more than you know.

17 hours later…

tlfong01
tlfong01
6962
yst 19:44
Now I have placed both the N20 motor board and L258N driver board on a bigger proto board. Next step is to do offline, manual, jumper signal/wiring test.
user image

tlfong01
tlfong01
6962
yst 19:57
Now I have extracted the L298N’s 3 control signals for each motor (enable, in1, In2) to a control signal routing board and by hand use jumpers wires to move motor forward and backward. Next step is to use Pico micropython program to set the control signals to move the motor.
Offline moving motor by hand jumper wires: youtu.be/ODebwJUs96w

Justin Priede
Justin Priede
yst 20:55
Looks sweet! Excited to see what results you get with your code.

1 hour later…

tlfong01
tlfong01
6962
yst 22:12
So I have modified my pico micropython tmc2225 stepper driver functions to L298N DC motor. The tmc2225 to L298n conversion is easy, because tmc2225 has 3 control signals: Enable, Direction, StepPulse, and L298N also has three, enable, in1, in2.
But before assigning timing parameter ranges to the new L298N control functions, I usually cheat by first using my favourite cheapy XY-PLWM PWM/sig gen to scan for a rough range that the L298N and N20 would operate/move, then do the software fine tuning. Now I am by hand set the In1, In2 to High and Low, and use the XY-LPWM sig gen to send the WPM signal (1kHz, 50% duty) to see if the motor moves. The YouTube results is shown below.
L298N + N20 test results, using XY-LPWM sig gen as PWM signal to the L298N enable terminal: youtu.be/Pl0WBa975CE

18 hours later…

tlfong01
tlfong01
6962
16:06
Now I apply 5kHz, 50% duty cycle to the L298N, N20 motor turns at 34 rpm.
user image

2 hours later…

tlfong01
tlfong01
6962
17:43
I also tried 2kHz and 1kHz, and found speed ~= 42 rpm. So I concluded that 1kHz is good benchmark frequency.
user image
user image
Next step is to keep applying 1kHz, but vary duty cycle, to confirm if higher duty cycle means higher speed.

1 hour later…

tlfong01
tlfong01
6962
18:55
Now then, 1 kHz, 50%, 90%, and 20% duty cycle results. (10% duty cycle cannot start motor.)
user image
user image
user image
Results summary: 20% dc = 27.3 rpm, 50% dc = 4.16 rpm, 90% dc = 47.6 rpm

2 hours later…

tlfong01
tlfong01
6962
20:40

## L298N N20 Test Results

DutyCycle (%) 0 10 20 50 90 100

## Speed (rpm) 0 0 27.3 41.6 47.6 56

user image
The time has come to do some Pico MicroPython dual core programming.

1 hour later…

tlfong01
tlfong01
6962
21:45
Now I have written a very basic test function to run the N20 motor for four seconds. Next step is modifying this function to run the motor at specified speed (duty cycle) for a specific time (as small as a millisecond).

# L298N Config/Functions

l298nEnblPinNum = 20
l298nIn1PinNum = 21
l298nIn2PinNum = 22

def l298nDsblDriver(enblPinNum):
enblPin = gpOutPinDict[str(enblPinNum)]
setGpOutPinLow(enblPin)
return

def l298nSetDirCcw(in1PinNum, in2PinNum):
in1Pin = gpOutPinDict[str(in1PinNum)]
in2Pin = gpOutPinDict[str(in2PinNum)]
setGpOutPinLow(in1Pin)
setGpOutPinHigh(in2Pin)
return

def l298nSetDirCw(in1PinNum, in2PinNum):
in1Pin = gpOutPinDict[str(in1PinNum)]
in2Pin = gpOutPinDict[str(in2PinNum)]
setGpOutPinHigh(in1Pin)
setGpOutPinLow(in2Pin)
return

def l298nMoveFullSpeed(enblPinNum):
enblPin = gpOutPinDict[str(enblPinNum)]
setGpOutPinHigh(enblPin)
return

def testL298nV01(): # *** Move L298n + N20 motor at full speed for 4 seconds ***
printProgramName()
print(‘Begin testL298nV01(), …’)
l298nDsblDriver(l298nEnblPinNum)
l298nSetDirCw(l298nIn1PinNum, l298nIn2PinNum)
l298nMoveFullSpeed(l298nEnblPinNum)
pause(fourSeconds)
l298nDsblDriver(l298nEnblPinNum)
print(‘End testL298nV01().’)
return

# *** Sample Test ***

testL298nV01() # *** Move L298n + N20 motor at full speed for 4 seconds ***

”’ *** Sample Output ***

%Run -c \$EDITOR_CONTENT
========= ========= ========= ========= ========= ========= ========= =========
*** tmc2225test110.py 2022mar6hkt21:40 ***
========= ========= ========= ========= ========= ========= ========= =========

Begin testL298nV01(), …
End testL298nV01().

>
”’
Locking down bed time. Call it a day. See you tomorrow.

Categories: Uncategorized

This site uses Akismet to reduce spam. Learn how your comment data is processed.