Go Down

Topic: Problems using PING sensor with PWM on 2 motors (Read 2982 times) previous topic - next topic


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: [Select]
// 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);
  digitalWrite(PING, HIGH);
  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,



Oct 12, 2012, 03:10 am Last Edit: Oct 12, 2012, 03:18 am by AWOL Reason: 1
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"
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.


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.



   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.


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.
The art of getting good answers lies in asking good questions.

Go Up