-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample1_BasicReadings.ino
More file actions
62 lines (50 loc) · 1.59 KB
/
Example1_BasicReadings.ino
File metadata and controls
62 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* SparkFun Ulrasonic Distance Sensor - Example 1 Basic Distance Sensing
*
* Product:
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
* * https://www.sparkfun.com/1XXXX
*
* Written By: Elias Santistevan
* Date: 06/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;
void setup()
{
// Start serial
Serial.begin(115200);
Serial.println("Ultrasonic Distance Sensor Example 1 - Basic Readings");
Wire.begin();
// 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()
{
uint16_t distance = 0;
myUltrasonic.triggerAndRead(distance);
// Print measurement
Serial.print("Distance (mm): ");
Serial.println(distance);
//Serial.println("Distance (cm): ");
//Serial.print((distance / 10.0), 2);
//Serial.println("Distace (in): ");
//Serial.print((distance / 25.4), 2);
// Wait a bit
delay(250);
}