Pages: [1]   Go Down
Author Topic: Problems using PING sensor with PWM on 2 motors  (Read 1123 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a newbie to Arduino programming.  I have an Arduino Uno with an Arduino Motor Shield and a PING ultrasonic sensor.  I'm using the millis() function to fire the PING sensor every 1000 milliseconds.  When I test just the PING, its light flashes every second and I get a good reading just as expected.  Then when I add the motor controller code in the main loop (using digitalWrite for direction and analogWrite for speed) the light still blinks every second and the motors run the same direction when no objects are detected and opposite directions when an object is 30cm or closer - seems to work great except there's not enough power from the USB to run the motors.  Now the really strange part.  When I unplug from the USB and use a 9V, the PING light starts blinking rapidly and the motors change unpredictably.  When I test just the PING sensor on the 9V, it works fine.  Why does the code seem to run differently on USB power versus 9V when motors are in the mix?  Are there conflicts in the pins I've chosen?  Is my code all wrong?  I'm perplexed.

My code:
Code:
// pins for motors and PING sensor
const int motorA = 3;
const int motorB = 11;
const int dirA = 12;
const int dirB = 13;
const unsigned int PING = 7;
// program variables
const unsigned int DISTANCE = 30; // in CM
unsigned long start = 1000;
boolean turn = false;

void loop()                     
{
  if (millis() > start) {
    turn = detect_object_ahead();
    if (turn == true) {
      digitalWrite(dirA, LOW);
      digitalWrite(dirB, HIGH);
    } else {
      digitalWrite(dirA, LOW);
      digitalWrite(dirB, LOW);
    }
    analogWrite(motorA, 150);
    analogWrite(motorB, 155);
    start = millis() + 1000;
  }
}

boolean detect_object_ahead() {
  pinMode(PING, OUTPUT);
  digitalWrite(PING, LOW);
  delayMicroseconds(2);
  digitalWrite(PING, HIGH);
  delayMicroseconds(5);
  digitalWrite(PING, LOW);
  pinMode(PING, INPUT);
  long cm = microseconds_to_cm(pulseIn(PING, HIGH));
  if (cm < DISTANCE) {
    return true;
  } else {
    return false;
  }
}

unsigned long microseconds_to_cm(const unsigned long microseconds) {
  return microseconds / 29 / 2;
}


Thanks for any help,

Brad
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I unplug from the USB and use a 9V
I hope the rest of the sentence says "2 amp regulated power supply" and not "smoke alarm battery"
« Last Edit: October 11, 2012, 08:18:33 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately it's a smoke alarm battery. smiley-roll I've also tried running it off of 5 AAs that the robot kit came with. They both behave the same.

Dave said I didn't connect the grounds. I'm just using one power source, isn't it all one ground?

I'm definitely a beginner with electronics. Let's say it's grossly underpowered. Would that cause the PING sensor to blink at an erratic fast pace? It also seems odd to me that when just on USB-power only, the PING behaves correctly even though the motors are barely turning.

Do I need to have a separate power supply for the motors?  Do I just need more amps?

Any other helpful hints are welcome.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

   I know this is an old post, but I was wondering if someone could help me out with a similar issue. I am using the Arduino mega 2560 I have two 5v dc motors hooked to a motor driver, and one ping sensor. What I want to do is power the ping sensor and motors independently. Right now they are all hooked up to the 5v on the Arduino board. Could you tell me the proper way to set up the power input. I have the option of either 9v or using a combination of double A's.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Right now they are all hooked up to the 5v on the Arduino board.
That won't supply near enough current to power any respectable sized motor. The motor shield should provide a way to supply external power for the motors.

The ping sensor can (easily) be powered by the Arduino.
Logged

Pages: [1]   Go Up
Jump to: