0% found this document useful (0 votes)
68 views2 pages

Arduino Ultrasonic HCSR40

This Arduino sketch uses an HC-SR04 ultrasonic distance sensor to measure distances between 0-200 cm. It triggers the sensor, measures the echo pulse duration to calculate distance, and prints the distance to the serial monitor or a negative number if out of range. It originated online and has been modified by multiple sources for readability and functionality.

Uploaded by

Miftahul Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Arduino Ultrasonic HCSR40

This Arduino sketch uses an HC-SR04 ultrasonic distance sensor to measure distances between 0-200 cm. It triggers the sensor, measures the echo pulse duration to calculate distance, and prints the distance to the serial monitor or a negative number if out of range. It originated online and has been modified by multiple sources for readability and functionality.

Uploaded by

Miftahul Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1 /*

2 HC-SR04 Ping distance sensor:


3 VCC to arduino 5v
4 GND to arduino GND
5 Echo to Arduino pin 7
6 Trig to Arduino pin 8
7
8 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
9 Has been modified by Winkle ink here:
10 http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
11 And modified further by ScottC here:
12 http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic13 sensor.html
14 on 10 Nov 2012.
15 */
16
17
18 #define echoPin 7 // Echo Pin
19 #define trigPin 8 // Trigger Pin
20 #define LEDPin 13 // Onboard LED
21
22 int maximumRange = 200; // Maximum range needed
23 int minimumRange = 0; // Minimum range needed
24 long duration, distance; // Duration used to calculate distance
25
26 void setup() {
27 Serial.begin (9600);
28 pinMode(trigPin, OUTPUT);
29 pinMode(echoPin, INPUT);
30 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
31 }
32
33 void loop() {
34 /* The following trigPin/echoPin cycle is used to determine the
35 distance of the nearest object by bouncing soundwaves off of it. */
36 digitalWrite(trigPin, LOW);
37 delayMicroseconds(2);
38
39 digitalWrite(trigPin, HIGH);
40 delayMicroseconds(10);
41
42 digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

//Calculate the distance (in cm) based on the speed of sound.


distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}

You might also like