Avoidance Robot Problem: Motor not working with Ultrasonic S

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
gmontague0205
 
Posts: 2
Joined: Thu Jan 16, 2014 11:32 pm

Avoidance Robot Problem: Motor not working with Ultrasonic S

Post by gmontague0205 »

Hello, I'm working on an avoidance robot as a learning project during winter break. I'm fairly new to electronics. Anyway, here is the issue I am having...

I can control the robot to go forward and backwards with both motors running fine, and I can get the ultrasonic sensor to display accurate readings; however, when I incorporate the ultrasonic sensor with the motors, only one motor works.

Here is my source code broken down into the most basic form (with troubleshooting in comments):

Code: Select all

#include <AFMotor.h>
#define trigPin 3
#define echoPin 2

AF_DCMotor left_motor(2, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor right_motor(1, MOTOR12_64KHZ); // motor #2\

int maxDistance = 30;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  left_motor.setSpeed(255); 
  right_motor.setSpeed(255);
}

void forward(int delayTime) { 
  left_motor.run(FORWARD);
  right_motor.run(FORWARD);
  delay(delayTime);
}

void right(int delayTime) {
  left_motor.run(FORWARD);
  right_motor.run(BACKWARD);
  delay(delayTime);
}

int checkDistance(){
  long duration, distance;
  /* The following trigPin/echoPin cycle is used to determine the
  distance of the nearest object by bouncing soundwaves off of it. */ 
  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
 
  //Calculate the distance (in cm) based on the speed of sound.
  distance = duration/59.1;
  return distance;
}

void loop() {
  forward(500);     //both motors work fine (for first iteration through loop)
  delay(200);     //using extra delays for troubleshooting purposes..doesn't help
  backward(500);     //both motors work
  delay(200);
  if (checkDistance() >= maxDistance){
    Serial.println(" cm. No immediate obstacles detected. Moving forward.");
    delay(200);
    forward(2000);     //problem starts here... only left motor works
    delay(200);
    stop(1000);
    delay(200);
  } 
  else {
    delay(200);
    Serial.println(" cm. Obstacle detected. Changing direction.");
    delay(200);
    right(1500);      //problem here as well.. as only one motor turns (both are supposed to turn)
    delay(200);
    stop(1000);
    delay(200);
  }
}

Video of problem occurring (sorry for quality) http://youtu.be/-1DK9qSmqVM

Steps I've taken to troubleshoot issue:
1. Run on both external and USB power. External I have 4xAA power source for motors & 9V for Arduino.
2. Add two 100nF capacitors to motors Image
3. Isolate the issue.. Both motors work properly before checkDistance()--ultrasonic sensor--is used
4. Check serial output to see if ultrasonic sensor is working properly -- verified. Sample output:
41 cm. No immediate obstacles detected. Moving forward.
21 cm. Obstacle detected. Changing direction.
3 cm. Obstacle detected. Changing direction.
19 cm. Obstacle detected. Changing direction.
41 cm. No immediate obstacles detected. Moving forward.
Questions:
1. For the motors I used a thin single strand wire, could this cause a problem?

Image

2. After soldering connections for the ultrasonic sensor I used hot glue to support the joints, is this a problem?

Image

Also, here's a picture of my motor shield:

Image

I kind of hit a wall here, and not sure what else to do. I appreciate any thoughts you guys have. Thanks!

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Avoidance Robot Problem: Motor not working with Ultrason

Post by adafruit_support_bill »

That doesn't look like one of our shields. Where did you purchase it?

gmontague0205
 
Posts: 2
Joined: Thu Jan 16, 2014 11:32 pm

Re: Avoidance Robot Problem: Motor not working with Ultrason

Post by gmontague0205 »

Hello, I purchased it from eBay. Sorry I just realized this was a customer support forum. I was referred to post over here from stack exchange because I thought it was an Adafruit board.

waltr
 
Posts: 306
Joined: Wed Jun 12, 2013 5:01 pm

Re: Avoidance Robot Problem: Motor not working with Ultrason

Post by waltr »

Try over on the SoR forum.
A possible cause of your problem may be Voltage drop due to high current draw. Or the way the power is wired to the devices (US sensor/motors).

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”