Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Software & Tools
Arduino Web Editor
Project description
Code
Ultrasonic Ranger
arduino
It tells you the ping using the Serial Monitor.
1#define trigPin 12 2#define echoPin 11 3#define MAX_DISTANCE 200 4// define the timeOut according the maximum range. time out= 2*MAX_DISTANCE /100 /340 *1000000 = MAX_DISTANCE*58.8 5float timeOut = MAX_DISTANCE * 60; 6int soundVelocity = 340; 7void setup() { 8 pinMode(trigPin, OUTPUT); 9 pinMode(echoPin, INPUT); 10 Serial.begin(9600); 11} 12void loop() { 13 delay(100); 14 Serial.print("Ping: "); 15 Serial.print(getSonar()); 16 Serial.println("cm"); 17} 18float getSonar() { 19 unsigned long pingTime; 20 float distance; 21 digitalWrite(trigPin, HIGH); 22 delayMicroseconds(10); 23 digitalWrite(trigPin, LOW); 24 pingTime = pulseIn(echoPin, HIGH, timeOut); 25 distance = (float)pingTime * soundVelocity / 2 / 10000; 26 return distance; 27} 28 29
Downloadable files
Ranger
Ping
Ranger
Ranger
Ping
Ranger
Documentation
Ranger
Ping
Ranger
Ranger
Ping
Ranger
Comments
Only logged in users can leave comments