|
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 |
2 | 2 | * |
3 | 3 | * 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 |
6 | 6 | * |
7 | | - * Written By: Elias Santistevan |
8 | | - * Date: 06/2024 |
| 7 | + * Written By: Elias Santistevan and Xavior Pautin |
| 8 | + * Date: 09/2024 |
9 | 9 | * |
10 | 10 | * SPDX-License-Identifier: MIT |
11 | 11 | * |
12 | 12 | * Copyright (c) 2024 SparkFun Electronics |
13 | 13 | */ |
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 |
27 | 16 | const int triggerPin = 7; // Trigger Pin of Ultrasonic Sensor |
28 | 17 | const int echoPin = 8; // Echo Pin of Ultrasonic Sensor |
29 | 18 |
|
30 | 19 | // Used for distance calculation |
31 | 20 | float distance = 0.0; |
32 | 21 | 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 |
39 | 23 |
|
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"); |
42 | 27 |
|
43 | 28 | pinMode(triggerPin, OUTPUT); |
44 | 29 | 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!"); |
54 | 30 | } |
55 | 31 |
|
56 | 32 | 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 |
58 | 38 | digitalWrite(triggerPin, HIGH); |
59 | | - delay(5); |
| 39 | + delayMicroseconds(50); |
60 | 40 | digitalWrite(triggerPin, LOW); |
61 | | - // We don't want continually trigger while data is being retrieved from the sensor. |
62 | 41 |
|
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 | + |
65 | 46 | // Divide by two because we only want the time the wave traveled to the object, |
66 | 47 | // not to the object and back. |
67 | | - distance = (duration * speedOfSound * millisPerSecond) / 2; |
| 48 | + distance = (duration * speedOfSound) / 2; |
68 | 49 |
|
69 | 50 | // Print measurement |
70 | 51 | Serial.print("Distance (mm): "); |
71 | | - Serial.println(distance); |
| 52 | + Serial.println(distance, 1); // Print to 1 decimal place |
72 | 53 |
|
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 |
75 | 56 |
|
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 |
78 | 59 |
|
79 | | - delay(500); |
| 60 | + delay(500); // Must be no less than 25ms to ensure full 400cm sensor range |
80 | 61 | } |
0 commit comments