Skip to content

Instantly share code, notes, and snippets.

@AustinSaintAubin
Last active November 20, 2023 11:59
Show Gist options
  • Select an option

  • Save AustinSaintAubin/dc8abb2d168f5f7c27d65bb4829ca870 to your computer and use it in GitHub Desktop.

Select an option

Save AustinSaintAubin/dc8abb2d168f5f7c27d65bb4829ca870 to your computer and use it in GitHub Desktop.

Revisions

  1. AustinSaintAubin revised this gist Feb 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -103,7 +103,7 @@ void setup()
    display.setColor(WHITE);
    // - - - - - - - - - - - - - -
    display.setTextAlignment(TEXT_ALIGN_RIGHT);
    display.drawString(DISPLAY_WIDTH, 0, "v7");
    display.drawString(DISPLAY_WIDTH, 0, "v8");
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 0, "I2C Scanner w/ OLED");
    // - - - - - - - - - - - - - -
  2. AustinSaintAubin revised this gist Feb 18, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,8 @@
    Scans for i2c Devices
    Version History:
    Version 8
    Added preprocessor statments for display
    Version 6
    Added i2c Ports
    Added OLED output
  3. AustinSaintAubin revised this gist Feb 18, 2018. 1 changed file with 27 additions and 18 deletions.
    45 changes: 27 additions & 18 deletions I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    Title: I2C Scanner
    - Repository: https://gist.github.com/AustinSaintAubin/dc8abb2d168f5f7c27d65bb4829ca870
    Version: 7
    Version: 8
    Date: 2018 / 02 / 17
    Author: Austin St. Aubin
    Email: [email protected]
    @@ -41,13 +41,22 @@
    ============================================================================= */

    // [# Preprocessor Statements #]
    #define SCREEN true // Enable Debugging Output
    #define OLED_DISPLAY true // Enable OLED Display
    #define OLED_DISPLAY_SSD1306 // OLED Display Type: SSD1306(OLED_DISPLAY_SSD1306) / SH1106(OLED_DISPLAY_SH1106), comment this line out to disable oled

    // [ Included Library's ]
    #include <Wire.h>
    #if SCREEN

    // Initialize the OLED display using I2C
    #ifdef OLED_DISPLAY_SSD1306
    #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
    //#include "SH1106.h" // alias for `#include "SH1106Wire.h"`
    #elif defined OLED_DISPLAY_SH1106
    #include "SH1106.h" // alias for `#include "SH1106Wire.h"`
    #elif !OLED_DISPLAY
    #warning "OLED Display Disabled"
    #else
    #error "Undefined OLED Display Type"
    #endif
    /*For a connection via I2C using brzo_i2c (must be installed) include
    #include <brzo_i2c.h> Only needed for Arduino 1.6.5 and earlier
    #include "SSD1306Brzo.h"
    @@ -57,7 +66,6 @@
    #include "SSD1306Spi.h"
    #include "SH1106SPi.h"
    */
    #endif

    // [ Global Pin Constants / Varables ]
    /*Initialize the OLED display using SPI
    @@ -66,25 +74,26 @@
    D0 -> RES
    D2 -> DC
    D8 -> CS */
    const int pin_sda = D1; //SDA; // i2c SDA Pin
    const int pin_scl = D2; //SCL; // i2c SCL Pin
    const uint8_t I2C_SDA_PIN = D1; //SDA; // i2c SDA Pin
    const uint8_t I2C_SCL_PIN = D2; //SCL; // i2c SCL Pin

    // [ Global Classes ]
    #if SCREEN
    SSD1306 display(0x3c, pin_sda, pin_scl);
    //SH1106 display(0x3c, pin_sda, pin_scl);
    #ifdef OLED_DISPLAY_SSD1306
    SSD1306 display(0x3c, I2C_SDA_PIN, I2C_SCL_PIN);
    #elif defined OLED_DISPLAY_SH1106
    SH1106 display(0x3c, I2C_SDA_PIN, I2C_SCL_PIN);
    #endif

    void setup()
    {
    Wire.begin(pin_sda, pin_scl);
    Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);

    // Serial
    Serial.begin(115200);
    Serial.println("\nI2C Scanner");

    // OLED
    #if SCREEN
    #if OLED_DISPLAY
    display.init();
    display.flipScreenVertically();
    display.setContrast(255);
    @@ -110,7 +119,7 @@ void loop()

    Serial.println("Scanning...");

    #if SCREEN
    #if OLED_DISPLAY
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 14, "Scanning...");
    display.display();
    @@ -133,7 +142,7 @@ void loop()
    Serial.print(address,HEX);
    Serial.println(" !");

    #if SCREEN
    #if OLED_DISPLAY
    display.drawString(((nDevices -1) % 4) * 32, 23 + (round((nDevices -1) /4) *10), (String)nDevices + ":x" + String(address, HEX));
    display.display();
    #endif
    @@ -145,7 +154,7 @@ void loop()
    Serial.print("0");
    Serial.println(address,HEX);

    #if SCREEN
    #if OLED_DISPLAY
    display.drawString(((nDevices -1) % 4) * 32, 23 + (round((nDevices -1) /4) *10), "!:x" + String(address, HEX));
    display.display();
    #endif
    @@ -154,14 +163,14 @@ void loop()
    if (nDevices == 0) {
    Serial.println("No I2C devices found\n");

    #if SCREEN
    #if OLED_DISPLAY
    display.drawString(0, 23 , "No I2C devices found");
    display.display();
    #endif
    } else {
    Serial.println("done\n");

    #if SCREEN
    #if OLED_DISPLAY
    display.drawString(((nDevices -0) % 4) * 32, 23 + (round((nDevices -0) /4) *10), "done");
    display.display();
    #endif
    @@ -176,7 +185,7 @@ void loop()

    delay(5000); // wait 5 seconds for next scan

    #if SCREEN
    #if OLED_DISPLAY
    // Clear Bottom of Display
    display.setColor(BLACK);
    display.fillRect(0, 13, DISPLAY_WIDTH, DISPLAY_HEIGHT - 13);
  4. AustinSaintAubin revised this gist Feb 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@ void setup()
    display.setColor(WHITE);
    // - - - - - - - - - - - - - -
    display.setTextAlignment(TEXT_ALIGN_RIGHT);
    display.drawString(DISPLAY_WIDTH, 0, "v6");
    display.drawString(DISPLAY_WIDTH, 0, "v7");
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 0, "I2C Scanner w/ OLED");
    // - - - - - - - - - - - - - -
  5. AustinSaintAubin revised this gist Feb 18, 2018. 1 changed file with 22 additions and 7 deletions.
    29 changes: 22 additions & 7 deletions I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    /*===========================================================================
    Title: i2c Scanner
    Title: I2C Scanner
    - Repository: https://gist.github.com/AustinSaintAubin/dc8abb2d168f5f7c27d65bb4829ca870
    Version: 6
    Date: 2018 / 02 / 12
    Version: 7
    Date: 2018 / 02 / 17
    Author: Austin St. Aubin
    Email: [email protected]
    @@ -46,17 +46,33 @@
    // [ Included Library's ]
    #include <Wire.h>
    #if SCREEN
    #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
    #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
    //#include "SH1106.h" // alias for `#include "SH1106Wire.h"`
    /*For a connection via I2C using brzo_i2c (must be installed) include
    #include <brzo_i2c.h> Only needed for Arduino 1.6.5 and earlier
    #include "SSD1306Brzo.h"
    #include "SH1106Brzo.h"
    For a connection via SPI include
    #include <SPI.h> Only needed for Arduino 1.6.5 and earlier
    #include "SSD1306Spi.h"
    #include "SH1106SPi.h"
    */
    #endif

    // [ Global Pin Constants / Varables ]
    /*Initialize the OLED display using SPI
    D5 -> CLK
    D7 -> MOSI (DOUT)
    D0 -> RES
    D2 -> DC
    D8 -> CS */
    const int pin_sda = D1; //SDA; // i2c SDA Pin
    const int pin_scl = D2; //SCL; // i2c SCL Pin

    // [ Global Classes ]
    #if SCREEN
    SSD1306 display(0x3c, pin_sda, pin_scl);
    //SSD1306 display(0x3c, pin_sda, pin_scl);
    //SH1106 display(0x3c, pin_sda, pin_scl);
    #endif

    void setup()
    @@ -166,5 +182,4 @@ void loop()
    display.fillRect(0, 13, DISPLAY_WIDTH, DISPLAY_HEIGHT - 13);
    display.setColor(WHITE);
    #endif
    }

    }
  6. AustinSaintAubin revised this gist Feb 13, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /*===========================================================================
    Title: i2c Scanner
    - Repository:
    - Repository: https://gist.github.com/AustinSaintAubin/dc8abb2d168f5f7c27d65bb4829ca870
    Version: 6
    Date: 2018 / 02 / 12
  7. AustinSaintAubin created this gist Feb 13, 2018.
    170 changes: 170 additions & 0 deletions I2C_Scanner.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    /*===========================================================================
    Title: i2c Scanner
    - Repository:
    Version: 6
    Date: 2018 / 02 / 12
    Author: Austin St. Aubin
    Email: [email protected]
    Description:
    Scans for i2c Devices
    Version History:
    Version 6
    Added i2c Ports
    Added OLED output
    Version 5, March 28, 2013
    As version 4, but address scans now to 127.
    A sensor seems to use address 120.
    Version 4, March 3, 2013, Using Arduino 1.0.3
    by Arduino.cc user Krodal.
    Changes by louarnold removed.
    Scanning addresses changed from 0...127 to 1...119,
    according to the i2c scanner by Nick Gammon
    http://www.gammon.com.au/forum/?id=10896
    Version 3, Feb 26 2013
    V3 by louarnold
    Version 2, Juni 2012, Using Arduino 1.0.1
    Adapted to be as simple as possible by Arduino.cc user Krodal
    Version 1
    This program (or code that looks like it)
    can be found in many places.
    For example on the Arduino.cc forum.
    The original author is not known.
    Notes:
    This sketch tests the standard 7-bit addresses
    Devices with higher bit address might not be seen properly.
    ============================================================================= */

    // [# Preprocessor Statements #]
    #define SCREEN true // Enable Debugging Output

    // [ Included Library's ]
    #include <Wire.h>
    #if SCREEN
    #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
    #endif

    // [ Global Pin Constants / Varables ]
    const int pin_sda = D1; //SDA; // i2c SDA Pin
    const int pin_scl = D2; //SCL; // i2c SCL Pin

    // [ Global Classes ]
    #if SCREEN
    SSD1306 display(0x3c, pin_sda, pin_scl);
    //SSD1306 display(0x3c, pin_sda, pin_scl);
    #endif

    void setup()
    {
    Wire.begin(pin_sda, pin_scl);

    // Serial
    Serial.begin(115200);
    Serial.println("\nI2C Scanner");

    // OLED
    #if SCREEN
    display.init();
    display.flipScreenVertically();
    display.setContrast(255);
    display.setFont(ArialMT_Plain_10);
    display.setColor(WHITE);
    // - - - - - - - - - - - - - -
    display.setTextAlignment(TEXT_ALIGN_RIGHT);
    display.drawString(DISPLAY_WIDTH, 0, "v6");
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 0, "I2C Scanner w/ OLED");
    // - - - - - - - - - - - - - -
    display.drawLine(0, 12, DISPLAY_WIDTH, 12);
    // - - - - - - - - - - - - - -
    display.display();
    #endif
    }


    void loop()
    {
    byte error, address;
    int nDevices;

    Serial.println("Scanning...");

    #if SCREEN
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 14, "Scanning...");
    display.display();
    #endif

    nDevices = 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) {
    nDevices++;

    Serial.print("Device found @ 0x");
    if (address<16)
    Serial.print("0");
    Serial.print(address,HEX);
    Serial.println(" !");

    #if SCREEN
    display.drawString(((nDevices -1) % 4) * 32, 23 + (round((nDevices -1) /4) *10), (String)nDevices + ":x" + String(address, HEX));
    display.display();
    #endif
    }
    else if (error==4)
    {
    Serial.print("Unknow error @ 0x");
    if (address<16)
    Serial.print("0");
    Serial.println(address,HEX);

    #if SCREEN
    display.drawString(((nDevices -1) % 4) * 32, 23 + (round((nDevices -1) /4) *10), "!:x" + String(address, HEX));
    display.display();
    #endif
    }
    }
    if (nDevices == 0) {
    Serial.println("No I2C devices found\n");

    #if SCREEN
    display.drawString(0, 23 , "No I2C devices found");
    display.display();
    #endif
    } else {
    Serial.println("done\n");

    #if SCREEN
    display.drawString(((nDevices -0) % 4) * 32, 23 + (round((nDevices -0) /4) *10), "done");
    display.display();
    #endif
    }

    // DEBUGING
    // for(address = 40; address < 52; address++ ) {
    // nDevices++;
    // display.drawString(((nDevices -1) % 4) * 32, 23 + (round((nDevices -1) /4) *10), (String)nDevices + ":x" + String(address, HEX));
    // Serial.println(address,HEX);
    // }

    delay(5000); // wait 5 seconds for next scan

    #if SCREEN
    // Clear Bottom of Display
    display.setColor(BLACK);
    display.fillRect(0, 13, DISPLAY_WIDTH, DISPLAY_HEIGHT - 13);
    display.setColor(WHITE);
    #endif
    }