arduino_i2c_scan.ino
1 // SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries 2 // SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries 3 // 4 // SPDX-License-Identifier: MIT 5 // -------------------------------------- 6 // PiCowbell Proto and Pico I2C scan code. 7 // 8 // Modified from https://playground.arduino.cc/Main/I2cScanner/ 9 // -------------------------------------- 10 11 #include <Wire.h> 12 13 // Wire uses GPIO4 (SDA) and GPIO5 (SCL) automatically. 14 #define WIRE Wire 15 16 void setup() { 17 WIRE.begin(); 18 19 Serial.begin(9600); 20 while (!Serial) 21 delay(10); 22 Serial.println("\nPiCowbell Proto Pico I2C Scanner"); 23 } 24 25 26 void loop() { 27 byte error, address; 28 int nDevices; 29 30 Serial.println("Scanning..."); 31 32 nDevices = 0; 33 for(address = 1; address < 127; address++ ) 34 { 35 // The i2c_scanner uses the return value of 36 // the Write.endTransmission to see if 37 // a device did acknowledge the address. 38 WIRE.beginTransmission(address); 39 error = WIRE.endTransmission(); 40 41 if (error == 0) 42 { 43 Serial.print("I2C device found at address 0x"); 44 if (address<16) 45 Serial.print("0"); 46 Serial.print(address,HEX); 47 Serial.println(" !"); 48 49 nDevices++; 50 } 51 else if (error==4) 52 { 53 Serial.print("Unknown error at address 0x"); 54 if (address<16) 55 Serial.print("0"); 56 Serial.println(address,HEX); 57 } 58 } 59 if (nDevices == 0) 60 Serial.println("No I2C devices found\n"); 61 else 62 Serial.println("done\n"); 63 64 delay(5000); // wait 5 seconds for next scan 65 }