This gist was automatically pasted at the request of the code author or one of the discord server helpers. If you have any suggestions or bugs to report, you can do so on our GitHub repository, or in our discord server. This project is run by volunteers so feel free to fork and commit your changes then open a pull request!
Created
January 5, 2021 17:40
-
-
Save ArduinoDiscordBot/3fab7931af01b54d903cf9888e6decb7 to your computer and use it in GitHub Desktop.
Code by Doddlin#9941 - Tue Jan 05 2021 17:40:32 GMT+0000 (Coordinated Universal Time)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c++ | |
#include <Wire.h> //include Wire.h library | |
#include <Arduino.h> | |
#include <Adafruit_Sensor.h> | |
#include <SPI.h> | |
int sda = 5; | |
int scl = 4; | |
void setup() | |
{ | |
Wire.begin(sda, scl); // Wire communication begin | |
Serial.begin(9600); // The baudrate of Serial monitor is set in 9600 | |
while (!Serial); // Waiting for Serial Monitor | |
Serial.println("\nI2C address Scanner CircuitSchools.com"); | |
} | |
void loop() | |
{ | |
byte error, address; //variable for error and I2C address | |
int devicecount; | |
Serial.println("Scanning..."); | |
devicecount = 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("I2C device found at address 0x"); | |
if (address < 16) | |
Serial.print("0"); | |
Serial.print(address, HEX); | |
Serial.println(" !"); | |
devicecount++; | |
} | |
else if (error == 4) | |
{ | |
Serial.print("Unknown error at address 0x"); | |
if (address < 16) | |
Serial.print("0"); | |
Serial.println(address, HEX); | |
} else { | |
Serial.print("Searching"); | |
Serial.println(address, HEX); | |
} | |
} | |
if (devicecount == 0) | |
Serial.println("No I2C devices found\n"); | |
else | |
Serial.println("done\n"); | |
delay(5000); // wait 5 seconds for the next I2C scan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment