Let's Make Robots!

My robot don't move !

avoid obstacle
AttachmentSize
vue_face.jpg2.73 MB
DSC_0174.JPG2.54 MB
DSC_0175.JPG3.23 MB

Hello here !

I have a problem on the robot that I just built. The robot moves with the help of two stepper motor and detect obstacles using two sensors Ultrasonic.

For now, the robot should just be able to avoid obstacles. Then I could possibly add floors to add other functions as the return has a base for example.

So I try the robot and... nothing. The robot does not move exept one impulses (step ?) per second and per motor and theserial monitor (which gives me the distance of the two sensors) displays only the value of the left distance, the right distance is always equal to 0. I tested all the components separately with test codes and it all works correctly so no worries on that side.

I wonder if the problem is the robot itself (power, connection, ...) or his code.

So if you have an idea of the source of the probleme, please write it in the comment ;)

Here is the code :

#define trigPinD 2
#define echoPinD 3
#define trigPinG 4
#define echoPinG 5

//declare variables for the motor pins
int motorPin1D = 6;    // Blue   - 28BYJ48 pin 1
int motorPin2D = 7;    // Pink   - 28BYJ48 pin 2
int motorPin3D = 8;    // Yellow - 28BYJ48 pin 3
int motorPin4D = 9;    // Orange - 28BYJ48 pin 4
int motorPin1G = 10;    // Blue   - 28BYJ48 pin 1
int motorPin2G = 11;    // Pink   - 28BYJ48 pin 2
int motorPin3G = 12;    // Yellow - 28BYJ48 pin 3
int motorPin4G = 13;    // Orange - 28BYJ48 pin 4
                                           
// Red    - 28BYJ48 pin 5 (VCC)
                       

int motorSpeed = 1200;  //variable to set stepper speed
int lookupD[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
int lookupG[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
//////////////////////////////////////////////////////////////////////////////
void setup() {
 
//declare the motor pins as outputs
  pinMode
(trigPinD, OUTPUT);
  pinMode
(echoPinD, INPUT);
  pinMode
(trigPinG, OUTPUT);
  pinMode
(echoPinG, INPUT);
  pinMode
(motorPin1D, OUTPUT);
  pinMode
(motorPin2D, OUTPUT);
  pinMode
(motorPin3D, OUTPUT);
  pinMode
(motorPin4D, OUTPUT);
  pinMode
(motorPin1G, OUTPUT);
  pinMode
(motorPin2G, OUTPUT);
  pinMode
(motorPin3G, OUTPUT);
  pinMode
(motorPin4G, OUTPUT);
 
Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
 
int durationD, distanceD;
  digitalWrite
(trigPinD, LOW);
  delayMicroseconds
(2);
  digitalWrite
(trigPinD, HIGH);
  delayMicroseconds
(10);
  digitalWrite
(trigPinD, LOW);
  durationD
= pulseIn(echoPinD, HIGH);
  distanceD
= (durationD/2) / 29.1;
   
Serial.print(distanceD);
   
Serial.println(" cm droit");
   
   
int durationG, distanceG;
  digitalWrite
(trigPinG, LOW);
  delayMicroseconds
(2);
  digitalWrite
(trigPinG, HIGH);
  delayMicroseconds
(10);
  digitalWrite
(trigPinG, LOW);
  durationG
= pulseIn(echoPinG, HIGH);
  distanceG
= (durationG/2) / 29.1;
   
Serial.print(distanceG);
   
Serial.println(" cm gauch e");
   
   
   
if (distanceG < 10 ){
   clockwiseG
();
   anticlockwiseD
();
   
}
   
else if ( distanceD < 10 ){
   anticlockwiseG
();
   clockwiseD
();
   
}
   
else {
   clockwiseD
();
   clockwiseG
();
   
}
   
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwiseD(){

 
for(int iD = 0; iD < 8; iD++)
 
{
    setOutputD
(iD);
    delayMicroseconds
(motorSpeed);
 
}
 
}

void clockwiseD(){

 
for(int iD = 7; iD >= 0; iD--)
 
{
    setOutputD
(iD);
    delayMicroseconds
(motorSpeed);
 
}
}
 
 
void anticlockwiseG(){

 
for(int iG = 0; iG < 8; iG++)
 
{
    setOutputG
(iG);
    delayMicroseconds
(motorSpeed);
 
}
}

void clockwiseG(){

 
for(int iG = 7; iG >= 0; iG--)
 
{
    setOutputG
(iG);
    delayMicroseconds
(motorSpeed);
 
}
 
}

void setOutputD(int out){

  digitalWrite
(motorPin1D, bitRead(lookupD[out], 0));
  digitalWrite
(motorPin2D, bitRead(lookupD[out], 1));
  digitalWrite
(motorPin3D, bitRead(lookupD[out], 2));
  digitalWrite
(motorPin4D, bitRead(lookupD[out], 3));
}

 
void setOutputG(int out){

  digitalWrite
(motorPin1G, bitRead(lookupG[out], 0));
  digitalWrite
(motorPin2G, bitRead(lookupG[out], 1));
  digitalWrite
(motorPin3G, bitRead(lookupG[out], 2));
  digitalWrite
(motorPin4G, bitRead(lookupG[out], 3));
}

 

 

 

 

I try this code with only one motor and one sensor. The result was a bit better because it turned (very slowly and stopping random ways) and he has a better react to the detection of obstacles.

Sorry for spelling, i'm french ;)

thank, Théo.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I would check you have good power in your batteries.  I also note that you have said nothing about motor drivers.  The Arduino cannot provide enough power through it's own pins to drive a motor - you may destroy at least part of you microcontroller (mc) if you try to do so.  The pins on the mc can only provide around 20ma maximum.

If you read thorugh his code, you will see a comment about the ULN2003.

Looking at all your pics, and, reading your post shows no sign of you using any batteries. Your USB port can only supply 500mA at best. Steppers are current driven rather than voltage driven.

So the solution is to increase the power of the battery and connect the controller of my motor directly to the battery ?

But what kind of batterie should i use ? I have 1500mah 20C 11.1V LIPO battery, are they adapted or will they burn my arduino ?

 

Here is my motor and controller http://www.miniinthebox.com/fr/l-electronique-automobile-et-le-bricolage-5v-moteur-pas-a-pas-avec-chauffeur-uln2003_p340890.html

I have the same motors, and I made some tests. This motor contains gearbox, and therefore their output steps are too small. Try change motorSpeed = 5.

I've test with the "setspeed" set to 5 and the robot don't move at all with this setting. If you have the same motor, can you pass me your code please ? :)

Here is the schematics

You are using the little $5 stepper motors. The 2 middle pins need to be swapped either physically or via your code. This is just the way the steppers work. I personally would keep your stepper wiring the same, A,B,C,D and change your code.

 

I have made the modification needed in the code below. Cut and paste this, change nothing else, and it will work. (Probably)

int motorPin1D = 6;    // Blue   - 28BYJ48 pin 1
int motorPin2D = 8;    // Pink   - 28BYJ48 pin 2
int motorPin3D = 7;    // Yellow - 28BYJ48 pin 3
int motorPin4D = 9;    // Orange - 28BYJ48 pin 4
int motorPin1G = 10;    // Blue   - 28BYJ48 pin 1
int motorPin2G = 12;    // Pink   - 28BYJ48 pin 2
int motorPin3G = 11;    // Yellow - 28BYJ48 pin 3
int motorPin4G = 13;    // Orange - 28BYJ48 pin 4

I've already test all the compnent separetly and it work correctly.

the LED on the controller will not turn all when it is running, only 2 or 3 are lit on each controller and they turns off randomly.


After having switching the middle, the motors  began to spin but very choppy manner and with no reaction of the obstacle detection. But i feel we are on the right track :)

Here is the code which work perfectly with my motor :

//declare variables for the motor pins
int motorPin1 = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
  if(count < countsperrev )
    clockwise();
  else if (count == countsperrev * 2)
    count = 0;
  else
    anticlockwise();
  count++;
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

 

I know my explanation is not very precise but I have a bit of trouble with the English ^^

Thank for your help :)

I just figured out that you are stepping this motor manually. Is there any reason you are not using the built-in stepper commands?

You want to say the exemple in the arduino librairie  ?

I don't use those exemple because they not work correctly with my motor and make my motor very jerky and also very slow.