ESP32 - I2C Scanner using Arduino IDE (working)

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

ESP32 - I2C Scanner using Arduino IDE (working)

Postby jgustavoam » Sat Feb 17, 2018 3:17 pm

Picture of circuit ( ESP32 DEVKIT) :
https://www.flickr.com/photos/jgustavoa ... 409004158/

I did my first test with I2C Interface using Arduino IDE. And it works !
I2C scanner based on code of Nick Gammon http://www.gammon.com.au/forum/?id=10896
Very simple ! The I2C clock frequency is almost 100 KHz (97.96 KHz). I can't change this.

NOTE > PCF8574 - max frequency = 100 KHz (does not work with 400 KHz).
PCF8574 Datasheet = https://www.nxp.com/docs/en/data-sheet/ ... F8574A.pdf

Circuit Diagram - SDA = GPIO_21 and SCL = GPIO_22 .
Very Important = You MUST use Pullup Resistors of 3K3 ohms.

Without then it does not work ! I still do not know, how to configure (Arduino IDE) pullup internal resistors. Change jumpers A0,A1 and A2 to modify address. Default PCF8574 address is 0x20 ( Decimal 32).

Picture of circuit diagram:
https://www.flickr.com/photos/jgustavoa ... 409004158/

Decoding I2C Pulses with Logic Analyzer :
https://www.flickr.com/photos/jgustavoa ... 409004158/


I2C Scanner (Arduino IDE 1.8.5) :

Code: Select all

[code]
// ESP32 I2C Scanner
// Based on code of Nick Gammon  http://www.gammon.com.au/forum/?id=10896
// ESP32 DevKit - Arduino IDE 1.8.5
// Device tested PCF8574 - Use pullup resistors 3K3 ohms !
// PCF8574 Default Freq 100 KHz 

#include <Wire.h>

void setup()
{
  Serial.begin (115200);  
  Wire.begin (21, 22);   // sda= GPIO_21 /scl= GPIO_22
}

void Scanner ()
{
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;

  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);          // Begin I2C transmission Address (i)
    if (Wire.endTransmission () == 0)  // Receive 0 = success (ACK response) 
    {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);     // PCF8574 7 bit address
      Serial.println (")");
      count++;
    }
  }
  Serial.print ("Found ");      
  Serial.print (count, DEC);        // numbers of devices
  Serial.println (" device(s).");
}

void loop()
{
  Scanner ();
  delay (100);
}

[/code]

Console IDE (115200 Bps)

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28

I2C scanner. Scanning ...
Found address: 32 (0x20)
Found 1 device(s).

I2C scanner. Scanning ...
Found address: 32 (0x20)
Found 1 device(s).
Retired IBM Brasil
Electronic hobbyist since 1976.

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: ESP32 - I2C Scanner using Arduino IDE (working)

Postby jgustavoam » Sun Feb 09, 2020 1:13 pm

If you do not intend to use external pullup resistors for the I2C interface, you can configure internal pullups.

ESP32 Arduino driver/i2c.h
https://github.com/espressif/arduino-es ... iver/i2c.h

i2c_config_t Struct

gpio_pullup_t sda_pullup_en; /*!< Internal GPIO pull mode for I2C sda signal*/
gpio_pullup_t scl_pullup_en; /*!< Internal GPIO pull mode for I2C scl signal*/

/**
* @brief Configure GPIO signal for I2C sck and sda
*
* @param i2c_num I2C port number
* @param sda_io_num GPIO number for I2C sda signal
* @param scl_io_num GPIO number for I2C scl signal
* @param sda_pullup_en Whether to enable the internal pullup for sda pin
* @param scl_pullup_en Whether to enable the internal pullup for scl pin
* @param mode I2C mode
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2c_set_pin(i2c_port_t i2c_num, int sda_io_num, int scl_io_num,
gpio_pullup_t sda_pullup_en, gpio_pullup_t scl_pullup_en, i2c_mode_t mode);
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: No registered users and 13 guests