-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2cscanner.cpp
More file actions
57 lines (48 loc) · 1.29 KB
/
i2cscanner.cpp
File metadata and controls
57 lines (48 loc) · 1.29 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
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------
// https://i2cdevices.org/devices
#include <Arduino.h>
#include <Wire.h>
// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire
void i2CScanner() {
byte error, address;
int devices;
Serial.println(F("Scanning..."));
devices = 0;
for (address = 1; address < 127; address++)
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
WIRE.beginTransmission(address);
error = WIRE.endTransmission();
if (error == 0)
{
Serial.print(F("I2C device found at address 0x"));
if (address < 16)
Serial.print(F("0"));
Serial.print(address,HEX);
Serial.println(F("!"));
devices++;
}
else if (error == 4)
{
Serial.print(F("Unknown error at address 0x"));
if (address < 16)
Serial.print(F("0"));
Serial.println(address,HEX);
}
}
if (devices == 0) {
Serial.println(F("No I2C devices found."));
return;
}
Serial.println(F("...scanning done!"));
}