Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ultrasonic Sensor TYWu.

Similar presentations


Presentation on theme: "Ultrasonic Sensor TYWu."— Presentation transcript:

1 Ultrasonic Sensor TYWu

2 HC-SR04 Picture

3 HC-SR04 Specifications power supply :5V DC quiescent current : <2mA
effectual angle: <15° ranging distance : 2cm – 500 cm resolution : 0.3 cm 40KHz ultrasonic signal

4 HC-SR04 There are 4 pins Control
VCC , Trig, Echo, GND Control pull the Trig pin to high level  for more than 10us impulse , the module start ranging finish ranging, If you find an object in front, Echo pin will be high level, and based on the different distance, it will take the different duration of high level. So we can calculated the distance easily : Distance = High Level Duration*(Sonic:340m/s)/2

5 HC-SR04 Control

6 HC-SR04 Effectual Angle: <15°

7 HC-SR04 Library Ultrasonic.cpp Ultrasonic.cpp and Ultrasonic.h
#include "WProgram.h" #include "Ultrasonic.h" Ultrasonic::Ultrasonic(int TP, int EP) { pinMode(TP,OUTPUT); pinMode(EP,INPUT); Trig_pin=TP; Echo_pin=EP; }

8 HC-SR04 long Ultrasonic::Timing() { digitalWrite(Trig_pin, LOW);
delayMicroseconds(2); digitalWrite(Trig_pin, HIGH); delayMicroseconds(10); duration = pulseIn(Echo_pin,HIGH); // in microseconds return duration; }

9 HC-SR04 long Ultrasonic::Ranging(int sys) { Timing();
distacne_cm = duration / 29 / 2 ; // 34000cm/s*us=0.034=1/29 distance_inc = duration / 74 / 2; if (sys) return distacne_cm; else return distance_inc; }

10 HC-SR04 Ultrasonic.h #ifndef Ultrasonic_h #define Ultrasonic_h
#include "WProgram.h" #define CM 1 #define INC 0

11 HC-SR04 class Ultrasonic { public: Ultrasonic(int TP, int EP);
long Timing(); long Ranging(int sys); private: int Trig_pin; int Echo_pin; long duration,distacne_cm,distance_inc; }; #endif

12 HC-SR04 Test Example (Sketch) #include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13); // TP: pin 12, EP: pin 13 void setup() { Serial.begin(9600); } void loop() Serial.print(ultrasonic.Ranging(CM)); Serial.println(" cm");

13 LV-MaxSonar®-EZ1 Pictures

14 LV-MaxSonar®-EZ1 Overview
detects objects from 0-inches to 254-inches (6.45-meters) 1-inch resolution The interface output formats included are pulse width output, analog voltage output, and serial digital output.

15 LV-MaxSonar®-EZ1 AN – Outputs analog voltage with a scaling factor of (Vcc/512) per inch. A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in. The output is buffered and corresponds to the most recent range data.

16 LV-MaxSonar®-EZ1 Test Example (Sketch)
int ultraAn = 0; // select the input pin for the ultrasonic sensor int val = 0; // variable to store the value coming from the sensor int dist = 0; // variable to store the converted distance in inches // VDD Connect to 3.3V void setup() { Serial.begin(9600); // setup the serial port to send the values // back to the computer }

17 LV-MaxSonar®-EZ1 void loop() {
val = analogRead(ultraAn); // read the value from the sensor Serial.println(val); // print the value to the serial port dist = (val*2.1); // converts the analog value to distance in inches Serial.print(dist); // prints the distance in cm Serial.println(" cm"); // prints the distance in cm delay (500); // delays 0.5 seccond in between readings }


Download ppt "Ultrasonic Sensor TYWu."

Similar presentations


Ads by Google