ESP32-P4 + i2c not work

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 1:20 pm

Hi,
Waveshare ESP32-P1 dev kit basic and DSI 10.1"
IDE 2.3.7 or PIOARDUINO.
Core 3.3.5

i2c not work with ESP32-P4 on connector jst sh 1.0 from board.
but it work when i use without DSI 10.1"

i2c is connected to PN532.
i use pin 7 and 8.

I have tested other pin and is a same.
May be is conflict with LCD that use i2c when it init display?

Thanks in advance.
Attachments
Capture10.JPG
Capture10.JPG (139.23 KiB) Viewed 1895 times

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 1:29 pm

i think is conflict with initialization from display and nfc.begin()

Code: Select all

...............
DEV_I2C_Port port = DEV_I2C_Init();
  display_init(port);
  set_display_backlight(port, 127);  
 
  // Init Display
  if (!gfx->begin()) {
    term_write("gfx->begin() failed!");
  }
  ..............
if i put nfc.begin() before init display, nothing appear on the screen.
if i put nfc.begin() after init display, screen work, but PN532 fail.

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 1:38 pm

According to other tests I've done, the problem comes from the fact that I'm calling i2c twice, once for screen initialization and the other for the PN532, this causes a problem, which means that I wouldn't be able to use sensors for example in i2c.

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-P4 + i2c not work

Postby lbernstone » Sat Dec 27, 2025 7:13 pm

I2C is a bus. So, you can either have both devices on the same bus, using the same pins (they will get unique addresses), or you can have 2 busses with a device on each. For your case, I would recommend using a single bus, since neither device will be very intensive, and that saves you a couple pins.
How this is implemented depends on the library you are using (which you didn't include in your post). Typically, there will be a method of initializing that allows you to tell the library which i2c object to use, in which case it will assume you have called i2cInit previously in your code. Some libraries are poorly written and just assume it will use TwoWire(0) or even just hard coded pins.
https://wokwi.com/projects/451520519561237505

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 10:34 pm

I'm using Adafruit_PN532, which in turn uses Adafruit_I2CDevice and calls Wire.h; TwoWire is used.

As I said, I2C on this board works perfectly, but as soon as I connect the 10.1" DSI monitor, it stops working. There must be something to do with the monitor.

Code: Select all

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // Pin 7,8


void setup(void) {
  // has to be fast to dump the entire memory contents!
  Serial.begin(115200);
  
  term_init();  // Display initialization
  nfc.begin();   // NFC PN532 initialization from Adafruit library
  
  term_write("Looking for PN532...\r\n");  
  
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    term_write("Didn't find PN53x board\r\n");
    while (1); // halt
  }
Last edited by andromeda92 on Sat Dec 27, 2025 10:49 pm, edited 1 time in total.

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-P4 + i2c not work

Postby lbernstone » Sat Dec 27, 2025 10:46 pm

Please post your whole code if you want help. I am not a mind reader.

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 10:55 pm

NFC PN532 is image below
putting switch to 1 and 0 is i2c, putting to 0 0 is for HSU, just for information

Code :

Code: Select all

/**************************************************************************/
/*!
    @file     mifareclassic_memdump.pde
    @author   Adafruit Industries
  @license  BSD (see license.txt)

    This example attempts to dump the contents of a Mifare Classic 1K card

    Note that you need the baud rate to be 115200 because we need to print
  out the data and read from the card at the same time!

    This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
    This library works with the Adafruit NFC breakout
      ----> https://www.adafruit.com/products/364

    Check out the links above for our tutorials and wiring diagrams
    These chips use SPI or I2C to communicate

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!

*/
/**************************************************************************/


#include <Adafruit_PN532.h>
#include "term_func.h"


// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (7)   // For esp32-p4 7,8
#define PN532_RESET (8)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


//static const int RXPin = 36, TXPin = 2;

//HardwareSerial ss(2);
// Or use hardware Serial:
//Adafruit_PN532 nfc(PN532_RESET, &ss);


void setup(void) {
  // has to be fast to dump the entire memory contents!
  Serial.begin(115200);
  
  //ss.begin(115200, SERIAL_8N1, RXPin, TXPin);
  term_init();    
  nfc.begin();
  
  term_write("Looking for PN532...\r\n");  
  
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    term_write("Didn't find PN53x board\r\n");
    while (1); // halt
  }
  // Got ok data, print it out!
  term_write("Found chip PN5 "); term_printf("%d", (versiondata>>24) & 0xFF);
  term_write("Firmware ver. "); term_printf("%d", (versiondata>>16) & 0xFF);
  term_write("."); term_printf("%d\r\n", (versiondata>>8) & 0xFF);

  nfc.SAMConfig();
  term_println("Waiting for an ISO14443A Card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    // Display some basic information about the card
    term_println("Found an ISO14443A card");
    term_write("  UID Length: ");term_printf("%d", uidLength);term_println(" bytes");
    term_write("  UID Value: ");
    term_PrintHex(uid, uidLength);
    term_println("");

    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ...
      term_println("Seems to be a Mifare Classic card (4 byte UID)");

      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      term_println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

	  // Start with block 4 (the first block of sector 1) since sector 0
	  // contains the manufacturer data and it's probably better just
	  // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);

      if (success)
      {
        term_println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];

        // If you want to write something to block 4 to test with, uncomment
		// the following line and this text should be read back in a minute
        //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);
        // success = nfc.mifareclassic_WriteDataBlock (4, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);

        if (success)
        {
          // Data seems to have been read ... spit it out
          term_println("Reading Block 4:");
          term_PrintHexChar(data, 16);
          term_println("");

          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          term_println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        term_println("Ooops ... authentication failed: Try another key?");
      }
    }

    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      term_println("Seems to be a Mifare Ultralight tag (7 byte UID)");

      // Try to read the first general-purpose user page (#4)
      term_println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        term_PrintHexChar(data, 4);
        term_println("");

        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        term_println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}

Code for init display: on term_init:

Code: Select all

#include "term_func.h"

#include "displays_config.h"


Arduino_ESP32DSIPanel *dsipanel = new Arduino_ESP32DSIPanel(
  display_cfg.hsync_pulse_width,
  display_cfg.hsync_back_porch,
  display_cfg.hsync_front_porch,
  display_cfg.vsync_pulse_width,
  display_cfg.vsync_back_porch,
  display_cfg.vsync_front_porch,
  display_cfg.prefer_speed,
  display_cfg.lane_bit_rate);

Arduino_DSI_Display *gfx = new Arduino_DSI_Display(
  display_cfg.width,
  display_cfg.height,
  dsipanel,
  0,
  true,
  -1,
  display_cfg.init_cmds,
  display_cfg.init_cmds_size);

void term_init()
{
  
  DEV_I2C_Port port = DEV_I2C_Init();
  display_init(port);
  set_display_backlight(port, 127);  
 
  // Init Display
  if (!gfx->begin()) {
    term_write("gfx->begin() failed!");
  }

  gfx->fillScreen(RGB565_BLACK);
  gfx->setCursor(0, 1);
  // Rotation to 90°
  gfx->setRotation(1);
  gfx->setFont(&CONSOLE_FONT); 
  gfx->setTextSize(1);
}
Attachments
71ZkJi3kdJL._AC_SL1500_.jpg
71ZkJi3kdJL._AC_SL1500_.jpg (133.61 KiB) Viewed 1878 times

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sat Dec 27, 2025 11:26 pm

i use this board:
Attachments
Capture.JPG
Capture.JPG (61.02 KiB) Viewed 1875 times

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-P4 + i2c not work

Postby lbernstone » Sun Dec 28, 2025 6:21 am

Ah, I misunderstood, and thought you were trying to use two i2c devices. The DSI does indeed use the I2C marked pins, and does not appear to share. I would recommend you use gpio 32 & 33, which correspond to the pins marked as I3C on the grove connector. Define a new bus on i2c(1) and then call the nfc library against that bus.

Code: Select all

#define NFC_I2C_SDA 33
#define NFC_I2C_SCL 32
TwoWire wire1(1);
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET, &wire1);
...
setup() (
wire1.begin(NFC_I2C_SDA, NFC_I2C_SCL);
nfc.begin();

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: ESP32-P4 + i2c not work

Postby andromeda92 » Sun Dec 28, 2025 7:40 am

Thank you so much again, I didn't know you could switch buses using Wire.
Thanks again.
I kept the same pins and it works

Code: Select all

#define PN532_IRQ   (7)   // For esp32-p4 7,8
#define PN532_RESET (8)  // Not connected by default on the NFC Shield

..........
TwoWire WirePN532(1);
// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET, &WirePN532);

......
void setup(void) {
  // has to be fast to dump the entire memory contents!
  Serial.begin(115200);
  
  term_init();    
  WirePN532.begin(PN532_IRQ, PN532_RESET);
  nfc.begin();
  
  term_write("Looking for PN532...\r\n");  
  .......

Who is online

Users browsing this forum: Google [Bot] and 6 guests