From 7fcdbd29a8b8a79ffec53fd391236e391fdec32c Mon Sep 17 00:00:00 2001 From: Xavior Pautin Date: Fri, 27 Sep 2024 03:29:21 -0700 Subject: [PATCH 1/3] Example 3 Patch: removed unneeded I2C check, fixed distance calculation, fixed incorrect delay causing sensor to not read, added clarifying comments, tested for function --- .../Example3_Trigger_Echo.ino | 71 +++++++------------ 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino index c21f829..1919e10 100644 --- a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino +++ b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino @@ -1,80 +1,61 @@ /* SparkFun Ulrasonic Distance Sensor - Example 3 - Distance using Trigger and Echo Pins * * Product: - * * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX) - * * https://www.sparkfun.com/1XXXX + * * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-24805) + * * https://www.sparkfun.com/products/24805 * - * Written By: Elias Santistevan - * Date: 06/2024 + * Written By: Elias Santistevan and Xavior Pautin + * Date: 09/2024 * * SPDX-License-Identifier: MIT * * Copyright (c) 2024 SparkFun Electronics */ -#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h" - -// Create an ultrasonic sensor object -QwiicUltrasonic myUltrasonic; - -// Here we set the device address. Note that an older version of the Qwiic -// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00, -// you'll need to change the address below. It is also recommended to run -// Example 2 to change the address to the new default. -uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F -// uint8_t deviceAddress = 0x00; - -// Adjust these to your setup. +// Using the echo and trigger breakout pins does not require a QWIIC connector I2C connection +// Adjust these pins to your setup const int triggerPin = 7; // Trigger Pin of Ultrasonic Sensor const int echoPin = 8; // Echo Pin of Ultrasonic Sensor // Used for distance calculation float distance = 0.0; float duration = 0.0; -const float speedOfSound = 340.00; // Speed of sound in m/s -const float millisPerSecond= 1000.00; // Number of milliseconds in a second - -void setup() { - - Wire.begin(); +const float speedOfSound = 0.343; // Speed of sound in mm/us - Serial.begin(115200); - Serial.println("Ultrasonic Distance Sensor - Example 2 Distance using Trigger and Echo Pins."); +void setup() { + Serial.begin(115200); + Serial.println("Ultrasonic Distance Sensor - Example 3 Distance using Trigger and Echo Pins"); pinMode(triggerPin, OUTPUT); pinMode(echoPin, INPUT); - - // Attempt to begin the sensor - while (myUltrasonic.begin(deviceAddress) == false) - { - Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!"); - delay(2000); - } - - Serial.println("Ultrasonic sensor connected!"); } void loop() { - + // Required pulse to trigger the ultrasonic sensor + // delayMicroseconds() must be greater than 10us, but less than 200us + // 10 microseconds is the minimum pulse width listed by the manufacturer + // 200 microseconds is the approximate delay between the rise of the trigger pulse + // and fall of the echo pulse digitalWrite(triggerPin, HIGH); - delay(5); + delayMicroseconds(50); digitalWrite(triggerPin, LOW); - // We don't want continually trigger while data is being retrieved from the sensor. - duration = pulseIn(echoPin, HIGH); - // Time until sound detected * speed of sound * conversion to mm + // pulseIn() set LOW because the echo pin of the sensor outputs a negative pulse from HIGH to LOW + // pulseIn() duration is in microseconds + duration = pulseIn(echoPin, LOW); + // Divide by two because we only want the time the wave traveled to the object, // not to the object and back. - distance = (duration * speedOfSound * millisPerSecond) / 2; + distance = (duration * speedOfSound) / 2; // Print measurement Serial.print("Distance (mm): "); - Serial.println(distance); + Serial.println(distance, 1); // Print to 1 decimal place - //Serial.println("Distance (cm): "); - //Serial.print((distance / 10.0), 2); + // Serial.print("Distance (cm): "); + // Serial.println((distance / 10.0), 2); // Print to 2 decimal places - //Serial.println("Distace (in): "); - //Serial.print((distance / 25.4), 2); + //Serial.print("Distace (in): "); + //Serial.println((distance / 25.4), 2); // Print to 2 decimal places delay(500); } From 20a5f1d3cc030da469c44a63a219dc4ef8aefb28 Mon Sep 17 00:00:00 2001 From: Xavior Pautin Date: Sun, 29 Sep 2024 00:12:01 -0700 Subject: [PATCH 2/3] Added comment to main loop delay --- examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino index 1919e10..d75c4d6 100644 --- a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino +++ b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino @@ -54,8 +54,8 @@ void loop() { // Serial.print("Distance (cm): "); // Serial.println((distance / 10.0), 2); // Print to 2 decimal places - //Serial.print("Distace (in): "); - //Serial.println((distance / 25.4), 2); // Print to 2 decimal places + // Serial.print("Distace (in): "); + // Serial.println((distance / 25.4), 2); // Print to 2 decimal places - delay(500); + delay(500); // Must be no less than 25ms to ensure full 400cm sensor range } From bf9e2028a1e6fa7ba74cce58cfdd67198e411812 Mon Sep 17 00:00:00 2001 From: Xavior Pautin Date: Mon, 14 Oct 2024 15:22:18 -0700 Subject: [PATCH 3/3] Fixed example 3 document title typo --- examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino index d75c4d6..1c4eb10 100644 --- a/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino +++ b/examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino @@ -1,4 +1,4 @@ -/* SparkFun Ulrasonic Distance Sensor - Example 3 - Distance using Trigger and Echo Pins +/* SparkFun Ultrasonic Distance Sensor - Example 3 - Distance Using Trigger and Echo Pins * * Product: * * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-24805)