Skip to content

Commit 96f7fa8

Browse files
authored
Merge pull request #5 from sparkfun/release/v01.02.00
Release/v01.02.00
2 parents c1d2217 + 6fcec52 commit 96f7fa8

File tree

6 files changed

+151
-118
lines changed

6 files changed

+151
-118
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ The following examples are provided with the library
8585

8686
## Documentation
8787

88-
The full API and use documentation for this library is provided [here](http://docs.sparkfun.com/SparkFun_Qwiic_Ultrasonic_Arduino_Library/). For a quick reference, the main methods available in the library are listed [here](https://docs.sparkfun.com/SparkFun_Qwiic_Ultrasonic_Arduino_Library/functions.html).
88+
89+
The full API and use documentation for this library is provided [here](http://docs.sparkfun.com/SparkFun_Qwiic_Ultrasonic_Arduino_Library/). For a quick reference, the main methods available in the library are listed [here](https://docs.sparkfun.com/SparkFun_Qwiic_Ultrasonic_Arduino_Library/class_qwiic_ultrasonic.html).
8990

9091
Curious about the hardware this board works with - visit the SparkFun Qwiic Ultrasonic Distance Sensor [hardware repository](https://github.com/sparkfun/SparkFun_Ultrasonic_Distance_Sensor-Qwiic).
9192

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* SparkFun Ulrasonic Distance Sensor - Example 4 Changing the Ultrasonic's Address
22
* To reset the original I2C address, ground the "RST" pad on the backside of the board.
33
* by touching a wire to the pad and then to ground. The address will be reset to 0x2F.
4-
*
5-
* Product:
4+
*
5+
* Product:
66
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
77
* * https://www.sparkfun.com/1XXXX
8-
*
8+
*
99
* Written By: Elias Santistevan
1010
* Date: 06/2024
1111
*
@@ -22,7 +22,7 @@ QwiicUltrasonic myUltrasonic;
2222
// sensor, then uncomment the line below and comment out the line above.
2323
// You can check the version in copper by the "R" on the front of the board.
2424
// Newer versions are >v10 and older versions simply do not have a version number.
25-
//QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWOld);
25+
// QwiicUltrasonic myUltrasonic(kQwiicUltrasonicFWOld);
2626

2727
// Here we set the device address. Note that an older version of the Qwiic
2828
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
@@ -33,71 +33,67 @@ uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
3333

3434
// New addres is 7-bit unshifted.
3535
uint8_t newAddr = 0x20;
36-
//If using an older version of the Qwiic Ultrasonic, your address range is: 0x20 - 0x2F
37-
//uint8_t newAddr = 0x2F;
38-
36+
// If using an older version of the Qwiic Ultrasonic, your address range is: 0x20 - 0x2F
37+
// uint8_t newAddr = 0x2F;
3938

4039
void setup()
4140
{
4241
// Start serial
43-
Serial.begin(115200);
44-
Serial.println("Ultrasonic Distance Sensor - Example 4 - Change Address");
45-
46-
Wire.begin();
42+
Serial.begin(115200);
43+
Serial.println("Ultrasonic Distance Sensor - Example 4 - Change Address");
4744

48-
// This sketch wont' run until you open the serial monitor
49-
while(!Serial)
50-
;
45+
Wire.begin();
5146

52-
// Attempt to begin the sensor
53-
while (myUltrasonic.begin(deviceAddress) == false)
54-
{
55-
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
56-
delay(2000);
57-
}
47+
// This sketch wont' run until you open the serial monitor
48+
while (!Serial)
49+
;
5850

59-
Serial.println("Ready to change address.");
60-
delay(1000);
61-
62-
Serial.print("Changing Address To: ");
63-
Serial.println(newAddr, HEX);
51+
// Attempt to begin the sensor
52+
while (myUltrasonic.begin(deviceAddress) == false)
53+
{
54+
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
55+
delay(2000);
56+
}
6457

58+
Serial.println("Ready to change address.");
59+
delay(1000);
6560

66-
// Call change address.....
67-
sfeTkError_t err = myUltrasonic.changeAddress(newAddr);
61+
Serial.print("Changing Address To: ");
62+
Serial.println(newAddr, HEX);
6863

64+
// Call change address.....
65+
sfTkError_t err = myUltrasonic.changeAddress(newAddr);
6966

70-
if(err)
71-
{
72-
while(1)
67+
if (err != ksfTkErrOk)
7368
{
74-
Serial.print("Error changing address: ");
75-
Serial.println(err);
76-
delay(1000);
69+
while (1)
70+
{
71+
Serial.print("Error changing address: ");
72+
Serial.println(err);
73+
delay(1000);
74+
}
7775
}
78-
}
79-
80-
Serial.println("Address changed successfully!");
81-
Serial.println("Reading Distance at new address......");
82-
delay(3000);
8376

77+
Serial.println("Address changed successfully!");
78+
Serial.println("Reading Distance at new address......");
79+
delay(3000);
8480
}
8581

8682
void loop()
8783
{
88-
uint16_t distance = 0;
89-
myUltrasonic.triggerAndRead(distance);
84+
uint16_t distance = 0;
85+
myUltrasonic.triggerAndRead(distance);
9086

91-
// Print measurement
92-
Serial.print("Distance (mm): ");
93-
Serial.println(distance);
87+
// Print measurement
88+
Serial.print("Distance (mm): ");
89+
Serial.println(distance);
9490

95-
//Serial.println("Distance (cm): ");
96-
//Serial.print((distance / 10.0), 2);
91+
// Serial.println("Distance (cm): ");
92+
// Serial.print((distance / 10.0), 2);
9793

98-
//Serial.println("Distace (in): ");
99-
//Serial.print((distance / 25.4), 2);
94+
// Serial.println("Distace (in): ");
95+
// Serial.print((distance / 25.4), 2);
10096

101-
// Wait a bit
102-
delay(100);
97+
// Wait a bit
98+
delay(100);
10399
}

library.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=SparkFun Qwiic Ultrasonic Arduino Library
2-
version=1.1.2
2+
version=1.2.0
33
author=SparkFun Electronics <techsupport@sparkfun.com>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=A library to use the SparkFun Qwiic Ultrasonic Distance Sensor
66
paragraph=The SparkFun Qwiic Ultrasonic Distance Sensor, a user-friendly upgrade to the popular HC-SR04 sensor, offers non-contact distance measurement from 2cm to 400cm with an accuracy of 3mm. This sensor is designed to seamlessly integrate with the Qwiic ecosystem for I2C communication, making it a breeze to add to your existing projects and daisy-chain with other Qwiic components.
77
category=Sensors
88
url=https://github.com/sparkfun/SparkFun_Qwiic_Ultrasonic_Arduino_Library
99
architectures=*
10-
depends=SparkFun Toolkit (<1.0.0)
10+
depends=SparkFun Toolkit (>=1.0.0)
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
1-
/* SparkFun Ulrasonic Distance Sensor
2-
*
3-
* Product:
4-
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
5-
* * https://www.sparkfun.com/1XXXX
6-
*
7-
* SPDX-License-Identifier: MIT
1+
/*!
2+
* @file SparkFun_Qwiic_Ultrasonic_Arduino_Library.h
3+
* @brief Arduino header file for the SparkFun Qwiic Ultrasonic Arduino library
4+
*
5+
* This file contains the class definitions for the SparkFun Qwiic Ultrasonic Arduino library,
6+
* including methods for initialization, reading ultrasonic values, and changing the I2C address.
87
*
9-
* Copyright (c) 2024 SparkFun Electronics
8+
* @author SparkFun Electronics
9+
* @date 2024
10+
* @copyright Copyright (c) 2024-2025, SparkFun Electronics Inc. This project is released under the MIT License.
11+
*
12+
* SPDX-License-Identifier: MIT
1013
*/
1114

1215
#pragma once
1316

17+
// clang-format off
18+
#include <SparkFun_Toolkit.h>
19+
#include "sfTk/sfDevUltrasonicDist.h"
1420
#include "Arduino.h"
15-
#include "sfeQwiicUltrasonic.h"
1621
#include <Wire.h>
22+
// clang-format on
1723

18-
class QwiicUltrasonic : public sfeQwiicUltrasonic
24+
class QwiicUltrasonic : public sfDevUltrasonicDist
1925
{
2026
public:
21-
/// @brief Begins the Qwiic Ultrasonic sensor
22-
/// @param address I2C device address to use for the sensor
23-
/// @param wirePort Wire port to use for I2C communication
24-
/// @return True if successful, false otherwise
27+
/**
28+
* @brief Begins the Qwiic Ultrasonic sensor
29+
*
30+
* This method initializes the Qwiic Ultrasonic sensor by setting up the I2C bus
31+
* and starting communication with the sensor.
32+
*
33+
* @param address I2C device address to use for the sensor
34+
* @param wirePort Wire port to use for I2C communication
35+
* @return True if successful, false otherwise
36+
*/
2537
bool begin(uint8_t address = kQwiicUltrasonicDefaultAddress, TwoWire &wirePort = Wire)
2638
{
27-
// Setup Arudino I2C bus
39+
// Setup Arduino I2C bus
2840
_theI2CBus.init(wirePort, address);
2941

3042
// Begin the sensor
31-
return sfeQwiicUltrasonic::begin(&_theI2CBus) == kSTkErrOk;
43+
return sfDevUltrasonicDist::begin(&_theI2CBus) == ksfTkErrOk;
3244
}
3345

34-
/// @brief Checks if the Qwiic Ultrasonic sensor is connected
35-
/// @return True if the sensor is connected, false otherwise
46+
/**
47+
* @brief Checks if the Qwiic Ultrasonic sensor is connected
48+
*
49+
* This method verifies if the Qwiic Ultrasonic sensor is properly connected
50+
* by checking the communication status.
51+
*
52+
* @return True if the sensor is connected, false otherwise
53+
*/
3654
bool isConnected()
3755
{
38-
return sfeQwiicUltrasonic::isConnected() == kSTkErrOk;
56+
return sfDevUltrasonicDist::isConnected() == ksfTkErrOk;
3957
}
4058

4159
private:
42-
sfeTkArdI2C _theI2CBus;
60+
/**
61+
* @brief I2C bus instance for communication with the sensor
62+
*
63+
* This variable holds the I2C bus instance used for communication with the Qwiic Ultrasonic sensor.
64+
*/
65+
sfTkArdI2C _theI2CBus;
4366
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
/* SparkFun Ulrasonic Distance Sensor
1+
/**
2+
* @file sfDevUltrasonicDist.cpp
3+
* @brief Implementation file for the Ultrasonic Distance sensor class
24
*
3-
* Product:
4-
* * SparkFun Qwiic Ultrasonic Distance Sensor - HC-SR04 (SEN-1XXXX)
5-
* * https://www.sparkfun.com/1XXXX
5+
* This file contains the implementation of the ultrasonic distance sensor class, including
6+
* methods for initialization, measuring distance values, and setting the I2C address.
67
*
7-
* SPDX-License-Identifier: MIT
8+
* @author SparkFun Electronics
9+
* @date 2024
10+
* @copyright Copyright (c) 2024-2025, SparkFun Electronics Inc. This project is released under the MIT License.
811
*
9-
* Copyright (c) 2024 SparkFun Electronics
12+
* SPDX-License-Identifier: MIT
1013
*/
1114

12-
#include "sfeQwiicUltrasonic.h"
13-
#include "sfeTk/sfeTkError.h"
15+
#include "sfDevUltrasonicDist.h"
1416

15-
sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
17+
//------------------------------------------------------------------------
18+
sfTkError_t sfDevUltrasonicDist::begin(sfTkII2C *theBus)
1619
{
1720
// Nullptr check
1821
if (theBus == nullptr)
19-
return kSTkErrFail;
22+
return ksfTkErrFail;
2023

2124
// Check the device address
2225
if (_fwVersion == kQwiicUltrasonicFWOld)
@@ -27,7 +30,7 @@ sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
2730
// It's not really a valid address, but we need to allow it. Any other
2831
// address can't be used
2932
if (theBus->address() != 0x00)
30-
return kSTkErrFail;
33+
return ksfTkErrFail;
3134
}
3235
}
3336
// Set bus pointer
@@ -37,78 +40,79 @@ sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
3740
return isConnected();
3841
}
3942

40-
sfeTkError_t sfeQwiicUltrasonic::isConnected()
43+
//------------------------------------------------------------------------
44+
sfTkError_t sfDevUltrasonicDist::isConnected()
4145
{
4246
// Just ping the device address, there's no product ID register to check
4347
return _theBus->ping();
4448
}
4549

46-
sfeTkError_t sfeQwiicUltrasonic::triggerAndRead(uint16_t &distance)
50+
//------------------------------------------------------------------------
51+
sfTkError_t sfDevUltrasonicDist::triggerAndRead(uint16_t &distance)
4752
{
4853
size_t bytesRead;
4954
size_t numBytes = 2;
5055
uint8_t rawData[2] = {};
5156

5257
// Get the distance
53-
sfeTkError_t err = _theBus->readRegisterRegion(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
58+
sfTkError_t err = _theBus->readRegister(kUltrasonicDistanceReadCommand, rawData, numBytes, bytesRead);
5459

5560
// Check whether the read was successful
56-
if (err != kSTkErrOk)
61+
if (err != ksfTkErrOk)
5762
return err;
5863

5964
// Store raw data
6065
distance = (rawData[0] << 8) | rawData[1];
6166

6267
// Done!
63-
return kSTkErrOk;
68+
return ksfTkErrOk;
6469
}
6570

66-
sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
71+
//------------------------------------------------------------------------
72+
sfTkError_t sfDevUltrasonicDist::changeAddress(uint8_t &address)
6773
{
6874
// Check whether the address is valid
6975

70-
sfeTkError_t err;
76+
sfTkError_t err;
7177

7278
if (_fwVersion == kQwiicUltrasonicFWOld)
7379
{
7480
// Old firmware only supports a limited range of addresses.
7581
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
76-
return kSTkErrFail;
82+
return ksfTkErrFail;
7783

7884
// Write the new address to the device. The first bit must be set to 1
79-
err = _theBus->writeByte(address | 0x80);
85+
err = _theBus->writeUInt8(address | 0x80);
8086
}
8187
else if (_fwVersion == kQwiicUltrasonicFWLatest)
8288
{
8389
size_t numBytes = 2;
8490
// Latest firmware versions supports all of the available I2C addresses.
8591
if (address < kQwiicUltrasonicI2CAddressMin || address > kQwiicUltrasonicI2CAddressMax)
86-
return kSTkErrFail;
92+
return ksfTkErrFail;
8793

8894
// We want to shift the address left before we send it.
8995
uint8_t tempAddress = address << 1;
9096
const uint8_t toWrite[2] = {kUltrasonicAddressChangeCommand, tempAddress};
9197

9298
// Write the new address to the device
93-
err = _theBus->writeRegion(toWrite, numBytes);
99+
err = _theBus->writeData(toWrite, numBytes);
94100
}
95101
else
96102
{
97103
// There was some error setting the version in the constructor
98104
// return an error.
99-
return kSTkErrFail;
105+
return ksfTkErrFail;
100106
}
101107

102108
_theBus->setAddress(address);
103-
// Check whether the write was successful
104-
if (err != kSTkErrOk)
105-
return err;
106109

107-
// Done!
108-
return kSTkErrOk;
110+
// return err
111+
return err;
109112
}
110113

111-
uint8_t sfeQwiicUltrasonic::getAddress()
114+
//------------------------------------------------------------------------
115+
uint8_t sfDevUltrasonicDist::getAddress()
112116
{
113117
return _theBus->address();
114118
}

0 commit comments

Comments
 (0)