Guest User

Fixed and expanded Jainrk I2C scanner

a guest
Sep 22nd, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. /*
  2. * i2c_port_address_scanner (fixed and expanded)
  3. * Scans ports D0 to D8 on an ESP8266 and searches for I2C device. based on the original code
  4. * available on Arduino.cc and later improved by user Krodal and Nick Gammon (www.gammon.com.au/forum/?id=10896)
  5. * D8/GPIO15 will fail because of the pulldown resistor, thus it has been left out.
  6. * Additionally, the original scanner used addresses 1-7 and 120-127 which are reserved addresses.
  7. * https://www.instructables.com/ESP8266-I2C-PORT-and-Address-Scanner/
  8. * https://github.com/jainrk/i2c_port_address_scanner
  9. *
  10. * Serial uses UART0, which is mapped to pins GPIO1 (TX) and GPIO3 (RX). If you need to use GPIO1 or GPIO3 for
  11. * I2C then Serial may be remapped to GPIO15 (TX) and GPIO13 (RX) by calling Serial.swap() after Serial.begin.
  12. * Calling Serial.swap() again maps UART0 back to GPIO1 and GPIO3.
  13. */
  14.  
  15. #include <Wire.h>
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19. //  Serial.swap();  
  20.   Serial.println("\n\nI2C Scanner to scan for devices on each port pair D0 to D7");
  21.   scanPorts();
  22. }
  23.  
  24. uint8_t portArray[] = {16, 5, 4, 0, 2, 14, 12, 13};
  25. //uint8_t portArray[] = {16, 5, 4, 0, 1, 2, 3, 14, 12}; //if using Serial.swap()
  26.  
  27. //String portMap[] = {"D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7"}; //for D1 Mini
  28. String portMap[] = {"GPIO16", "GPIO5", "GPIO4", "GPIO0", "GPIO2", "GPIO14", "GPIO12", "GPIO13"};
  29. //String portMap[] = {"GPIO16", "GPIO5", "GPIO4", "GPIO0", "GPIO1", "GPIO2", "GPIO3", "GPIO14", "GPIO12"}; //if using Serial.swap()
  30.  
  31. void scanPorts() {
  32.   for (uint8_t i = 0; i < sizeof(portArray); i++) {
  33.     for (uint8_t j = 0; j < sizeof(portArray); j++) {
  34.       if (i != j){
  35.         Serial.print("Scanning (SDA : SCL) - " + portMap[i] + " : " + portMap[j] + " - ");
  36.         Wire.begin(portArray[i], portArray[j]);
  37.         check_if_exist_I2C();
  38.       }
  39.     }
  40.   }
  41. }
  42.  
  43. void check_if_exist_I2C() {
  44.   byte error, address;
  45.   int nDevices;
  46.   nDevices = 0;
  47.   for (address = 8; address < 120; address++ )  {
  48.     // The i2c_scanner uses the return value of
  49.     // the Write.endTransmisstion to see if
  50.     // a device did acknowledge to the address.
  51.     Wire.beginTransmission(address);
  52.     error = Wire.endTransmission();
  53.     delayMicroseconds(100);
  54.    
  55.     if (error == 0){
  56.       Serial.print("I2C device found at address 0x");
  57.       if (address < 16)
  58.         Serial.print("0");
  59.       Serial.print(address, HEX);
  60.       Serial.println("  !");
  61.  
  62.       nDevices++;
  63.     } else if (error == 4) {
  64.       Serial.print("Unknown error at address 0x");
  65.       if (address < 16)
  66.         Serial.print("0");
  67.       Serial.println(address, HEX);
  68.     }
  69.   } //for loop
  70.   if (nDevices == 0)
  71.     Serial.println("No I2C devices found");
  72.   else
  73.     Serial.println("**********************************\n");
  74. //  delay(1000);           // wait 1 seconds for next scan
  75. }
  76.  
  77. void loop() {
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment