zondag 18 oktober 2020

i2c scanner code

 


//i found the scanning script at CircuitMagic.com so some credits go to them

#include <Wire.h>
#include <SPI.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9   // SDA op display
#define OLED_CLK   10   // SCL op display
#define OLED_DC    11   // DC op display
#define OLED_CS    12   // (niet op deze display aanwezig 
#define OLED_RESET 13   // RES op diplay
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  
void setup() {
   Wire.begin(); //i2c start
  Serial.begin(9600);
 
   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
 }

  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(" Spike ");
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10,30);             // Start at top-left corner
  display.println("Arduino");
  display.setCursor(10,50);             // Start at top-left corner
  display.println("Projects");
  display.display();// – call this method for the changes to make effect
  delay(2000);
  
}

void loop() {
 byte error, address;
  int nDevices;
  Serial.println("Scanning I2C bus...");
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println("Scanning ");
  display.setCursor(20,20);             // Start at top-left corner
  display.println("I2C Bus");
  display.display();// – call this method for the changes to make effect

  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)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setCursor(30,40);             // Start at top-left corner
  display.print("0x");
  display.print(address,HEX);
  display.display();//

delay(1000);
 
       nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0){
    Serial.println("No I2C devices \n");


  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,40);             // Start at top-left corner
  display.println("No I2C devices found");
  display.display();// – call this method for the changes to make effect
  }
 
  else
    Serial.println("done\n");
    display.clearDisplay();

  delay(1000); // wait 1 second for next scan
}

Geen opmerkingen:

Een reactie posten

pzem module op Wemos D1 ESP easy

pzem module voorzien van R 1K tussen rx en 5v RX van pzem op D8 TX van pzem op D7 Wemos Config esp easy RX: GPIO-13 (D7) TX: GPIO-15 (D8) ht...