2

I am building a robot for a school project. I use an Arduino Uno, 2 dc motor's and an Ultra Sonic range measurement module. I want the robot to be autonomous, he has to be able to move around on his own using the Ultra Sonic sensor. Important to mention is that I don't use a MotorShield to control my dc motors. This is my latest version of coding:


#include <Servo.h> //include Servo library
#include <AFMotor.h> //include DC motor Library
#define trigPin 12// define the pins of your sensor
#define echoPin 13
AF_DCMotor motor2(7); // set up motors.
AF_DCMotor motor1(6);


void setup() {
  Serial.begin(9600); // begin serial communitication  
  Serial.println("Motor test!");
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  motor1.setSpeed(105); //set the speed of the motors, between 0-255
    motor2.setSpeed (105);  
}

void loop() {

   long duration, distance; // start the scan
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
    motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
      motor2.run (BACKWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward! 
    motor2.run(FORWARD);  
  }  






}

enter image description here Now I added an ARDUINO MOTOR SHIELD REV3 to control my dc motors. Now the wheels are actually spinning, but after a few turns they stop. I think it is software related, but I am not 100% sure. Also I think I am connecting my motors correct to my motorshield, but don't adress them properly in my code. And I don't have any clue how to solve this problem. Can anybody help me with this?

2
  • 1
    Break it down into parts. Test each part independently. Then you'll know which one is (or ones are) broken. You already have prints in there to tell you what's going on - so what IS going on? Is it detecting obstacles etc? Please supply a circuit diagram. Are you attempting to drive motors directly from the Arduino outputs? This will not work, and I would be surprised if it hadn't already cooked your Arduino.
    – Mark Smith
    Commented Feb 15, 2017 at 18:00
  • Include a schematic of how you've connected everything and you'll probably get better answers to your questions.
    – linhartr22
    Commented Feb 16, 2017 at 21:25

1 Answer 1

-1

I think your problem is that you connected the positive wire to the 5v pin when it said 5v and not 9v. Also, I am assuming the 6 double AA is in series creating 9v. The thing is, double AA has a lot of amps. I tested it with my multimeter and it has 7 Amps, way to much. What I would do it connect a 9v battery to the voltage in pin-Vin. That pin can take a 9v but not 7 Amps produced by your 6 AA in series. You have 2 dc motors you said.Each is running I am assuming for each of them I notice draw about 1 Amp. 9v will supply enough and be safe. Try connecting a 9v to the Vin pin. I don't think your issue is software related.

5
  • 1
    Your understanding of the way current is drawn is flawed. A circuit will draw a current - the battery cannot somehow pump a specific current through a circuit. The short-circuit current produced by six AA batteries is not a useful or meaningful here.
    – Mark Smith
    Commented Feb 15, 2017 at 17:58
  • The battery is not drawing, I never said that. The Vin pin is taking in voltage from the battery. Measure the Amps of a 9v and a double AA battery, you will notice a difference. All I am saying is that the 5v outputs power and not input power. Please read what I said again, I think you're misinterpreting what I am saying.
    – Sean.D
    Commented Feb 15, 2017 at 19:07
  • Sean, I read it the same way Mark did. I'm not sure if this is a language issue or if you need to study a little more about what current and Amps mean.Circuits will always follow Ohms law. From this you would know that voltage and resistance determine how much current a circuit will draw from it's source. If the source can't deliver that much current you'll get unreliable or no results. If the source can deliver more current it doesn't force more current into the circuit.
    – linhartr22
    Commented Feb 16, 2017 at 21:22
  • I do know about amps and voltage. I thought I explained good enough, but looks like I did not. The pin he is using is the 5v, not the Vin pin, the Vin pin takes power into the arduino, the 5v supplies power out to a device connected to the arduino. I was also trying to mention that a double AA battery has 7 Amps, I tested it with my multi meter many times. He has a pack, meaning it is wired in series and not parrel. I am saying his circuit is wrong. & amps is to much for the arduino. A 9v supplies about 2 amps. That is max for arduino, so it will be fine. I am considering volts and amps.
    – Sean.D
    Commented Feb 17, 2017 at 5:00
  • Sean, it seems like I wasn't clear enough myself. I use the Vin port for my battery pack. I use a breadboard to make all the wiring. The breadboard itself is connected to the 5V port on my arduino. Commented Feb 17, 2017 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.