Dear reader
I have a I2C scanner code that prints out I2C addresses and if a device is connected to the I2C bus or not. It works perfect for the first 0...123 addresses till the last line 4...127 needs to be printed
My question : how is it possible that the last line of addresses is printed after the "#2#" while the code illustrates that this is not possible. Hope someone can tell me.
This code is run on an ESP8266 and uses the Arduino libraries.
The same code run on a ATmega 328 does not show this strange behavior.
This is the code
#include <arduino.h>
#include <Wire.h>
#define STOP true
#define MASTER_ADR 0x01
#define SDA_PIN 13 // GPIO4, PCB-D2 can be any free pin on ESP8266
#define SCL_PIN 12 // GPIO5, PCB-D1 can be any free pin on ESP8266
uint8_t I2C_scan(uint8_t mode){ // mode0=no output, Mode1= Summery, mode2=full output
uint8_t device_cnt=0, I2C_adr=0, cnt;
uint8_t rc, colcnt=0, collumns=4;
// struct I2C_devices {
// uint8_t I2C_adr;
// const char* deviceID;
// const char* type;
// } I2Cdevice[]={{0x36,"AS5600","Magn Rotary"},{0x27, "PCF8574","GPIO(diplay)"},{0x3C,"DS1306","OLED-Disp"}, {0x68,"DS3231","RTC"}, 0x57, "AT24C32", "2KB Mem" };
struct I2C_devices {
uint8_t I2C_adr;
const char *deviceID;
const char *type;
}
I2Cdevice[]={
{0x36,"AS5600","Magn Rotary"},
{0x27, "PCF8574","GPIO(diplay)"},
{0x3C,"DS1306","OLED-Disp"},
{0x68,"DS3231","RTC"},
{0x57, "AT24C32", "2KB Mem"},
};
switch (mode){
case 2 : {
printf("\nI2C Scanner\n");
for(cnt= 1; cnt <= 14*collumns+5; cnt++) printf("%s","=");
printf("\n ");
for(colcnt=0;colcnt<collumns;colcnt++) printf("%6s%02u%6s","",colcnt,"|");
for(I2C_adr = 0; I2C_adr <= 127; I2C_adr++) {
Wire.beginTransmission(I2C_adr);
rc = Wire.endTransmission();
if ( ((I2C_adr)%collumns)==0) printf("\n %03u\t", I2C_adr);
// printf("0x%02X = %3s | ",I2C_adr, rc ? " - " : "Fnd");
if (rc) printf("0x%02X = %s | ", I2C_adr, " - ");
else printf("0x%02X = %s | ", I2C_adr, "Fnd");
}
printf("\n");
for(cnt= 1; cnt <= 14*collumns+5; cnt++) printf("%s","=");
printf("\n");
} //break;
case 1 : {
printf("I2C active devices:\n");
device_cnt=0;
for(I2C_adr = 0; I2C_adr <= 127; I2C_adr++) {
Wire.beginTransmission(I2C_adr);
rc = Wire.endTransmission();
if(!rc) {
device_cnt++;
for (cnt=0; cnt<=(sizeof(I2Cdevice)/sizeof(I2Cdevice[0])-1); cnt++){
if(I2C_adr==I2Cdevice[cnt].I2C_adr) printf("0X%02X, ID= %10s, %s\n",I2Cdevice[cnt].I2C_adr, I2Cdevice[cnt].deviceID, I2Cdevice[cnt].type);
}
}
}
printf("Nr of found I2C devices : %u\n\n", device_cnt);
}
case 0:{
device_cnt=0;
for(I2C_adr = 0; I2C_adr <= 127; I2C_adr++) {
Wire.beginTransmission(I2C_adr);
rc = Wire.endTransmission();
if(!rc) device_cnt++;
}
}
}
return device_cnt;
}
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN, MASTER_ADR); // join i2c bus (address optional for master)
///Wire.setClock(400000L); // default 100Khz uncommend if 400Khz is needed
I2C_scan(2);
}
void loop() {
}
This is the printed output
