element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Issue with Ardumoto and SR04 sensor
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 9 replies
  • Answers 1 answer
  • Subscribers 394 subscribers
  • Views 588 views
  • Users 0 members are here
  • robot
  • arduino
Related

Issue with Ardumoto and SR04 sensor

Former Member
Former Member over 12 years ago

I am using the Ardumoto board on a Magician chassis with the HC-SR04 ultrasonic sensor to check for obstacles.

I am using standard code to check for obstacles (using the NewPing.h library). However, though the obstacle checking code and the code to run the motors work fine by themselves, when I use the two in conjunction, the ultrasonic sensor always returns the wrong distance (always about 4 - 5 cm).

 

Here is the code - any help appreciated!

 

#include <NewPing.h>

#define pingPin  8 

#define trigPin  7

#define MAX_DISTANCE 200

 

// pins for motor speed and direction

int speed1 = 3, speed2 = 11, direction1 = 12, direction2 = 13;

int startSpeed = 150;

 

long duration; //time it takes to receive bounced back signal

const int dangerThresh = 15; //threshold for obstacles (in cm)

 

NewPing sonar(trigPin, pingPin, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

 

// initial speeds of left and right motors

int left = startSpeed, right = startSpeed;

 

 

void setup() {

 

  pinMode(trigPin, OUTPUT);

  pinMode(pingPin, INPUT);

  pinMode(9, OUTPUT);  // led

 

// set the motor pins to outputs

  pinMode(speed1, OUTPUT);

  pinMode(speed2, OUTPUT);

  pinMode(direction1, OUTPUT);

  pinMode(direction2, OUTPUT);

 

  delay(1000); 

  // make both motors same speed

  left = startSpeed;

  right = startSpeed;

  Serial.begin(115200);

}

 

void loop() {

 

  // read the sensor

  delay(50);

  int distanceFwd = sonar.ping();

  distanceFwd = distanceFwd/US_ROUNDTRIP_CM;

 

  while ((distanceFwd<dangerThresh) && (distanceFwd!=0)) { //if path is blocked

    Serial.print("dist: ");

    Serial.println(distanceFwd);

    goBeep();

    distanceFwd = sonar.ping();

    distanceFwd = distanceFwd/US_ROUNDTRIP_CM;  

    // no logic yet to stop the motors... just testing...

  }

 

  // no obstacle, so move ahead

  // if I comment out the moveAhead() line below, I get the correct distance,

  // otherwise I always get a distance of about 4 - 5 cm

  moveAhead();}

 

 

// was using a buzzer first to beep when obstacle detected.

// but just have an LED on Pin 9 now...

void goBeep() {

  analogWrite(9, 20);                            

  delay(200);         

  analogWrite(9, 0);      

  delay(200);

}

 

void moveAhead() {

// set motor direction to forward

  digitalWrite(direction1, HIGH);

  digitalWrite(direction2, HIGH);

// set speed of both motors

  analogWrite(speed1,left);

  analogWrite(speed2,right);   

}

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 12 years ago in reply to billabott +1
    I have fixed the problem - thanks to the hint in your first reply! I was using the same power supply for both the ultrasonic sensor and the motors... I have just fixed up another power supply for the sensor…
Parents
  • billabott
    0 billabott over 12 years ago

    Please clarify.  Without changing the sketch, disconnect the pins that control the motor.  Does the ping))) sensor return correct values?

     

    If it does, then you could have a poorly isolated H-bridge that is dumping a surge (spikes) on the ground plane power supply rails. 

     

    There are two Ardumoto - Motor Driver Shields from Sparkfun:   DEV-09815 and DEV-09213

     

    What motors are you using?  Provide a full designation please.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • billabott
    0 billabott over 12 years ago

    Please clarify.  Without changing the sketch, disconnect the pins that control the motor.  Does the ping))) sensor return correct values?

     

    If it does, then you could have a poorly isolated H-bridge that is dumping a surge (spikes) on the ground plane power supply rails. 

     

    There are two Ardumoto - Motor Driver Shields from Sparkfun:   DEV-09815 and DEV-09213

     

    What motors are you using?  Provide a full designation please.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
  • Former Member
    0 Former Member over 12 years ago in reply to billabott

    Yes, if I comment out the line that says: moveAhead(), I get the correct distances in the serial monitor...

    If I disconnect the pins that control the motor, I get the right distance values too...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to Former Member

    You then said: "If it does then you could have a poorly isolated H-bridge that is dumping a surge (spikes) on the ground plane."

     

    How do I fix that? I am using the Ardumoto shield on top of a Uno.

     

      Ravi

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to Former Member

    Capacitors and Diodes or Separate Power Supplies.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to billabott

    I have fixed the problem - thanks to the hint in your first reply!

    I was using the same power supply for both the ultrasonic sensor and the motors... I have just fixed up another power supply for the sensor and voila! it works fine now!!

     

    Thank you billabot...

     

      :-)

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to Former Member

    You are welcome.  Glad I could help.  I will enjoy all 25 of those points too.  image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to billabott

    Hi Ravi,

    3 years later, I have the same exact problem. Could you tell me how exactly you powered up the ultrasonic sensor separately?

     

    Thanks so much!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 9 years ago in reply to Former Member

    Vcv must be powered off independent power like 5V usb

    Power supply :5V DC, quiescent current : <2mA,effectualangle: <15 ranging distance : 2cm~500 cm resolution : 0.3 cm 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 9 years ago in reply to clem57

    If you look at my post here, you'll see I simply ran it from the Arduino 5v.

    There isn't a circuit (small oversight on my part) but I simply used the Arduino 5v.

     

    Be careful with the load you are switching, in my case the NeoPixels were upsetting the readings, but a large capacitor stopped it.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube