You are on page 1of 4

LV-MaxSonar®-EZ1™

Sonar Range Finder


MaxSonar's top features is low cost, high quality ultrasonic distance sensors, no sensor dead zone,
calibrated beam patterns, stable range readings and low power demands
Measuring distance: 0 to 645 cm
Analog Voltage, Serial, Pulse Width

/Original author: Bruce Allen


//Date: 23/07/09
//Changed by Marte and Runar 09/10/14, to only have cm as output<br>//Digital
pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout
execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;
int output;
<br>void setup() {<br> //This opens up a serial connection to shoot the
results back to the PC console
Serial.begin(9600);<br>}
<br>void loop() {<br>pinMode(pwPin, INPUT);<br>//Used to read in the pulse
that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per
Inch.<br>pulse = pulseIn(pwPin, HIGH);
//147uS per inch
inches = pulse/147;
//change inches to centimetres
cm = inches * 2.54;<br>// Serial.print(inches);
// Serial.print("in, ");
// Serial.print(cm);
// Serial.print("cm");
// Serial.println();
output= map(cm,0,645);<br>Serial.println(output);<br>delay(1);
}

Example code showing how to make decisions on


distance:
/* YourDuino SKETCH UltraSonic Serial 2.0
*** Modified to find distance < 3 inches (7cm)
*** Frances Mae Gerpacio
Runs HC-04 and SRF-06 and other Ultrasonic Modules
Open Serial Monitor to see results
Reference: http://playground.arduino.cc/Code/NewPing
Questions? terry@yourduino.com */

/*-----( Import needed libraries )-----*/


#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define TRIGGER_PIN 11
#define ECHO_PIN 10
#define MAX_DISTANCE 14 // Maximum distance we want to ping for (in
centimeters).
//Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins
and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup() /****** SETUP: RUNS ONCE ******/


{
Serial.begin(9600);
Serial.println("UltraSonic Distance Measurement");
Serial.println("YourDuino.com terry@yourduino.com");
}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/


{
delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be
the shortest delay between pings.
DistanceIn = sonar.ping_in();
Serial.print("Ping: ");
Serial.print(DistanceIn); // Convert ping time to distance and print result
// (0 = outside set distance range, no ping echo)
Serial.print(" in ");

delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be


the shortest delay between pings.
DistanceCm = sonar.ping_cm();
Serial.print("Ping: ");
Serial.print(DistanceCm);
Serial.println(" cm");
if ( (DistanceCm <= 7) && (DistanceCm != 0) )
{
Serial.println("3 Inches or closer! ");
}

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

// None
//*********( THE END )***********
3rd Code

/*
This code should work to get warning cross the buzzer when something be closer
than 0.5 meter
Circuit is ultrasonic sensor and buzzer +5v and Arduino uno is used
a_atef45@yahoo.com
www.zerosnones.net
+201153300223
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to
the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over
range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}
This code should work to get warning cross the buzzer when something be closer
than 0.5 meter
Circuit is ultrasonic sensor and buzzer +5v and Arduino uno is used
a_atef45@yahoo.com
www.zerosnones.net
+201153300223
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;

void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse
width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}

void loop()
{
// Duration will be the input pulse width and distance will be the
distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means
over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}

You might also like