Skip to content

Commit e9b8e35

Browse files
authored
Merge pull request #3 from xp-dv/example3-patch
Example 3 Patch
2 parents 96f7fa8 + bf9e202 commit e9b8e35

File tree

1 file changed

+28
-47
lines changed

1 file changed

+28
-47
lines changed
Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,61 @@
1-
/* SparkFun Ulrasonic Distance Sensor - Example 3 - Distance using Trigger and Echo Pins
1+
/* SparkFun Ultrasonic Distance Sensor - Example 3 - Distance Using Trigger and Echo Pins
22
*
33
* Product:
4-
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
5-
* * https://www.sparkfun.com/1XXXX
4+
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-24805)
5+
* * https://www.sparkfun.com/products/24805
66
*
7-
* Written By: Elias Santistevan
8-
* Date: 06/2024
7+
* Written By: Elias Santistevan and Xavior Pautin
8+
* Date: 09/2024
99
*
1010
* SPDX-License-Identifier: MIT
1111
*
1212
* Copyright (c) 2024 SparkFun Electronics
1313
*/
14-
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
15-
16-
// Create an ultrasonic sensor object
17-
QwiicUltrasonic myUltrasonic;
18-
19-
// Here we set the device address. Note that an older version of the Qwiic
20-
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
21-
// you'll need to change the address below. It is also recommended to run
22-
// Example 2 to change the address to the new default.
23-
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
24-
// uint8_t deviceAddress = 0x00;
25-
26-
// Adjust these to your setup.
14+
// Using the echo and trigger breakout pins does not require a QWIIC connector I2C connection
15+
// Adjust these pins to your setup
2716
const int triggerPin = 7; // Trigger Pin of Ultrasonic Sensor
2817
const int echoPin = 8; // Echo Pin of Ultrasonic Sensor
2918

3019
// Used for distance calculation
3120
float distance = 0.0;
3221
float duration = 0.0;
33-
const float speedOfSound = 340.00; // Speed of sound in m/s
34-
const float millisPerSecond= 1000.00; // Number of milliseconds in a second
35-
36-
void setup() {
37-
38-
Wire.begin();
22+
const float speedOfSound = 0.343; // Speed of sound in mm/us
3923

40-
Serial.begin(115200);
41-
Serial.println("Ultrasonic Distance Sensor - Example 2 Distance using Trigger and Echo Pins.");
24+
void setup() {
25+
Serial.begin(115200);
26+
Serial.println("Ultrasonic Distance Sensor - Example 3 Distance using Trigger and Echo Pins");
4227

4328
pinMode(triggerPin, OUTPUT);
4429
pinMode(echoPin, INPUT);
45-
46-
// Attempt to begin the sensor
47-
while (myUltrasonic.begin(deviceAddress) == false)
48-
{
49-
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
50-
delay(2000);
51-
}
52-
53-
Serial.println("Ultrasonic sensor connected!");
5430
}
5531

5632
void loop() {
57-
33+
// Required pulse to trigger the ultrasonic sensor
34+
// delayMicroseconds() must be greater than 10us, but less than 200us
35+
// 10 microseconds is the minimum pulse width listed by the manufacturer
36+
// 200 microseconds is the approximate delay between the rise of the trigger pulse
37+
// and fall of the echo pulse
5838
digitalWrite(triggerPin, HIGH);
59-
delay(5);
39+
delayMicroseconds(50);
6040
digitalWrite(triggerPin, LOW);
61-
// We don't want continually trigger while data is being retrieved from the sensor.
6241

63-
duration = pulseIn(echoPin, HIGH);
64-
// Time until sound detected * speed of sound * conversion to mm
42+
// pulseIn() set LOW because the echo pin of the sensor outputs a negative pulse from HIGH to LOW
43+
// pulseIn() duration is in microseconds
44+
duration = pulseIn(echoPin, LOW);
45+
6546
// Divide by two because we only want the time the wave traveled to the object,
6647
// not to the object and back.
67-
distance = (duration * speedOfSound * millisPerSecond) / 2;
48+
distance = (duration * speedOfSound) / 2;
6849

6950
// Print measurement
7051
Serial.print("Distance (mm): ");
71-
Serial.println(distance);
52+
Serial.println(distance, 1); // Print to 1 decimal place
7253

73-
//Serial.println("Distance (cm): ");
74-
//Serial.print((distance / 10.0), 2);
54+
// Serial.print("Distance (cm): ");
55+
// Serial.println((distance / 10.0), 2); // Print to 2 decimal places
7556

76-
//Serial.println("Distace (in): ");
77-
//Serial.print((distance / 25.4), 2);
57+
// Serial.print("Distace (in): ");
58+
// Serial.println((distance / 25.4), 2); // Print to 2 decimal places
7859

79-
delay(500);
60+
delay(500); // Must be no less than 25ms to ensure full 400cm sensor range
8061
}

0 commit comments

Comments
 (0)