Show Posts
Pages: 1 ... 24 25 [26] 27 28 ... 31
376  Using Arduino / Sensors / Re: How do I check if ive got a faulty HC-SRO4? on: June 25, 2012, 10:38:03 am
Hi, that sketch is brilliant, I understand every line of it. But its still saying Ping 0cm!

What happens if you wire it to pins 11 and 12 (don't use pin 13)?  Here's the full sketch:

Be sure 12 is wired to trigger and 11 wired to echo, if you switch them, it won't work for sure.  Also, 0cm is the correct result if there's nothing within the ping range, so put a box or something foot away. I have 4 SR04 sensors and they "work" with the above sketch.  3 of my SR04 sensors do have a problem where they don't work well beyond about 50cm.  It seems to me that this may be a common problem with the SR04, so be sure your test object is fairly close to the sensor.

Also, just to make sure we're both using the same library, attached is my development version of NewPing (v1.4 pre-release).  Just replace these files with the ones already in the NewPing folder.

377  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 25, 2012, 09:39:26 am
I am having the same problem with two of my HC-SR04s. All I'm getting is a reading of 0cm regardless of what's in front of the sensor. These sensors both work with a different ultrasonic library. Does anyone have an idea of what may be causing this?

First, try using the example sketch below, it's the new example that's being included with NewPing.  Be sure you have the trigger and echo pins wired correctly.  Don't change anything in the example, even the pins used.

Also, when you say you have a problem with two sensors, does that mean you have other sensors that work and two that do not?  I've found the SR04 sensors tend to have a terrible quality control.  Of the 4 that I have, only 1 I would say works correctly.  The other 3 work, but get all wonky at distances beyond 50cm.

Finally, just to make sure we're both using the same library, attached is my development version of NewPing (v1.4 pre-release).  Just replace these files with the ones already in the NewPing folder.

378  Using Arduino / Displays / Re: LCD Bitmap - Updated v1.4 on: June 21, 2012, 04:47:37 pm
Thanks, it worked!.

I had changed the LCD reference for LiquidCrystal and of course it worked, but nice to have the latest files with the conditionals.



Thanks for testing.  I'll incorporate that in the next release.

379  Using Arduino / Sensors / Re: Ping))) and Midi on: June 21, 2012, 04:41:46 pm
Hello people,

I have a little problem which has big consequences. It belongs my bachelor thesis.

I use 3 Ping))) sensors and convert their value into midi messages via the midi library. The values the ping sensor send via midi were perfect for short distance but for a distance in 2,5m range it just jumps around. Is there a work around for it, or should I use other sensors.

Thank you so much for fast answers!


I've got an ultrasonic library that may give you better results.  I'm also looking for someone with a PING))) to test.  If you want to try it out, let me know and I'll get you a copy that works with PING))) sensors.  For more information on my library see this thread:,106043.0.html

380  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 21, 2012, 04:38:23 pm
FYI, further testing and I'm having problems with the combination of a SRF06, using one pin, and using the Teensy hardware.  It works, but only about half the time.  I'm going to try a few things this evening and I'll let everyone know the results.  The plan is to post the new release in the forum for people to test before I release this version.  Specifically, testing it using only one Arduino pin.

381  Using Arduino / Displays / Re: LCD Bitmap - Updated v1.4 on: June 21, 2012, 01:57:30 pm
maybe to help newbies, you could add a compile time check/error to tell them how to resolve the issue.
For example, LCDBitmap.h includes <LiquidCrystal.h>
fm's LiquidCrystal.h includes "LCD.h" so the include of "LCD.h" is not needed in LCDBitmap.h
(This will eliminate the preprocessor error).

Actually, when using a 4bit connection the LCDBitmap library was supposed to work without the need for the New LiquidCrystal library.  I've changed the code so it should work with or without the New LiquidCrystal library and attached the revision to my previous reply.

Adding an error if trying to use other connection types without the New LiquidCrystal library is a good idea too, and I'll probably be implementing that as well.

382  Using Arduino / Displays / Re: LCD Bitmap - Updated v1.4 on: June 21, 2012, 01:39:08 pm

I have installed arduino 1.0.1 and I just downloaded LCDBitmap library . I loaded the LCDBitmap_standard_4bit example and try to compile it.

This is the outcome:

/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:89:17: error: LCD.h: not found
In file included from LCDBitmap_Standard_4bit.cpp:2:
/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:110: error: expected `)' before ‘*’ token
/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:133: error: ISO C++ forbids declaration of ‘LCD’ with no type
/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:133: error: expected ‘;’ before ‘*’ token
LCDBitmap_Standard_4bit:4: error: no matching function for call to ‘LCDBitmap::LCDBitmap(LiquidCrystal*, int, int)’
/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:108: note: candidates are: LCDBitmap::LCDBitmap()
/home/ydirgan/SCRIPTS/Arduino/arduino-1.0.1/libraries/LCDBitmap/LCDBitmap.h:108: note:                 LCDBitmap::LCDBitmap(const LCDBitmap&)

The first line shows a LCD.h not found statement... where LCD.h header is?. Am I missing something?.

any advice will be appreciated.



I guess I missed this.  I had believed LCDBitmap would work without the "New LiquidCrystal" library.  But, that doesn't seem to be the case.  Attached to this message are replacement library files.  Save and replace these files with the existing files in your Arduino/libraries/LCDBitmap/ folder.  I don't have a 4bit project in front of me to test, but the attached files do compile with a stock Arduino 1.0.1 with the LCDBitmap_Standard_4bit sketch.

Let me know if it works.

383  Using Arduino / Sensors / Re: How do I check if ive got a faulty HC-SRO4? on: June 21, 2012, 10:47:58 am
Thanks guys, ill try all suggestions later today and let you know how I get on.

I did try newping but it didnt make much sense to me, im very much a newbie and this is the first thing ive done othr than making an LED flash and other very simple bits.

NewPing has fairly simple examples.  But, maybe in my side-quest to steer people to an event-driven programming paradigm I've confused some.  Below is an example NewPing sketch using a more common to Arduino, line-by-line with delays which works well for a "hello world" sample:

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     13  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

void loop() {
  delay(50);                // Wait 50 milliseconds between pings (ping about 20 times per second).
  int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
  Serial.print("Ping: ");
  Serial.print(cm);         // Print the result (0 = outside the set distance range, no ping echo)

As you're new to the Arduino I believe the above sketch would make more sense.  Let me know if it's not clear.  I would suggest, however, that you consider an event-driven paradigm instead of the all too common line-by-line with delays.  Line-by-line with delays may be easy to understand and works fine for a example.  But, it doesn't work well when you try to build something complex.  Changing your mindset now to an event-driven paradigm will make it much easier down the road when you want to incorporate motors, servos and sensors in your project and have your sketch do complex tasks.

384  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 21, 2012, 08:19:42 am
yeah, it works when change PING_INTERVAL from 35 to 29.

And now one sonar in the front of bot is not assembled well. so I have only one sonar in the front and one sonar in the back.
I test two sonars on opposite sites of my bot,  with the PING_INTERVAL of 8 ms, and it work.
So i'll modify my code later to shorter the time.

and i have a question that where 35ms , 29ms,  8ms come from ,how it calulate.  The max distance is 2m, so the time of echo is  (2/340) * 2 = 12ms

The time calculations have nothing to do with what you set as the maximum ping distance.  Even if you set a max ping distance to 25cm, the sensor doesn't know that or do something differently.  All that happens is the library will only care about ping echo's within the 25cm range.  What still happens is the sensor sends out a ping and listens for an echo.  That ping echo can be detected from up to 400cm to 500cm away.

So, when one sensor sends out a ping, it can travel up to lets say a maximum of 500cm bounce, and come back and hit a different sensor that you may be reading for a ping echo at the time.  This would be cross-sensor echo.  When calculating the time, you always look at the sensor's maximum detection distance (lets say 500cm as that's what most spec).  500cm * 2 * 28.5uS/sec = 28,500 uS.  There's also around 450uS of delay before a ping starts with the SRF05, which works out to right around 29ms.  In theory, if a sensor is really only able to detect a ping at a maximum distance of 500cm, you could have two sensors right next to each other and ping at 29ms intervals and you should never get any misreadings from cross-sensor echo.

I typically say to ping every 33 to 35ms as a bit of a buffer, just to be sure.  Also, 33ms works out to 30 pings/second which is typically well faster than anyone needs for a single sensor.  Finally, thinking of the sketch being event-driven and doing other things, we'd want to give the ATmega other time to do other things in the sketch other than just ping the sensors.

Your situation is a bit different as you have so many sensors.  A complete sweep at 29ms would take 464ms, so you can only do about 2 sensor sweep cycles per second.  That may be fast enough, and allow time for your sketch to do the other things it needs to do.  Then again, maybe you need it to sweep faster.  The 8ms came from testing of a simple 2 sensor system (pointed in opposite directions) in a small room environment.  At 8ms, it still gave accurate readings without cross-sensor echo.  At 7ms, I started getting random results.  This test by no means is the final decision that 8ms between pings is safe.  It was just what I saw in my test.

For your robot, I would change the ping cycle to trigger the sensors on opposite sides of the robot and rotate around (like drawing a star).  That way, you could limit the possibility of cross-sensor echo and thereby lower the ping rate.  29ms should work really well and I doubt that you could get 8ms to work as reliably as 29ms.  But, maybe somewhere between 29 and 8 will work perfectly for your situation.  It also depends on how fast you need to cycle through the sensors.  If 2 sensor sweeps per second is fast enough, leave it at 29ms.  This gives your sketch more time to do other things than if you tried to make it 8ms.

385  Using Arduino / Sensors / Re: Which Maxsonar rangefinder do you suggest? on: June 21, 2012, 07:52:17 am
excuse me, I just found the Beam width explanation file.. smiley-razz
seems like the ez4 is the one with the narrowest beam width so I'll go for that one smiley

The EZ4 also has the shortest range, keep that in mind if range is important.  I would suggest the EZ3, as it has about the same width beam but the range is much longer.

I have two problems with the Maxsonar sensors.  First, they can't detect objects closer than 6 inches, which in many cases is important.  Secondly, while it has a robust set of interface options (analog voltage, serial & pulse width), it's not compatible with most ultrasonic libraries and sketches.  This limits you to using their interface code/library or developing your own.

I've never used a Maxsonar sensor so I don't know if there's other issues (like ping speed) which could be important when used as a scanner.

386  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 20, 2012, 09:23:41 pm
Did a bunch of testing and the SRF06 will not work as-is using one pin.  However, the SR04 and SRF05 do work with the trigger and echo pins tied to a single Arduino pin.  It appears to work just as well as when using two Arduino pins.  Keep in mind, this is with my slightly modified library, not the release version.

Then I got to thinking; There's probably just a simple component like a diode on the SR04 and SRF05 which makes a one pin connection work.  I quickly figured there wasn't going to be an easy method using diodes alone.  Then I thought maybe a capacitor would isolate the sensor pins.  Sure enough, the SRF06 also works great using only one Arduino pin with just about any capacitor tied to the trigger and echo pins.

So, for the SR04 and SRF05 you can tie both the trigger and echo pins to one Arduino pin using no additional components.  And for the SRF06, tie the trigger and echo pins on the sensor together with a capacitor then tie the trigger pin on the sensor to the Arduino pin.  You don't use a capacitor for the SR04 or SRF05, just for the SRF06.

With duxiaoshi's 15 sensor project, this will save him 15 pins!  He could probably use a Teensy 2.0 "brain" now for that giant robot instead of a Arduino Mega.

I'd like to release v1.4 of NewPing with this one pin modification.  But before doing so, I need someone with a Ping))) sensor that can test it.  I want to test it with a Ping))) sensor first because I believe this modification will also make NewPing compatible with the Ping))) sensor.  If anyone has one and is willing to test, let me know.

387  Using Arduino / Sensors / Re: How do I check if ive got a faulty HC-SRO4? on: June 20, 2012, 04:13:40 pm

 You need to try this new library, it is very nice!,106043.0.html

Very good suggestion!  ;-)

Also, I would try using an echo pin other than 13.  Because the Arduino has an LED on pin 13, that can cause problems when using pin 13 as an input.  Typically, this is not an issue with ultrasonic sensors.  But, I've started to see odd things with just some SR04 sensors and I'm starting to guess the problem has to do with using ping 13 for input, not the sensor at all.

Worth a shot, but I'd still use the NewPing library anyway.  If for no other reason the one pin mode that's being added in the next release.

388  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 20, 2012, 04:04:16 pm
I was thinking about the Ping))) sensor and how simple it would be to implement in the NewPing library.  Then, I figured I'd try connecting a SR04 and SRF05 with the trigger and echo pins tied to just one Arduino pin, just like with the Ping))) sensor.  Guess what, with an adjusted library, it works perfectly.  Even works for crazy-fast ping rates of 8ms.

I figured that if the sensor's trigger pin got the output of the echo pin it would cause another trigger or basically screw things up.  Not the case.  Seems that either the echo output doesn't trigger an echo or the sensor turns off sensing of the trigger pin till it gets an echo or exceeds some time length.  Either way, it works, and works as far as I can tell, perfectly.

From my testing, you only need to use one Arduino pin to control an ultrasonic sensor.  At least the SR04, SRF05 and obviously the Ping))) which was designed to use one control pin.

389  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 20, 2012, 02:10:55 pm
thanks, teckel smiley
your code works well, no strange or negative value return. 

This is my robot.

two sensor in the front,  one in the back, two in the left and two in the right and two in each corner.
So it's 15 in all.

Now i'm working on how to avoid obstacle by these 15 sensors. I think the hard thing is how to combine these sensors data, and make decision to let bot run right way and avoid obstacle .

let the robot run autonomous and intelligent navigation is my final goal.( I have 9dof IMU sensor in bot and webcam in front to get the odometry data)

Glad to hear it works.  With that many sensors, it's a prefect situation to see how low you could set PING_INTERVAL before you started getting cross-sensor echo.  It should work if you change from 35ms to 29ms (which as a benefit also speeds up the total cycle time by 100ms).  But, if you're creative with the sensor order, you very well could make this much shorter.  For example, if the sensor ping order is on opposite sites of your robot where you're less likely to get an echo, you'll be able to set PING_INTERVAL lower without cross-sensor echo.  With two sensors in opposite directions, I was able to get accurate results with a PING_INTERVAL of only 8ms!  The advantage is that you could sample more frequently (about 8/second with 8ms interval instead of about 2/second with 35ms).  The disadvantage would be less time to do other things, like process 9dof data.  If the pings are in order around your robot, you probably cant get away with this as the adjacent sensor could get an echo from the previous sensor.

I've added the 15 sensors example sketch to the NewPing Wiki section for others that want to use a bunch of sensors and still have processing cycles to do other things.

390  Using Arduino / Sensors / Re: NewPing (Ultrasonic) Library for HC-SR04, SRF05 and SRF06 sensors - v1.3 on: June 19, 2012, 04:05:54 pm
great job... i use the library in my project and test my robot.
i have 15 SRF05 sonar sensors on the bot. I set pingSpeed 200ms, and pingInterval 35ms, but sometimes i get big value or negative value.  

The ping12 result like this :

Ping12: 3328cm
Ping12: 12288cm
Ping12: -24544cm

my code is not simple and clean, and i'd like to know how to use NewPing to slove 15 sonar sensors problem

15 sensors!  Now THAT'S what I'm talking about as a perfect use for the NewPing library!

First, is this only happening for sensor 12?  If so, that could isolate the problem to something wrong with that sensor.

In any case, I believe the problem is that you're setting up the pings to happen 35ms apart, but then starting the loop process again in only 100ms.  This works for 2 sensors, as it only takes 35*2=70ms per cycle through all the sensors.  But, with 15, it would take at least 525ms to cycle through all 15 sensors (35*15=525).  Also, as AWOL stated, making an array of sonar objects makes the code a LOT shorter.  I also noticed you're not using the "time" variable and the "cm" array should be set outside loop().  Below is a streamlined version of your sketch.  I also converted it to use the timer event so it's more event driven:

#include <NewPing.h>

#define SONAR_NUM     15
#define MAX_DISTANCE 200
#define PING_INTERVAL 35 // Milliseconds between each sensor ping (35ms is about the minimum to avoid cross-sensor echos.  You could try making this as low as 29ms and see what happens).

const int pingCycle = PING_INTERVAL * (SONAR_NUM + 1); // This used to be PING_SPEED, pings as fast as possible.
unsigned long pingTimer[SONAR_NUM + 1]; // +1 for timer that displays results.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0;  // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {
  NewPing(41, 42, MAX_DISTANCE),
  NewPing(43, 44, MAX_DISTANCE),
  NewPing(45, 20, MAX_DISTANCE),
  NewPing(21, 22, MAX_DISTANCE),
  NewPing(23, 24, MAX_DISTANCE),
  NewPing(25, 26, MAX_DISTANCE),
  NewPing(27, 28, MAX_DISTANCE),
  NewPing(29, 30, MAX_DISTANCE),
  NewPing(31, 32, MAX_DISTANCE),
  NewPing(34, 33, MAX_DISTANCE), //10
  NewPing(35, 36, MAX_DISTANCE), //11
  NewPing(37, 38, MAX_DISTANCE),
  NewPing(39, 40, MAX_DISTANCE),
  NewPing(50, 51, MAX_DISTANCE),
  NewPing(52, 53, MAX_DISTANCE)

void setup() {
  pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    pingTimer[i+1] = pingTimer[i] + PING_INTERVAL;

void loop() {
  for (uint8_t i = 0; i <= SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += pingCycle; // Set next time this sensor will be pinged.
      if (i == SONAR_NUM) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      else {
        sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping.
        currentSensor = i; // Sensor being accessed.
        cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
        sonar[currentSensor].ping_timer(echoCheck); // Do the ping and wait for the echo interrupt.
  // The rest of your code would go here.

void echoCheck() {
  if (sonar[currentSensor].check_timer()) { // Check to see if the ping was received.
    cm[currentSensor] = sonar[currentSensor].convert_cm(sonar[currentSensor].ping_result); // Set the sensor distance to the array.

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print("cm ");

I don't have 15 sensors nor an Arduino Mega to totally test this sketch.  But, I did test it with 3 sensors and it worked.  Let me know how it works with your project.  Also, I'd love to see a picture of whatever you have going with 15 sensors.

Pages: 1 ... 24 25 [26] 27 28 ... 31