idf i2c scanner could not find device(while arduino ide can)
Posted: Sat Mar 16, 2019 9:44 am
I used to program in arduino ide, and i2c works fluently.
As I decided to move on to IDF, first thing I tried is i2c_scanner program from
https://github.com/nkolban/esp32-snippe ... cscanner.c
In IDF, no i2c device could be found. Actually, there was once or twice, the device was found, but most of time after reboot, no device no luck.
How can I get i2c scanner work in IDF? Any help would be very welcomed
Code: Select all
Wire.begin(22,21,400000);
Wire.setClock(400000);
Code: Select all
Scanning I2C Addresses
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. 4B .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
Scan Completed, 1 I2C Devices found.
As I decided to move on to IDF, first thing I tried is i2c_scanner program from
https://github.com/nkolban/esp32-snippe ... cscanner.c
In IDF, no i2c device could be found. Actually, there was once or twice, the device was found, but most of time after reboot, no device no luck.
Code: Select all
static char tag[] = "i2cscanner";
ESP_LOGD(tag, ">> i2cScanner");
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = 22;
conf.scl_io_num = 21;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 400000;
i2c_param_config(I2C_NUM_0, &conf);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
int i;
esp_err_t espRc;
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
printf("00: ");
for (i=3; i< 0x78; i++) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
if (i%16 == 0) {
printf("\n%.2x:", i);
}
if (espRc == 0) {
printf(" %.2x", i);
} else {
printf(" --");
}
//ESP_LOGD(tag, "i=%d, rc=%d (0x%x)", i, espRc, espRc);
i2c_cmd_link_delete(cmd);
}
printf("\n");
vTaskDelete(NULL);
Code: Select all
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --