I'm new to programming the arduino so I'm looking for a bit of guidance.
I'm attempting to use the arduino to drive stepper motors via a shift register. Components are Arduino Nano, Shift register 74HC595, and a 28bjy-48 stepper with ULN2003 driver.
I'm attempting to input bits using port manipulation because I believe it will enable a few daisy chained shift registers to turn the motors faster, and I can easily turn the motors in unison but to different degrees (θ).
My code is
void setup()
{
pinMode (11, OUTPUT); // Connected to Serial input on shift register
pinMode (8, OUTPUT); // Clock pin
pinMode (9, OUTPUT); // Latch pin
}
void loop()
{
for(i = 0; i < 512; i++)
{
PORTB = B00000001 // Send 0 on pin 11 and clock high on pin 8
PORTB = B00000001 // Send 0 on pin 11 clock high on pin 8
PORTB = B00000001 // Send 0 on pin 11
PORTB = B00001011 // Send 1 on pin 11 and latch pin 9 (output to stepper)
PORTB = B00000011 // Send 0 on pin 11 and latch pin 9 (output to stepper)
PORTB = B00000011 // Send 0 on pin 11 and latch pin 9 (output to stepper)
PORTB = B00000011 // Send 0 on pin 11 and latch pin 9 (output to stepper)
}
}
In my mind the shift register would look like this
In the IDE format your code with <ctrl> t. This will show some of your issues.
The clock pin has to be toggled (clocked) once for each bit you want to shift. Shift eight times for each set of stepper outputs so that the control bits end up at the same S.R. outputs each time.
What pattern of bits did you want to send? In general you start by setting LATCH to LOW. Then, for each data bit, set the DATA pin and turn the CLOCK on and off. For example, to send the pattern 0100 to the bottom 4 bits:
Then you would need to repeat such a pattern for the three other steps needed to complete a cycle. Then repeat the four steps 128 times for 512 total steps.
If the pattern is a single moving bit you can move it by doing one clock cycle:
but remember that that only works in one direction. To step in the other direction you need to send enough bits to shift the old '1' off the top each time.
PS: I don't think your stepper can do anywhere near to a million steps a second so why do you think you need direct port manipulation?
After a bit of tinkering I was able to get it to work.
I'm using 40 stepper motors (each requiring 4 bits to be loaded into the appropriate SReg output pins). The motors are stepped by shifting the 1 down from [1000] to [0100] and so forth every 2 ms. This essentially means the Arduino needs to output (every 4 cycles) 160 bits in 2ms to reinitialise all the steppers to [1000] or 80,000 bits per second.
I'm not sure if digitalWrite would have been able to do this, but there were other benefits to using port manipulation.
For example, if I wanted to turn Stepper A 10 times and the Stepper B 20 times it was very easy to write the program to:
Detect that Stepper A and Stepper B were to be turned a different amount
Start initialising the Stepper A's Shift register pins to [0000] after the 10th cycle.
Anyway the program works now and you knew the problem. Thank you
I was able to control 48 28BYJ-48 steppers to their speed limit (~15RPM) with a classic Nano, without port manipulation. With processor time left.
Leo..
Use SPI.transfer. or SPI.transfer16 to write to the shift register chain.
Then 100 steppers at ~12RPM (all at once) is still possible with a basic Nano, even with some acceleration.
Leo..