Introduction: All About Max Sonar EZ0 and Arduino

I Searched In the Internet about EZ0 Ultrasonic And I found Many information in the datasheet for this sensor , I will show you how we can program this sensor in Arduino IDE .

At first , the Datasheet " You can see it Here " Have all Specification and Features For this sensor .

The EZ0 Sonar detects objects from 0-inches to 254-inches (6.45-meters) and provides sonar range information from 6-inches out to 254-inches with 1-inch resolution. Objects from 0-inches to 6-inches typically range as 6- inches

This sensor Has 3 Output formats To interface with :

1 ) Analog interface .

2) Pulse width Modulation "PW".

3) Serial Interface .


Material :

1) Arduino Board " I used Arduino Nano".

2) Ultrasonic Max Sonar EZ0 .

3)Wires and BreadBoard .


Download the library for this sensor from HERE

Step 1: Analog Interface

The Analog Interface is the easiest way to communicate with this sensor , there is a directly proportional between Distance and Output voltage .

Outputs analog voltage with a scaling factor of (Vcc/512) per inch .

For example , a voltage supply of 5V yields ~9.8mV/inch , a voltage supply of 3.3 V yields ~6.4mV/inch.

The analog Output voltage not accurate rather than PW And serial . so I prefer to use PW or serial .

this is a simple code :

// Written by : Mohannad Rawashdeh
// this code foe EZ0 Max sonar , analog interface
// https://www.instructables.com/member/Mohannad+Rawashdeh/
//.................................................
float Inch=0.00;
float cm=0.00;
int SonarPin=A0;
int sensorValue;
void setup(){
pinMode(SonarPin,INPUT);
Serial.begin(9600);
}
void loop(){
sensorValue=analogRead(SonarPin);
delay(50);
Inch= (sensorValue*0.497);
cm=Inch*2.54;
Serial.println(sensorValue);
Serial.print(Inch);
Serial.println("inch");
Serial.print(cm);
Serial.println("cm");
delay(100);
}

Step 2: Pulse Width "PW" Interface

Pulse width "PW" Is another way to communicate with this sensor .

This pin outputs a pulse width representation of range. The distance can be calculated using the scale factor of 147uS per inch.

So using Pulse in to calculate the distance .

I prepared This Library for this method , You can Download from attachment .

This code without Library and with Median/Mode filter :
//...........................................................
// this code Based on : http://playground.arduino.cc/Main/MaxSonar
int pw_pin=7;
int arraysize = 9;
int array[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
long inch;
int exact_cm_value;
void setup() {
// put your setup code here, to run once:
pinMode(pw_pin, INPUT);
Serial.begin(9600);
}
void sensorRead(){
for(int i = 0; i < arraysize; i++)
{
inch = pulseIn(pw_pin, HIGH);
array[i] = inch/58;
delay(10);
}
}
void array_arrangment(int *a,int n){
// Author: Bill Gentles, Nov. 12, 2010)
for (int i = 1; i < n; ++i)
{
int j = a[i];
int k;
for (k = i - 1; (k >= 0) && (j < a[k]); k--)
{
a[k + 1] = a[k];
}
a[k + 1] = j;
}
}

int filter(int *a,int n){
int i = 0;
int count = 0;
int maxCount = 0;
int filter = 0;
int median;
int prevCount = 0;
while(i<(n-1)){
prevCount=count;
count=0;
while(a[i]==a[i+1]){
count++;
i++;
}
if(count>prevCount && count>maxCount){
filter=a[i];
maxCount=count;
median=0;
}
if(count==0){
i++;
}
if(count==maxCount){//If the dataset has 2 or more modes.
median=1;
}
if(filter==0||median==1){//Return the median if there is no mode.
filter=a[(n/2)];
}

return filter;

}
}
void loop() {
// put your main code here, to run repeatedly:
sensorRead();
array_arrangment(array,arraysize);
exact_cm_value= filter(array,arraysize);
Serial.print("The distance = ");
Serial.print(exact_cm_value);
Serial.println(" cm ");
delay(100);

}

Library code :
//......................................................
#include "SonarEZ0pw.h"
SonarEZ0pw Sonar(7); // pin D7
float cm_dis=0.00;
float Inch_dis=0.00;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
cm_dis= Sonar.Distance(cm); // To calculate the distance in cm
Inch_dis=Sonar.Distance(inch);// To calculate the distance in Inch
Serial.println("Distance " );
Serial.print(Inch_dis);
Serial.println(" inch ");
Serial.print(cm_dis);
Serial.println(" cm ");
delay(250);
}

Step 3: Serial Interface

In this method i will use SoftwareSerial library to write the code for this sensor , 

And we will Connect D4 With BW , Leave it  open or hold low for serial output on the TX output .

The output is an ASCII capital “R” which equal 0x52 , followed by three ASCII character digits representing the range in inches up to a maximum of  255, followed by a carriage return (ASCII 13 or 0x0D)

the image show Logic  Analyzer and the Output on it 

code :

// written By : Mohannad Rawashdeh
// http://WWW.Genotronex.com
// genotronex@gmail.com
// https://www.instructables.com/member/Mohannad+Rawashdeh/
// this code to test EZ0 Sonar sensor using Serial method .
// Connect Tx....D8
// Connect Rx ....D9
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9,true); // RX, TX
int BW=4;
char *buffer;
byte x;
char array[3];
int counter=0;
void setup() {
  // put your setup code here, to run once:
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(BW,OUTPUT);
  digitalWrite(BW,LOW);
  delay(250);
  Serial.println("Calibrartion Cycle ");
  delay(150);
}

void reading(){
 
  mySerial.println(1);
while (mySerial.available())
{
   x= mySerial.readBytes(buffer,1);
   if(*buffer==0x52){ 
   x= mySerial.readBytes(buffer,1);
   array[0]=*buffer; 
   x= mySerial.readBytes(buffer,1);
   array[1]=*buffer; 
   x= mySerial.readBytes(buffer,1);
   array[2]=*buffer;
   }
}

delayMicroseconds(220);
}
void loop() {
  // put your main code here, to run repeatedly:
  reading();
  int Final_inch=(array[0]-48)*100 + (array[1]-48)*10 +(array[2]-48) ;
  float Final_cm=Final_inch*2.54;
  Serial.print(Final_inch);
  Serial.println(" Inch ");
  Serial.print(Final_cm);
  Serial.println(" cm ");
  delay(200);
}

Comments

author
NatS8 made it!(author)2016-08-12

can i use to for motion detection please? to count people passing through the door?

author
BengF made it!(author)2016-03-16

Hi, i am using the analog way to calculate the distance, however i would like to know why did u multiply the sensorvalue by 0.497 to get inches, cause i read that 5/512 is used as a scaling factor to calculate in inches?

author
DkD4 made it!(author)2016-01-22

I tried this sensor. it works fine , but i tried fixing it into 4" dia pvc pipe it doesn't work as it has to.. how to fix this sensor inside the pipe? help me out...

email me at : look4ursoul@live.com

will be very grateful.

author
Jos%C3%A9G100 made it!(author)2015-12-15

What is the beamwidth of this sensor?

author
barbarossa789 made it!(author)2015-04-14

can i send the output data through a bluetooth shield?

author
ben.landry.923 made it!(author)2014-09-19

Nice code, but worst datasheet EVER!

author
aznorulazman made it!(author)2014-05-14

Hi. I have tried these coding in my project to test the sensors. and it works! thanks. However, I am using 7 LV Maxsonar Ultrasonic Sensors for my blind detection system to detect obstacles. Somehow there is errors in my coding. Would you help me?

author
manicmonday made it!(author)2013-11-14

Can you use this to alarm when something is moving toward you very quickly? IE to mount on the front of a vehicle to help keep you from running into someone that is stopped. Maybe later to activate a break.

author
Mohannad+Rawashdeh made it!(author)2013-11-20

Yes you can

author
Mohannad+Rawashdeh made it!(author)2013-09-19

please explain more .

Reading a sensor value , store it in a variable and the convert it to string and send it .

author
britich made it!(author)2013-09-18

how can i use it with xbee?

About This Instructable

23,291views

35favorites

Bio: Maker, PCB designer , electronics instructor from Jordan just one word ! I Adore electronics follow me on FB https://www.facebook.com/Mohannad-Rawashdeh-Raw-774983565988641/
More by Mohannad Rawashdeh:Convert Your Pringles Cans to RTTTL Audio PlayerTTP224 Digital Touch Sensor Applications Using ArduinoRF 315MHz/433MHz Wireless and Arduino...again!
Add instructable to: