Skip to content

Example 3 Patch #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 28 additions & 47 deletions examples/Example3_Trigger_Echo/Example3_Trigger_Echo.ino
Original file line number Diff line number Diff line change
@@ -1,80 +1,61 @@
/* 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-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);
delay(500); // Must be no less than 25ms to ensure full 400cm sensor range
}