Driving a 28BYJ-48 Stepper Motor?

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
IDWizard
Posts: 6
Joined: Tue Jul 25, 2017 5:01 pm

Driving a 28BYJ-48 Stepper Motor?

Post by IDWizard » Tue Jul 25, 2017 6:45 pm

This may not be possible, I know Python but my hardware knowledge is scant.

I have some 5V 28BYJ-48 DC Stepper motors. I can drive these with an Arduino UNO and a ULN2003 driver board with 4 output pins and an external 5V supply by sending a series of HIGH / LOW values on the pins. See: http://www.instructables.com/id/BYJ48-Stepper-Motor/

I wondered whether doing the same on a micro:bit would be possible. I tried:

[code]
from microbit import *

display.off()

order = [
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0],
[1,0,0,0],
[1,0,0,1],
[0,0,0,0]
]

while True:
for step in order:
pin16.write_digital(step[0])
pin15.write_digital(step[1])
pin14.write_digital(step[2])
pin13.write_digital(step[3])
print(".")
sleep(250)
[/code]

But no dice. I'm either doing it wrong or it is not technically possible with the bit. Advice gratefully received!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Driving a 28BYJ-48 Stepper Motor?

Post by mattyt » Wed Jul 26, 2017 6:20 am

Just off the top of my head...as you've mentioned, the 28BYJ-48 is a 5V stepper and the ULN2003 driver is also 5V. The Micro Bit is a 3V device. You probably need a level converter (something simple like the Adafruit BSS138 will do) in between the Micro Bit and the stepper driver.

But first check the output of pins 13-16 with a multimeter and ensure they are going high/low as you expect. If so then look at the high voltage level; you should note that it's 3V (or thereabouts) which is unlikely to be enough to control the stepper driver.

Good luck and keep us posted!

IDWizard
Posts: 6
Joined: Tue Jul 25, 2017 5:01 pm

Re: Driving a 28BYJ-48 Stepper Motor?

Post by IDWizard » Thu Jul 27, 2017 9:49 pm

Thanks for response and the pointers. I've been testing the bit with a multimeter and the REPL and that's telling me that a write_digital(1) on any of those pins will output around 3.25v. What was interesting to me was that pinX.write_digital(1) means "write high to that pin forever". For some reason I'd expected it to be a pulse with some kind of duration (my electronics naivete showing).

I ordered some: https://www.amazon.co.uk/gp/product/B00ZC6B8VM which should hopefully do the job of pushing this logical 3.25v to 5v so I can drive these motors. If you see a problem with that choice, let me know. Otherwise I'll let you know how it works out.

Thanks again.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Driving a 28BYJ-48 Stepper Motor?

Post by mattyt » Fri Jul 28, 2017 9:01 am

Looks like a solid choice. Keep us posted... :)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Driving a 28BYJ-48 Stepper Motor?

Post by pythoncoder » Sat Jul 29, 2017 10:03 am

I'm not so sure. The stepper motor has a 50Ω coil resistance so it will draw 100mA per phase at 5V. That level convertor is designed for logic level conversion: as far as I can see it isn't a motor driver.

The ULN2003 is capable of sinking up to 500mA so can easily drive the motor. It is designed for driving inductive loads with back-EMF protection diodes. And it can be driven by a 3.3V logic level. They are Darlington transistors with a 2.7KΩ series current limit resistor: the data sheet shows that an input voltage of 2.7V max is required for the device to sink 200mA.

In my opinion the ULN2003 is eminently suitable for the job. If it isn't working either it is damaged or is wired incorrectly. @IDWizard Perhaps you could supply details of how it is wired.

Regarding your question about pulses it is normal for a stepper motor at rest to have a voltage applied to one or two phases. This has the effect of holding the rotor in position against an externally applied torque.
Peter Hinch
Index to my micropython libraries.

IDWizard
Posts: 6
Joined: Tue Jul 25, 2017 5:01 pm

Re: Driving a 28BYJ-48 Stepper Motor?

Post by IDWizard » Sat Jul 29, 2017 7:43 pm

Thanks Pythoncoder, you encouraged me to take a closer look at my wiring and /facepalm it turned out I had the poles to my 5v supply switched...

Soon after discovering I was able to get the motor running. I've done a bit of tinkering and made a (very raw!) library if anyone else wants to try it. I am sure this could be made more efficient with bitwise operations instead of a list of high/low values.

My next goal is to see if I can get it to drive a core-xy movement (however slowly/weakly) - by driving two motors and interleaving the steps I was able to get "simultaneous" movement so some kind of "CNC" is possible but this is all new to me so we'll see.

Library is here (MIT):

https://github.com/IDWizard/uln2003

Very much appreciate the help from mattyt and pythoncoder!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Driving a 28BYJ-48 Stepper Motor?

Post by mattyt » Sun Jul 30, 2017 12:16 am

Congrats! Nice work. Sorry to lead you astray - and thanks to Pythoncoder for correcting my poor advice. :)

IDWizard
Posts: 6
Joined: Tue Jul 25, 2017 5:01 pm

Re: Driving a 28BYJ-48 Stepper Motor?

Post by IDWizard » Sat Sep 16, 2017 8:28 pm

Took me a while as a side project but I got there in the end and created a micro:bit based pen plotter using core-xy kinematics and micropython.

I'll need to update my GitHub library to provide the CoreXY class but the code to create this image was:

Code: Select all

    s1 = Stepper(FULL_STEP, microbit.pin3, microbit.pin4, microbit.pin5, microbit.pin6, delay=1)   
    s2 = Stepper(FULL_STEP, microbit.pin13, microbit.pin14, microbit.pin15, microbit.pin16, delay=1)    

    c = CoreXY(s2, s1)
    i = 6
    while i > 0:
        c.left(150 * i)
        c.down(150 * i)
        c.right(150 * i)
        c.up(150 * i)
        i = i - 1
As you can see below, the construction is somewhat ghetto and the movement speed is quite slow (a few mm a second) but given that it's held together with zip ties, using wooden dowels as rails and fishing line instead of belts I'm reasonably pleased with the results and it was cheap to make. OK, I used a 3d printer for some parts but they are very basic and could be replaced with wooden blocks.

Thanks again for the help/encouragement from mattyt and pythoncoder!
MicroPlotter.jpg
MicroPlotter.jpg (96.61 KiB) Viewed 15456 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Driving a 28BYJ-48 Stepper Motor?

Post by pythoncoder » Mon Sep 18, 2017 8:12 am

Glad you got it working :) An ingenious low-cost build.
Peter Hinch
Index to my micropython libraries.

IDWizard
Posts: 6
Joined: Tue Jul 25, 2017 5:01 pm

Re: Driving a 28BYJ-48 Stepper Motor?

Post by IDWizard » Mon Sep 18, 2017 8:43 am

Thanks. This may sound very obvious but at the moment I'm using two power supplies - a 5v to the bit and a separate 5v supply to the motors. I'd like to power both with a single 5v supply but be able to plug the bit just into the edge connector.

I think the best option for me is to get 3V to the GPIO pins on that edge connector as shown here:

https://www.kitronik.co.uk/blog/powerin ... -microbit/

Any advice on best option to step down a 5V supply (split from the motor supply) to the 3v the bit expects?

Post Reply