Skip to content

Commit edc5e99

Browse files
authored
Merge pull request #1 from sparkfun/v1.0.0
First implementation
2 parents b1e2de5 + a6a3c18 commit edc5e99

13 files changed

+312
-79
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SparkFun_Template_Arduino_Library
1+
# SparkFun_Qwiic_Ultrasonic_Arduino_Library
22
========================================
33

44
<table class="table table-hover table-striped table-bordered">

documents/HC_SR04_Datasheet.pdf

78.7 KB
Binary file not shown.

documents/add_documents_here.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/Example01_BasicUsage/Example01_BasicUsage.ino

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
2+
3+
// Create an ultrasonic sensor object
4+
QwiicUltrasonic myUltrasonic;
5+
6+
// Here we set the device address. Note that an older version of the Qwiic
7+
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
8+
// you'll need to change the address below. It is also recommended to run
9+
// Example 2 to change the address to the new default.
10+
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
11+
// uint8_t deviceAddress = 0x00;
12+
13+
void setup()
14+
{
15+
// Start serial
16+
Serial.begin(115200);
17+
Serial.println("Qwiic Ultrasonic Example 1 - Basic Readings");
18+
19+
Wire.begin();
20+
21+
// Attempt to begin the sensor
22+
while (myUltrasonic.begin(deviceAddress) == false)
23+
{
24+
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
25+
delay(1000);
26+
}
27+
28+
Serial.println("Ultrasonic sensor connected!");
29+
}
30+
31+
void loop()
32+
{
33+
// Get measurement from sensor. Note that the mesaured distance actually
34+
// comes from the previous trigger, so measurements will be slightly delayed
35+
uint16_t distance = 0;
36+
myUltrasonic.triggerAndRead(distance);
37+
38+
// Print measurement
39+
Serial.print("Distance (mm): ");
40+
Serial.println(distance);
41+
42+
// Wait a bit
43+
delay(100);
44+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
2+
3+
// Create an ultrasonic sensor object
4+
QwiicUltrasonic myUltrasonic;
5+
6+
// Here we set the device address. Note that an older version of the Qwiic
7+
// Ultrasonic firmware used a default address of 0x00. If yours uses 0x00,
8+
// you'll need to change the address below. It is also recommended to run
9+
// Example 2 to change the address to the new default.
10+
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
11+
// uint8_t deviceAddress = 0x00;
12+
13+
void setup()
14+
{
15+
// Start serial
16+
Serial.begin(115200);
17+
Serial.println("Qwiic Ultrasonic Example 2 - Change Address");
18+
19+
Wire.begin();
20+
21+
// Attempt to begin the sensor
22+
while (myUltrasonic.begin(deviceAddress) == false)
23+
{
24+
Serial.println("Ultrasonic sensor not connected, check your wiring and I2C address!");
25+
delay(1000);
26+
}
27+
28+
Serial.println("Ultrasonic sensor connected!");
29+
30+
// Repeat until the address has been successfully changed
31+
bool addressChanged = false;
32+
while (addressChanged == false)
33+
{
34+
// Print instructions
35+
Serial.println();
36+
Serial.println("Please enter a new address for the sensor.");
37+
Serial.println("Any value between 0x20 and 0x2F is valid.");
38+
Serial.println("Enter the address in hexadecimal without the `0x`.");
39+
Serial.println();
40+
41+
// Clear serial buffer and wait for input
42+
while (Serial.available() > 0)
43+
Serial.read();
44+
while (Serial.available() == 0)
45+
;
46+
47+
// Read input from user
48+
char input[4]; // Only expecting a few characters
49+
size_t numBytesRead = Serial.readBytesUntil('\n', input, 4);
50+
51+
// Parse input using strtoul (STRing TO Unsigned Long integer)
52+
uint8_t newAddress = strtoul(input, nullptr, 16);
53+
54+
Serial.print("Parsed address: ");
55+
Serial.println(newAddress, HEX);
56+
57+
// Check if the address is valid
58+
if (newAddress < kQwiicUltrasonicMinAddress || newAddress > kQwiicUltrasonicMaxAddress)
59+
{
60+
Serial.println("Invalid address!");
61+
continue;
62+
}
63+
64+
// Address is valid, attempt to change it on the device
65+
sfeTkError_t err = myUltrasonic.changeAddress(newAddress);
66+
67+
// Check whether the address was changed successfully
68+
if (err)
69+
Serial.println("Failed to change address!");
70+
71+
// Success, we're done here!
72+
addressChanged = true;
73+
}
74+
75+
Serial.println("Address changed successfully! Continuing...");
76+
77+
// Wait a moment so user can read the messages
78+
delay(1000);
79+
}
80+
81+
void loop()
82+
{
83+
// Get measurement from sensor. Note that the mesaured distance actually
84+
// comes from the previous trigger, so measurements will be slightly delayed
85+
uint16_t distance = 0;
86+
myUltrasonic.triggerAndRead(distance);
87+
88+
// Print measurement
89+
Serial.print("Distance (mm): ");
90+
Serial.println(distance);
91+
92+
// Wait a bit
93+
delay(100);
94+
}

keywords.txt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
#########################################################
2-
# Syntax Coloring Map for TODO: Add name #
2+
# Syntax Coloring Map for the Qwiic Ultrasonic Sensor #
33
#########################################################
44
# Class
55
#########################################################
66

7-
# TODO: Add this
8-
TemplateClass KEYWORD1
7+
QwiicUltrasonic KEYWORD1
98

109
#########################################################
1110
# Methods and Functions
1211
#########################################################
1312

14-
# TODO: Add these
1513
begin KEYWORD2
14+
isConnected KEYWORD2
15+
triggerAndRead KEYWORD2
16+
changeAddress KEYWORD2
1617

1718
#########################################################
1819
# Constants
1920
#########################################################
2021

21-
# TODO: Add these
22-
TEMPALTE_CONSTANT LITERAL1
23-
24-
#########################################################
25-
# Structs
26-
#########################################################
27-
28-
# TODO: Add these
29-
template_struct_t LITERAL3
22+
QWIIC_ULTRASONIC_ADDRESSES LITERAL1
23+
QWIIC_ULTRASONIC_NUM_ADDRESSES LITERAL1
24+
QWIIC_ULTRASONIC_MIN_ADDRESS LITERAL1
25+
QWIIC_ULTRASONIC_MAX_ADDRESS LITERAL1
26+
QWIIC_ULTRASONIC_DEFAULT_ADDRESS LITERAL1
27+
QWIIC_ULTRASONIC_REGISTER_TRIGGER LITERAL1

library.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name=SparkFun Template Arduino Library
1+
name=SparkFun Qwiic Ultrasonic Arduino Library
22
version=1.0.0
33
author=SparkFun Electronics <techsupport@sparkfun.com>
44
maintainer=SparkFun Electronics <sparkfun.com>
5-
sentence=TODO: Add short description
6-
paragraph=TODO: Add long description
7-
category=TODO: Add category (Sensors, Display, Communication, etc.)
8-
url=TODO: Add URL
9-
architectures=TODO: Add architectures (*, esp32, teensy, stm32, megaavr, etc.)
5+
sentence=A library to use the SparkFun Qwiic Ultrasonic Distance Sensor
6+
paragraph=
7+
category=Sensors
8+
url=https://github.com/sparkfun/SparkFun_Qwiic_Ultrasonic_Arduino_Library
9+
architectures=*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "Arduino.h"
4+
#include "sfeQwiicUltrasonic.h"
5+
#include <Wire.h>
6+
7+
class QwiicUltrasonic : public sfeQwiicUltrasonic
8+
{
9+
public:
10+
/// @brief Begins the Qwiic Ultrasonic sensor
11+
/// @param address I2C device address to use for the sensor
12+
/// @param wirePort Wire port to use for I2C communication
13+
/// @return True if successful, false otherwise
14+
bool begin(uint8_t address = kQwiicUltrasonicDefaultAddress, TwoWire &wirePort = Wire)
15+
{
16+
// Setup Arudino I2C bus
17+
_theI2CBus.init(wirePort, address);
18+
19+
// Begin the sensor
20+
return sfeQwiicUltrasonic::begin(&_theI2CBus) == kSTkErrOk;
21+
}
22+
23+
/// @brief Checks if the Qwiic Ultrasonic sensor is connected
24+
/// @return True if the sensor is connected, false otherwise
25+
bool isConnected()
26+
{
27+
return sfeQwiicUltrasonic::isConnected() == kSTkErrOk;
28+
}
29+
30+
private:
31+
sfeTkArdI2C _theI2CBus;
32+
};

src/SparkFun_Template_Arduino_Library.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/SparkFun_Template_Arduino_Library.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/sfeQwiicUltrasonic.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "sfeQwiicUltrasonic.h"
2+
3+
sfeTkError_t sfeQwiicUltrasonic::begin(sfeTkII2C *theBus)
4+
{
5+
// Nullptr check
6+
if (theBus == nullptr)
7+
return kSTkErrFail;
8+
9+
// Check the device address
10+
if (theBus->address() < kQwiicUltrasonicMinAddress || theBus->address() > kQwiicUltrasonicMaxAddress)
11+
{
12+
// An older version of the firmware used 0x00 as the default address.
13+
// It's not really a valid address, but we need to allow it. Any other
14+
// address can't be used
15+
if (theBus->address() != 0x00)
16+
return kSTkErrFail;
17+
}
18+
19+
// Set bus pointer
20+
_theBus = theBus;
21+
22+
// Just check if the device is connected, no other setup is needed
23+
return isConnected();
24+
}
25+
26+
sfeTkError_t sfeQwiicUltrasonic::isConnected()
27+
{
28+
// Just ping the device address, there's no product ID register to check
29+
return _theBus->ping();
30+
}
31+
32+
sfeTkError_t sfeQwiicUltrasonic::triggerAndRead(uint16_t &distance)
33+
{
34+
size_t bytesRead = 0;
35+
uint8_t rawData[2] = {0, 0};
36+
37+
// Attempt to read the distance
38+
sfeTkError_t err = _theBus->readRegisterRegion(kQwiicUltrasonicRegisterTrigger, rawData, 2, bytesRead);
39+
40+
// Check whether the read was successful
41+
if (err != kSTkErrOk)
42+
return err;
43+
44+
// Store raw data
45+
distance = (rawData[0] << 8) | rawData[1];
46+
47+
// Done!
48+
return kSTkErrOk;
49+
}
50+
51+
sfeTkError_t sfeQwiicUltrasonic::changeAddress(const uint8_t &address)
52+
{
53+
// Check whether the address is valid
54+
if (address < kQwiicUltrasonicMinAddress || address > kQwiicUltrasonicMaxAddress)
55+
return kSTkErrFail;
56+
57+
// Write the new address to the device. The first bit must be set to 1
58+
sfeTkError_t err = _theBus->writeByte(address | 0x80);
59+
60+
// Check whether the write was successful
61+
if (err != kSTkErrOk)
62+
return err;
63+
64+
// Update the address in the bus
65+
_theBus->setAddress(address);
66+
67+
// Done!
68+
return kSTkErrOk;
69+
}
70+
71+
uint8_t sfeQwiicUltrasonic::getAddress()
72+
{
73+
return _theBus->address();
74+
}

0 commit comments

Comments
 (0)