Run 2 Motors (28BYJ-48) with driver (ULN2003) at the same time

Hey

I have a question. I am building a car that follows a colored line but now i have a problem. Every time I program the two motor to run at the same time it never works but one time it works and since the i use the same lines of cod for that action.
Is it possible to run the to motors at the same time? and when How? or do I need a motor shield (adafruit).?

Best regards

Max

AccelStepper library makes this simple.

You didn't post your code.

void loop() //LINKS

{
color();

Serial.print(" Wert Rot Links: “); //Auf dem seriellen Monitor soll jeweils „Wert“
Serial.print(rotL, DEC); //mit der entsprechenden Farbe angezeigt
Serial.print(” Wert Gruen Links: “);//werden und dahinter der Wert, welcher in der
Serial.print(gruenL, DEC); //void color(); Funktion ausgelesen wurde.
Serial.print(” Wert Blau Links: “);
Serial.print(blauL, DEC);
Serial.print(” Wert Rot Rechts: “); //Auf dem seriellen Monitor soll jeweils „Wert“
Serial.print(rotR, DEC); //mit der entsprechenden Farbe angezeigt
Serial.print(” Wert Gruen Rechts: “);//werden und dahinter der Wert, welcher in der
Serial.print(gruenR, DEC); //void color(); Funktion ausgelesen wurde.
Serial.print(” Wert Blau Rechts: ");
Serial.print(blauL, DEC);

if (blauL < blauR)
{
Serial.println(" – (Motor Rechts)");
MotorR.step(1024);

}

else if (blauR < blauL)
{
MotorL.step(-1024);
Serial.println(" – (Motor Links)");

}

else if ( blauR < 10 && blauL < 10)
{
Serial.println(" – (Motor Rechts & Links)");
MotorL.step(-1024),
MotorR.step(1024);

else

{
MotorL.step (1024),
MotorL.step (1024),
MotorL.step (1024);
Serial.println();
}
}

Are you trying to run 2 motors (8 wires) with 1 ULN2003 (7 channels)? A ULN2803 has 8 channels. A tpicB595 shift register (8 channels) could run 2 of those little steppers with 3 wires from Arduino.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a complete copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

So thats the whole code that I have written.

#include <Stepper.h> 
int SPU = 2048;

Stepper MotorL(SPU, 3,5,4,6);
Stepper MotorR (SPU, A3, A1, A2, A0);

const int s0 = A15;
const int s1 = A14;
const int s2 = A13; 
const int s3 = A12;
const int out = A11;

int roteLED = 2; //Verbindung der LEDs mit dem Arduino festlegen
int grueneLED = 3;
int blaueLED = 4;

int rot = 0; //Variablen für LEDs benennen
int gruen = 0;
int blau = 0;

void setup()

{

Serial.begin(9600); //Serielle Kommunikation starten
pinMode(s0, OUTPUT); //Die Kontakte des Farbsensors werden als Output oder
pinMode(s1, OUTPUT); // Input festgelgt
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
pinMode(roteLED, OUTPUT); //Die LEDs werden als Output festgelegt
pinMode(grueneLED, OUTPUT);
pinMode(blaueLED, OUTPUT);
digitalWrite(s0, HIGH); //Die vier weißen LEDs am Farbsensor sollen leuchten
digitalWrite(s1, HIGH);
MotorL.setSpeed(15);
MotorR.setSpeed(15);

}

void loop()

{

color();//Diese Funktion wird am Ende des Codes festgelegt (s.“void color();“)

Serial.print(" Wert Rot: "); //Auf dem seriellen Monitor soll jeweils „Wert“
Serial.print(rot, DEC); //mit der entsprechenden Farbe angezeigt
Serial.print(" Wert Gruen: ");//werden und dahinter der Wert, welcher in der
Serial.print(gruen, DEC); //void color(); Funktion ausgelesen wurde.
Serial.print(" Wert Blau: ");
Serial.print(blau, DEC);

//Hier folgen die Befehle für die LEDs
if (blau < rot && blau < gruen) //Das gleiche bei Blau und Grün

{

Serial.println(" – (Blaue Farbe)");
MotorL.step (-1024);

}
else if (gruen < rot and gruen < blau or gruen == blau )

{
  Serial.println(" – (Gruene Farbe)");
  MotorR.step(-1024);
}



else

{

MotorR.step(1024);
Serial.println();
}
 
}



void color() //Hier werden die Werte vom Farbsensor ausgelesen und unter den

//entsprechenden Variablen gespeichert

{

digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
rot = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
blau = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
gruen = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

}

I don't see where you have the ULN2003 GND (pin 8 - E) connected to the Arduino GND, There must be a signal return path.
Your setup() code does not match that ( >:( Fritzing >:( ) picture. Please post a proper (and correct) schematic.

Hi,
Your fritzy is not readable, not enough resolution, please ditch the fritzy picture and please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Label the is and show your power supply.

Thanks… Tom… :slight_smile:

Here is the cleard up code, the Schematic and a few Images from the car

#include <AccelStepper.h>


// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

const int s0 = 6; //Verbindung der Farbsensorkontakte mit dem Arduino festlegen
const int s1 = 7;
const int s2L = 8; //Links Sensor
const int s3L = 9;
const int outL = 10;
const int s2R = 11; // Rechts Sensor
const int s3R = 12;
const int outR = 13;

int roteLED = 2; //Verbindung der LEDs mit dem Arduino festlegen
int grueneLED = 3;
int blaueLED = 4;

int rotL = 0; //Variablen für LEDs benennen Links
int gruenL = 0;
int blauL = 0;

int rotR = 0; //Variablen für LEDs benennen Rechts
int gruenR = 0; 
int blauR = 0;

AccelStepper stepper1(AccelStepper::FULL4WIRE, A11,A13,A12,A14);
AccelStepper stepper2(AccelStepper::FULL4WIRE, A3, A5, A4, A6);

void setup()

{

Serial.begin(9600); 
pinMode(s0, OUTPUT);          // LeuchtFarb LED
pinMode(s1, OUTPUT); 
pinMode(s2L, OUTPUT);         // Farbsensor Links
pinMode(s3L, OUTPUT);
pinMode(outL, INPUT);
pinMode(s2R, OUTPUT);         //Farbsensort Rechts
pinMode(s3R, OUTPUT);
pinMode(outR, INPUT);
pinMode(roteLED, OUTPUT); //Die LEDs werden als Output festgelegt
pinMode(grueneLED, OUTPUT);
pinMode(blaueLED, OUTPUT);

digitalWrite(s0, HIGH); //Die vier weißen LEDs am Farbsensor sollen leuchten
digitalWrite(s1, HIGH);

    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(100);
    
    stepper2.setMaxSpeed(300.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(100);
}

void loop()                                                                                       //LINKS

{

color();//Diese Funktion wird am Ende des Codes festgelegt (s.“void color();“)
Serial.print(" Wert Rot Links: "); //Auf dem seriellen Monitor soll jeweils „Wert“
Serial.print(rotL, DEC); //mit der entsprechenden Farbe angezeigt
Serial.print(" Wert Gruen Links: ");//werden und dahinter der Wert, welcher in der
Serial.print(gruenL, DEC); //void color(); Funktion ausgelesen wurde.
Serial.print(" Wert Blau Links: ");
Serial.print(blauL, DEC);
Serial.print(" Wert Rot Rechts: "); //Auf dem seriellen Monitor soll jeweils „Wert“            //Rechts
Serial.print(rotR, DEC); //mit der entsprechenden Farbe angezeigt
Serial.print(" Wert Gruen Rechts: ");//werden und dahinter der Wert, welcher in der
Serial.print(gruenR, DEC); //void color(); Funktion ausgelesen wurde.
Serial.print(" Wert Blau Rechts: ");
Serial.print(blauR, DEC);

if (blauL < rotL && blauL < gruenL && blauR < rotR && blauR < gruenR)
{
 stepper1.run();
 stepper2.run(); 
}
else if (rotL < blauL && rotL < gruenL && rotL < 20)                                               //Links

{
Serial.println(" – (Rote Farbe Links)"); 
}

else if (blauL < rotL && blauL < gruenL) 

{
Serial.println(" – (Blaue Farbe Links)");
    stepper1.run();
}

else if (gruenL < rotL && gruenL < blauL)

{
Serial.println(" – (Gruene Farbe Links)");
}

else{ 
Serial.println(); 
}

if (rotR < blauR && rotR < gruenR && rotR < 20)                                         //Rechts

{
Serial.println(" – (Rote Farbe Rechts)"); 
}

else if (blauR < rotR && blauR < gruenR) 

{
Serial.println(" – (Blaue Farbe Rechts)");
    stepper2.run();

}

else if (gruenR < rotR && gruenR < blauR)

{
Serial.println(" – (Gruene Farbe Rechts)");
}

else{ 
Serial.println(); 
}



}




void color() 



{

digitalWrite(s2L, LOW);                                                   //Links
digitalWrite(s3L, LOW);
rotL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);
digitalWrite(s3L, HIGH);
blauL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);
digitalWrite(s2L, HIGH);
gruenL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);

digitalWrite(s2R, LOW);                                                   //Rechts
digitalWrite(s3R, LOW);
rotR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
digitalWrite(s3R, HIGH);
blauR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
digitalWrite(s2R, HIGH);
gruenR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
}

Hi,
Ops picture;
f26acd25bb4540ab4a29e984e82c8a65db132fdf.png
You do not have the gnd of the stepper supply connected to the gnd of the Arduino.

Forget about the colour sensing for the moment.

Just write some code to get ONE STEPPER running, nothing else in the code.

If you wrote this in stages you should already have a bit of code like just to drive a stepper.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Forget about fritzy, use a pen/pencil and paper and label your power supplies and IC pins.
Just to control ONE STEPPER.

This will be the easiest way to troubleshoot your problem.

Tom… :slight_smile:

Hi,

Your stepper is 5 wire, have you identified the two windings and how to drive a Unipolar Motor.

Check this link.

Thanks.. Tom.. :slight_smile:

I just used the Symbols because the right one I didnt find but i use the link. But it didnt work with does Cod lines. The two motor get the signals but they never turn. They only shake a bit and heat up brithy fast. And when I programm it id diffrent programms it works. Its only broken wehen I fuse the programs to one.

Bild Auto

Hi,
Ops picture
20180211_121805.jpg

What is the 9V transistor battery powering?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :slight_smile: