-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample02_SerialNumber.ino
More file actions
79 lines (62 loc) · 2.22 KB
/
Copy pathExample02_SerialNumber.ino
File metadata and controls
79 lines (62 loc) · 2.22 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Example 02 - Serial Number
Every SFA40 has a unique 48-bit serial number assigned by Sensirion during production.
This sketch reads it and prints it as a 12-digit hexadecimal value, which matches the
serial number laser-marked on the metal cap of the sensor.
Note: The serial number can only be read while the sensor is in idle state (no measurement
running). begin() leaves the sensor idle, so this sketch reads it before starting any
measurement.
SparkFun Electronics
Date: 2026
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Hardware Connections:
IoT RedBoard --> SFA40
QWIIC --> QWIIC
Open the Serial Monitor at 115200 baud.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/
*/
#include <SparkFun_SFA40.h>
SfeSFA40ArdI2C mySensor;
// Print an unsigned value as a fixed number of hexadecimal digits, with leading zeros.
void printHexPadded(uint32_t value, uint8_t digits)
{
for (int8_t shift = (digits - 1) * 4; shift >= 0; shift -= 4)
{
uint8_t nibble = (value >> shift) & 0x0F;
Serial.print(nibble, HEX);
}
}
void setup()
{
Serial.begin(115200);
Serial.println("SparkFun SFA40 Example 2 - Serial Number");
Wire.begin();
while (mySensor.begin() == false)
{
Serial.println("SFA40 not connected, check your wiring!");
delay(1000);
}
Serial.println("SFA40 connected!");
// Read the unique 48-bit serial number.
uint64_t serialNumber = 0;
if (mySensor.getSerialNumber(serialNumber) != ksfTkErrOk)
{
Serial.println("Failed to read serial number!");
return;
}
// A uint64_t cannot be printed directly on all platforms, so split it into the upper
// 16 bits and lower 32 bits and print each as zero-padded hex (4 + 8 = 12 digits).
uint32_t high = (uint32_t)(serialNumber >> 32) & 0xFFFF;
uint32_t low = (uint32_t)(serialNumber & 0xFFFFFFFF);
Serial.print("Serial number: 0x");
printHexPadded(high, 4);
printHexPadded(low, 8);
Serial.println();
Serial.println("(This should match the serial number laser-marked on the sensor cap.)");
}
void loop()
{
// Nothing to do here.
}