Iam using 28byj-48 stepper motor and I want to know how to control its speed without using any library. I got to know that we need to add delays in between steps but I don't know how much delay I need to add and how delay effects the rpm. It is very much helpful if anyone suggest me how to do this.
Vcc and ground need to be connected to your power source. Note, that it is risky to connect Vcc to the 5V pin of the Arduino. While that might work, when there is no load on the motor, a loaded stepper motor can draw rather high currents. Letting these current run through the Arduino might fry the voltage regulator or the power switching diode. High currents should always flow past the Arduino, not through it.
The other 4 pins (which are marked as IN1 to IN4 on my ULN2003 driver board) are for controlling the phases. You need to connect these 4 pins to 4 individual digital outputs on your Arduino. As these are the phases of the stepper motor, you need to activate them in the correct order. You have this pattern
for half steps. Each digit of each number represents the status of the corresponding phase; 1 means activated/HIGH, 0 means deactivated/LOW. To move the stepper motor forward, you move in the list of phases down. To move backwards, you move up in the list.
When using a stepper motor, the speed is determined by the number of steps per time interval. Let's assume, we want to use delay(), which is really easy, though not recommended (see explanation below), we can do steps and delay() inbetween:
This code would do a step and then delay for 10ms. Thus we have about 1 step every 10ms. With 64*64=4096 steps per revolution (the motor itself seems to have 64 steps; additionally there is a gear with ratio 64 mounted on it; this might be different for you) we get a full revolution every 4.096s. When you use half the delay(), the speed will be double: with 5ms delay we get a full rotation every 2.048s. So you can use the delay() to set the speed of the motor.
Now we need the step() function, that I used above. There are multiple ways of writing this in code. I would propose to put the pins and phases into arrays and iterate through them with a static variable, which holds the current position in our phase pattern. Something like this:
We hold the phase pattern in a 2-dimensional array. The variable current_step is declared static, so it will keeps its value between the executions of the function. Then - depending on the parameter forward - we increment or decrement the current step and take that value modulo 4 (the number of phase patterns). That ensures, that current_step stays in the index range of our phase pattern array and will wrap around from the end of the pattern to the start and vice versa. After that, we loop through the 4 stepper pins and set them according to the current element in the phase_pattern array. You can easily adept that code for half steps.
Now: Why not using delay()? This function will block the execution of other code. In that time the Arduino is just fiddling thumbs. In general that is considered bad practice. It is good, when you only need that one functionality and no responsiveness. But lets assume, you want to add a button, so that the motor reacts to your button presses. Long delay() calls will impact the responsiveness of your code. So instead you could use a non-blocking coding style. In the Arduino world this is easily done with the millis() function, which returns the number of milliseconds since startup and thus acts like a clock. Refer to the BlinkWithoutDelay example, that comes with the Arduino IDE, and to the many tutorials on the web, that you can find with search terms like "Arduino millis tutorial" or something like that.
The delay() call is determining the motor speed. If you want to change the speed, you need to change the value of the delay. You didn't mention any input device, so I just leave the speed constant. But of course you can define a variable for the delay and change that delay based on some input device. How exactly that is done depends on your requirements, mainly what type of input you want to use.
When you don't use any delay, then most likely your code will run too fast for the motor. The driver will set the motor phases very very fast. So fast, that the motor cannot follow that (because it has a specific inertia). (Much like the wheels of your car can loose the grip on the street and spin without moving the car, the stator of the motor can loose the magnetic grip on the rotor, which leads to further rotating phases, but a rotor, that is standing still) So you reach the maximum speed before that and it depends on the motor, the driver, how much load is on the motors and how much current your power source can give you (I once tried to provide 3 of these motors with a 1A power supply, but they moved very weak, until I upgraded to a bigger power supply, that can provide about 5A, I think). You need to test yourself, how fast you can drive your motors. So try to lower the delays step by step, until the motor cannot move in your setup anymore. Then you have the minimum delay, and the maximum speed.
Note: The delay() function won't be accurate for very low delays in the single digit milliseconds. In that case you might wanna use delayMicroseconds(), which is more accurate in that range and also can get in you in the microsecond range.
Curious, what if you put a resistor on it to reduce the current? Definitely would reduce the power of the motor, but assuming it turned would that work? Say the shaft only has an indicator needle on it... Flyback? Cap
capricorn2:
Curious, what if you put a resistor on it to reduce the current? Definitely would reduce the power of the motor, but assuming it turned would that work? Say the shaft only has an indicator needle on it... Flyback? Cap
capricorn2:
Thanks Robin, that's what I figured (flyback). It would have been nice to avoid the darlington and extra wiring is all, that would have been cool, but oh well, I guess I knew it wouldn't fly.
The kit and software drove the stepper directly from the Arduino pins and used no "libraries". Worked fine running the whole thing from a 9 volt battery. I did not run it for a really long time, but it did turn, step, both directions.
WELL! I had to go out to the shop to get the Arduino lesson manual and I was TOTALLY wrong about the stepper motor lesson!!!!!
It uses the ULN2003a chip which is a transistor array with built-in diodes to suppress the inductive reverse voltage when the stepper motor windings are switched off. So, you can't use the Arduino directly.
At $2 I really like these little "Stepper Motor Drivers"...
They are made for those little 28byj-48 steppers, but I use them for other stuff often. This is a general-purpose ULN2003 breakout board, with LEDs showing active bits. This can be used to drive small DC motors (one direction), high-power LEDs, incandescent lamps etc. up to 500 mA and 50 volts. The Data Sheet is here: -direct.com/Photos/uln2003.pdf
The factory we get them from usually sells them to Air Conditioner manufacturers who use them to control those little moving flaps on room air conditioners. They are also used in some air duct vane controllers. When I was looking for low-cost steppers and drivers for education these were the perfect hit.
Terry, thanks, I think you got me, cheap as the chip itself and ready to go. I really like the cheap motor too, they work well, nice that they have a gearbox built in too. Nothing wrong with pointing to your site IMO.
Since I've got you, do you know of a place to get tiny bipolar steppers? Fairly common again I think, about 10mm dia with stretched thread shaft. I didn't see any on that web site. I'm trying to miniturize.
Not sure how much you know about servos, I don't really like them but they are much simpler, I'm looking for "micro" servo with anything from 270 deg rot and up. I don't think they exist. Probably too many $ for me anyway. There are a few "360 deg", but those are misleading, they are continuous rotation, no pot.
Ever wanted to get a robot to turn at a precise angle, using only a few outputs of your Arduino or micro:bit ? All this for cheap ? That's the instructable for you ! In this instructable we will see how to drive a very cheap stepper motor using only 2 outputs of our controller and requiring only a 5V power supply !
The first thing that I'd recommend to start with, would be to learn more about stepper motors and the A4988 driver. Hey, but why do we need this driver ? Could we control a stepper motor without a driver ? The answer is no. Boards like Micro:bit and Arduino are good at processing information but not at giving out much current, and you need current to make a stepper motor move. To learn more about how both the motor and driver work this is the reference I would recommend. It is synthetic but also contains most of the information you'll need for the wiring.
But wait before trying to wire anything! Is the 28BYJ adapted to the A4988? If you do a quick search, you'll see that this motor comes rarely with the A4988 as a driver. If you read thoroughly the previous reference you might see why: our stepper is a unipolar motor while the A4988 is designed to drive bipolar motors, so we'll have to hack a bit our motor !
To make your motors compatible with the motor driver simply take the red wire out of the white connector. To do cut the connector to remove the red wire and cut the red wire of the motor. Then swap the yellow and pink cable on the connector. Keep the red wire and connector for the next step !
To get a cable out of the connector push the wire you want to remove in the connector and then push the visible metallic bit on the connector in with a sharp tool (above is a picture where I do this with my favorite knife, the opinel !), and finally pull and eventually the whole thing should come out as on the picture above. The last picture shows what the connector should look like at the end of those modifications: the order of the cable on the connector should be orange/pink/yellow/blue.
c01484d022