Ultrasonic sensor comparison: HC-SR04, PING, Maxbotix

For many projects You will need to measure distance, let’s say You are building a robot and You don’t want to have it bouncing on the walls, aren’t You? One of the first ideas to get some orientation in surroundings is to use ultrasonic range sensor. We took three different sensors and do some basic checks to do ultrasonic sensor comparison.  Which one could be best for Your project?

Ultrasonic sensor comparison
Ultrasonic sensor comparison

We did tested: Seeedstudio SEN136B5B, HC-SR04 and MaxBotix MaxSonar EZ1 (MB1010).

How ultrasonic sensor does work? They are built from sound source and receiver – both can be separate devices or single one. Maxbotix has both integrated into one case, while two other sensor have separate sound source and receiver. Sound source emits sound impulse in ultrasonic range (usually 32 kHz) and receivers listens for echo. Knowing how fast sound is in air, from time we get an echo we can calculate how far from sensor object is located.

Ultrasonic sensor comparison

For PING and HC-SR04 we used NewPing. Maxbotix has analog output (voltage shows distance) so it is easy to measure w/o any additional software. Maxbotix offers also other output formats – Serial and PWM (width of a pulse is proportional to distance).

Seeedstudio’s PING senosr data

Power voltage 5V
Supply current 15 mA
Working frequency 40k Hz
Range (max) 400 cm
Range (min) 3 cm
Resolution 1 cm
Minimal trigger pulse width 10 μs
Size 43x20x15 mm

Here is example code used in our test. As You can see the only thing You need to do is define to which pins is sensor attached. Using sonar.ping() will tell us how long sensor has to wait for response. Divide time through US_ROUNDTRIP_CM. to get distance. Library has support for simple filter (calculating average result from few measurements) but we did not use it.

#include <NewPing.h> 


#define PING_PIN  7  
// where sensor is connected

#define MAX_DISTANCE 400 
// max distance is used only in calculations by library You can not adjust range

NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); 

void setup() {
  Serial.begin(115200); 
}

void loop() {
  delay(50);                      
// Wait a moment - You have wait at least 29 ms between pings
  unsigned int uS = sonar.ping(); 
// wait for  a response
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); 
// get result in cm. 0 means - out of range
  Serial.println("cm");
}

Second tested sensor – HC-SR04 – similar to PING in terms of construction (two cones) and similar operation – measurement is starter after impulse on input, on output pin is returned impulse with width representing distance. Difference between PING and HC-SR04 is – in PING both trigger and response pin are the same, and in HC-SR04 they are separate.

Specifiaction

Power supply 5V
Supply current 15 mA
Working frequency 40k Hz
Range (max) 400 cm
Range (min) 2 cm
Working angle  15 deg
Minimal trigger pulse width 10 μs
Size 45x20x15 mm

Code for this sensor is identical only there are two separate pins defined and used in NewPing object initialization:

#include <NewPing.h> 

#define TRIGGER_PIN  12  
#define ECHO_PIN     11  
#define MAX_DISTANCE 400 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

Last one was Maxbotix MB1010 – compact sensor (single cone) with analog output (but also with Serial and PWM), using analog input is so simple as dividing reading from ADC by two. Assuming Arduino and sensor are powered from single Vcc – distance is in steps Vcc/512, ADC reading is in Vcc/1024 – just divide ADC reading by two to get result in inches. Since we are used to metric, multiply by 2.54 to get result in cm.

int sensorPin = 0; //analog pin 0

void setup(){
  Serial.begin(9600);
}

void loop(){
  float val = analogRead(sensorPin)/2.0;
  Serial.println(val*2.54);

 delay(100);

}

With this setup we made few measurements with different obstacles – concrete wall, wall from cardboxes. PING and  HC-SR04 does measure distance only after trigger, Maxbotix works continuously.

Detailed results.

Leaving details, what are outcomes? All sensors work very well with obstacle like concrete wall. Results were stable and accurate. With cardboxes showed first discrepancies. While Maxbotix and PING still got stable and accurate results, HC-SR04 did sometimes return reading which does not cohere to reality ;)

Last test we did was with empty IC container (with more or less rectangle cross-section). Maxbotix proved it is champion again, but this time – no second place. You can see details on video below, but there were many cases, when both Ping and HC-SR04 did not found any obstacle. We were surprised, that Maxbotix was able to measure distance, even when edge of container was pointing directly  to sensor.

Results of our ultrasonic sensor comparison

This was not comprehensive test, but it has proven our gut feeling, that Maxbotix provides best results as it comes to obstacle detection.

But to be honest – for each sensor there can be combination of angle, distance and other conditions which can yield in not detecting object.