Edit, this question has been answered in the comments by chrisl, and also after retesting the code with delay() which showed I forgot to update my lastupdate variable(eerst), I have answered it in the comments and chrisl possibly also will post their findings in a answer TLDR- 28BYJ-48 unipolar 5v not working only whining. run using transistors and code in the bottom of the post. transistors connected to 5V and resistors ranging from 100ohm to 1000ohm tried from Arduino to the transistors Trigger/Gate. - END TLDR
More Detailed I have some 5v and 12v 28BYJ-48 unipolar stepper motors,(I only connect one to the Arduino at a time) the common(tested with resistance meter) is connected to +, and I the other pins are connected to one BC336 transistor per pin so 4 in total. these transistors should handle 0.8A a piece and I measured the maximum amperage used by the coils to be 0.2+-0.02A for the 5v version at 5v, and 0.045 +- 0.001A for the 12v version at 5v. it makes sense if the 12v version doesn't work at 5v, I just wanted to test it to have an ultra-low-power stepper motor. however the 5v version should work, My setup allows a higher voltage drop in the coils in the motor and can handle a higher amperage than a normal driver however this is limited by the motors resistance ofcource. I drive it according to the steps in the datasheet with the standard method. and simplified my code to leave out all extra things and even hardcoding the values but still it only whiles and doesn't run the motor. I use the Arduino 5v to run the stepper motor but according to the datasheet and my measurements, you could drive more than one stepper motor on the 5v pin, even when running from USB power. I also set it really slow so that it should work(based on datasheet). I want to get this working like this because I am working on a custom budget stepper driver for Arduino which allows much simpler control, and which is much cheaper than the current drivers as well as that it by default can run higher voltage stepper motors as well, and one very specific functionality which almost everyone who has ever used one stepper motor or more has hoped that their stepper motor driver had.
I used pin 8 9 10 and 11 Broken Code:
unsigned long eerst=0;
unsigned long nu=0;
int state=0;
byte vals[8]={0b00000001,0b00000011,0b00000010,0b00000110,0b00000100,0b00001100,0b00001000,0b00001001};//made hardcoderd aray to make sure fault wasn't in the cyclic bitshifting
void setup() {
//0b76543210 portD <-- direction right is low num left is high num, keep confusing this since it differs on some platforms and not having used arduino for a while
DDRB=DDRB | 0b00001111;//only set 8,9,10, 11 as output do not change resst.
PORTB=0b00000000;//simplified from only turning of 8,9,10,11 to turning of all of portb
delay(1000);
eerst=millis();
}
void loop()
{
nu=millis();
if(nu-20>=eerst)
{
//forgot to update here PORTB=vals[state]; state++; if(state>7)//resets state { state=0; } } }
Working Code (might be changed for the long overflow stabilizing), changed variable names to make it clearer for others to read:
unsigned long TimeOfLastStep=0;
unsigned long CurrentTime=0;
int step=0;
byte vals[8]={0b00000001,0b00000011,0b00000010,0b00000110,0b00000100,0b00001100,0b00001000,0b00001001};//made hardcoderd aray to make sure fault wasn't in the cyclic bitshifting
void setup() {
DDRB=DDRB | 0b00001111;//only set 8,9,10, 11 as output do not change resst.
PORTB=0b00000000;
CurrentTime=millis();
}
void loop()
{
CurrentTime=millis();
if(CurrentTime-2>=TimeOfLastStep)//increased speed from 20ms delay to 2 ms
{
TimeOfLastStep=CurrentTime;//forgot this
PORTB=vals[state];
state++;
if(state>7)//resets state
{
state=0;
}
}
}
eerst
should be the timestamp variable. You need to set this inside the if statement. Currently you are running as fast as the Arduino can, probably too fast for the motor. Also you should handle the if statement line in theBlinkWithoutDelay
exampleif(nu - eerst >= 20)
. Then inside the if statement doeerst += 20;
. Please test this. I will write an answer if that solves it