Thursday, May 30, 2013

Stepper Motors and Arduino: 28BYJ-48 with ULN2003

Today I will be exploring the world of stepper motors. I recently purchased a 28BYJ-48 stepper motor with a ULN2003 controller. They are available from a host of vendors for a few dollars and seem to be pretty popular in the Arduino community.

Getting started, there are several links you might find useful.

  • Basic information on the motor and controller as well as a sample sketch using the standard Stepper library.
  • The Stepper Library- This is the library that is included with the Arduino IDE. This library is set up to run a stepper without a gearbox, so it would have to be modified.
  • Stepper2.ino- This sketch includes a full set of functions that can be used to run the 28BYJ-48. It is discussed on THIS page, but it appears that the plans to convert it into a "real" library were never implemented. 
  • Custom Stepper Library- This library can be used to control a variety of steppers, but the default settings are for the 28BYJ-48
First I just wanted to get the stepper turning. I found THIS forum thread with some basic code to get it running. HERE is the code. It is very basic and does work. If you read THESE notes and still didn't understand how steppers work. This sketch might clear it up for you. Below is a video of the sketch working. 

Note how it is wired. You don't want to power the stepper from the Arduino. It can pull 90mA which is a lot for your little Arduino. I used my nifty breadboard power supply that I picked up for a few dollars on Ebay. Power goes to the left 2 male pins on the ULN2003 breakout (marked - + 5-12V). The jumper on the right just switches power to the motor. Removing it opens the circuit between the + power supply and the motor. Other than that, just use some female-female jumpers to connect the inputs to the whatever pins you are using on the Arduino. I am using an Arduino Mega 2560 with an Arduino sensor shield v4, so this is very easy to do. For those that don't know, the ULN2003 is just a little Darlington Array that allows us to switch power from an external source on and off rather than using the Arduino's on board power supply.

Next I decided that I would try the Arduino Stepper library first. While Stepper2 looks promising, I wanted something actively supported. Luckily THIS wiki provides code for using the standard library. HERE it is again, saved for posterity. I will note that 4096 steps resulted in 2 revolutions. Also, at the default steps/revolution 300 appeared to be a good maximum speed. 400 would not run at all. When I changed the steps/revolution to 2048, 10 worked well as a max. Another useful thing to know, the clockwise and counter-clockwise directions are defined when looking at the motor from the back (the side with the label). That is, from the perspective I used in the video above it will be backwards. 

If it is not working try some of the things below. If those don't help, Google the problem. If all else fails, comment, and I will see what I can do.
  • Reduce the speed. These motors only turn so fast before they bind up and stop moving.
  • Check the motor's temperature. The top speed of mine seemed to depend a bit on how warm it was.
  • Check wiring. Make sure your Arduino is hooked up correctly and you have defined the right pins in your code.
  • Check the jumper on the ULN2003 control board. It must be in place (bridging the right two pins).
Well that's about all I have at the moment. I have not dug into the other two stepper libraries I listed. I just wanted to do an intro so I could add stepper motors to my robo-arsenal. If I do any projects with them I will post about it.

-Matthew

12 comments:

  1. Hey, I've just bought one of these too - not tried it out just yet though. More of a hardware question though- do you know anywhere that supplies toothed gear wheels etc. that would fit the output shaft of this?

    ReplyDelete
    Replies
    1. I don't know. I would probably break out the calipers and look at www.mcmaster.com or similar. They have about everything. It might also be worth it to go down to the hobby shop and see if something for an RC heli or car would fit. It shouldn't be too hard to find something with a set screw that could be made to fit.

      Delete
    2. these websites have hobby stuff, you can check out, also you can make some out of wood yourself.
      sparkfun.com , hobbyking.com , makershed.com , bananarobotics.com

      you can also check out your local hobby stores.

      Delete
  2. Any idea how to code simultaneous control of 2 motors?

    ReplyDelete
    Replies
    1. I don't have another one so my answer is not tested. I'm assuming you're changing the code I have above for the stepper library.

      The step() command is blocking so you will have to jump between running the two. Sort of like multiplexing LEDs, the Arduino can't do real multitasking but can switch much faster than we can perceive. So we need to initialize a second stepper with the pins for that motor. Then you need to do very short pulses of steps.
      Add this to initialize 2 steppers
      Stepper small_stepper(STEPS, 8, 10, 9, 11);
      Stepper small_stepper2(STEPS, 12, 13, 14, 15);

      Add this to drive both of them at the same time.
      for( int x = 0; x <100; x ++) // take 100 steps
      {
      small_stepper.step(1); //step stepper 1
      small_stepper2.step(1); //step stepper 2
      }

      You may need to increase the step intervals. I'd play around with it. If you are doing other stuff like getting sensor data you will probably want to put the motor calls on a timer interrupt. There may be something built into one of the libraries for that.

      Delete
  3. Knowing that the power jumper is probably quite important, I want to be sure I understand its function. You said, "The jumper on the left just switches power to the motor." It switches the power from where when it is in what position? Are you saying the jumper is located on the left side of the photo, or that the jumper should be on the left two pins? This sort of information does not seem to be anywhere.

    ReplyDelete
  4. I added a picture to make it more clear. The jumper bridges the gap between the power supply and the stepper motor. Removing it just cuts power to the motor. If you look at the traces on the bottom it is interesting to note that removing it does not cut power to the ULN2003.

    I hope that answers your question.
    Matthew

    ReplyDelete
  5. So, to double check myself, removing the jumper removes the voltage that that the chip routs to the motor leads. And am I mistaken that there are potentially two sources of power. I.e. a 5v source that powers the chip logic, and then, another source that powers the motors, perhaps at a higher voltage if the jumper is in place. From what wire does the chip get its 5 volt source that the chip logic runs on if the jumper is removed?

    ReplyDelete
  6. Well it's been a while since I messed with this, but as far as I know that is how it works. I don't remember if I ever powered it off of anything besides 5V or not. Just unplug it if it gets hot.


    The chip is just a darlington array. It acts as a transistor. When the pins from the Arduino go HIGH it lets current flow from your external supply to the corresponding motor lead. The chip isn't actually a microcontroller. It is just a switch.


    I hope that makes sense. With the jumper in place, the motor should turn when you run the code. When you take the jumper off, it should stop.

    ReplyDelete
  7. hi, I am having problems with this, I am moving the jumper to the right (where it does not say 5-12) and putting 5 volt power to those pins where it says + and - and plugging in sequence my wires from the arduino 8 -11 to the board in sequance, but nothing happens, is it possible to blow the chip or am i doing something wrong, i did try a lot of things, before understanding to put the jumper on the right so ????
    big thanks if u can help me
    dan

    ReplyDelete
  8. It sounds like you got it working. To answer your question, while I have not burnt one up, I am 100% sure the answer is yes, you can blow up the chip. I doubt putting the jumper in the wrong position would do it though. I would be more worried about running more current through it than it can handle.


    Let me know if I can help with anything else. Thanks for reading!

    ReplyDelete
  9. For a newb, your picture and details was not clear at all on what to do with the uln board, even the next comment below is confused about when we remove the jumper where does the power come from. I was checking all over the place until i realised that I had to get external power, remove jumper, move it to the right, place wires of ext power in its place to give motor power, in the pict we don't clearly see (just barely) that there is a jumper it's in a shadow and very dark, so I was trying this without the %^%$# jumper, the other part about the power not matching the rails is again newb stuff I thought they were mirrored and the + and minus on the board are so tiny mached with my 49+ eyes was a great match, if u need a close up pict, i have one that cost me about 2 hours of fidling around !!!

    ReplyDelete