All About Max Sonar EZ0 and Arduino
by Mohannad Rawashdeh in Circuits > Arduino
36354 Views, 41 Favorites, 0 Comments
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
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);
}
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);
}
Downloads
Serial Interface
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);
}