ESP32 I2C slave not working

Hello,

Quick summary of issue (code below).

I have an esp32-2432s028 (AKA Cheap Yellow Display (CYD)), I'm trying to setup I2C communication between the CYD & an Arduino Nano.

I've got Nano master to Nano slave working as a test.

I've got CYD master to Nano slave working but I can't get Nano master to CYD slave to work. Just to make things a little more complicated the standard SDA pin 21 on the CYD is unavailable as it's used for the backlight control so the SDA pin needs to be redefined as one of the few available pins (27).

I'm using the latest Wire.h library and the ESP32-WROOM-DA Module board in the Arduino IDE 1.8.16. The code complies & uploads fine but the CYD I2C slave doesn't work. I scanned for an I2C address using Nano scanning code which doesn't find an I2C address for the CYD with the slave I2C code loaded. I've tried the Wire.begin switches in various orders without any success.
Any help would be most appreciated as I been at this for a couple of days now, trawling the Internet for any solutions.

I've double checked all the connections (the CYD master & slave connections are the same).

The esp32-2432s028 slave code that doesn't work.


// I2C Slave - esp32-2432s028

// Include the required Wire library for I2C
#include <Wire.h>

int LED = 17;
int x = 0;

#define SDA_PIN 27
#define SCL_PIN 22
#define I2C_ADDRESS 9

void setup() {
  Serial.begin(115200);
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus with SDA_PIN, SCL_PIN Slave on address I2C_ADDRESS
  Wire.begin(SDA_PIN, SCL_PIN, I2C_ADDRESS);

  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

void loop() {
  digitalWrite(LED, HIGH);
  Serial.println (x);

  //If value received is 0 blink LED for 200 ms
  if (x == 5) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
}

The esp32-2432s028 Master code that does work

// I2C esp32-2432s028 Master


// Include the required Wire library for I2C
#include <Wire.h>


int x = 0;


#define SDA_PIN 27
#define SCL_PIN 22


void setup() {
    Serial.begin(115200);
  // Start the I2C Bus as Master
  Wire.begin(SDA_PIN, SCL_PIN);
  
}

void loop() {
     Serial.println (x);

  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x 
  Wire.endTransmission();    // stop transmitting
 
  x++; // Increment x
  if (x > 5) x = 0; // `reset x once it gets 6


  delay(2000);
}

The Nano slave code that works


// I2C Arduino Nano Slave


// Include the required Wire library for I2C
#include <Wire.h>

int LED = 4;
int x = 0;


void setup() {
  Serial.begin(115200);
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9);
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

void loop() {

  Serial.println (x);
  //If value received is 0 blink LED for 200 ms
  if (x == 5) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }

}

The Nano master code that works

// I2C Arduino Nano Master


// Include the required Wire library for I2C
#include <Wire.h>

int x = 0;

void setup() {
  Serial.begin(115200);
  // Start the I2C Bus as Master
  Wire.begin();
}

void loop() {
  Serial.println (x);
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x
  Wire.endTransmission();    // stop transmitting

  x++; // Increment x
  if (x > 5) x = 0; // `reset x once it gets 6

  delay(1000);
}

Hello ianmway, what is the micro in your CYD, ESP32 or ESP32-XX?

I have been able to communicate via I2C from a Master TTGO T7 ESP32-Wromer to and Slave Elecrow 3.5 in. ILI9488 TFT Display with a ESP32-Wromer micro.

I would like to be able to do the same but with the newer TFT display equipped with newer ESP32-S3 micro I purchased from Makerfabs so I can utilize the larger screens.

One more thought, both the Master TTGO T7 ESP32-Wromer and Slave Elecrow 3.5 in. ILI9488 TFT Display with a ESP32-Wromer micro have the same I2C Dev ID, Hex = 0x55 / Dec = 85!!!! Could this be the reason I can get the Wromer micros to communicate????

Need to get more info about the I2C Dev ID from the manufacture regarding the ESP32-S3 I2C micro's Dev ID and the correct SDA & SCL GPIO pin numbers.

Hello,

The controller on my ESP-32-2432S028 is ESP32 not ESP32-XX, the board I use in the Arduino IDE (V2.3.2) is ESP32-WROOM-DA MODULE, from the esp32 by Espressif Systems V2.0.14 package (the updated version didn't work for me).

You can define the I2C pins to a certain extent on the ESP-32 (ESP_32-2432S028).

The ESP32 chips have a GPIO matrix that allows you to route most peripherals (like I2C) to any available GPIO pin. In other words: pick two convenient GPIO pins, make sure they're not otherwise occupied (flash connected to them, bootstrap pins etc), and you can use them as your I2C pins - Pins for i2C on ESP32-S3-DevKitC-1? - ESP32 Forum .

The SDA pin I'm using is 27. #define SDA_PIN 27
The SCL pin I'm using is 22. #define SCL_PIN 22

An I2C Master does "not" have an address, only slaves have addresses.
All of the transactions on an I2C bus are pushed from or pulled to the master under the master's control.

I use the ESP-32-2432S028 as the Master and an Arduino Nano as the Salve, setting the Nano slaves address to 0x08.

For the CYD there is this github page with loads of useful information,

Hello ianmway, I have my setup passing two pairs of GPS coordinates from the Master TTGO T7 to the Elecrow ILI9488 TFT Display with the same micro as the T7 (ESP32-Wromer) which made the whole process simple.

Where did your purchase your CYD and where you satisfied? I ask, as I would like to communicate with someone from their organization as I would like to purchase a larger screen display that is based on the ESP32 micro. Is there a contact for the manufacture directly to discuss such info, or was your purchase thru a third party?

Have you come accross any info as to an ESP32 micro communicating via I2C with a TFT display equipped with a ESP32-XX micro?

I purchased I the ESP-32-2432S028 from AliExpress, Elecrow make a 7.0" version (7.0 inch -CrowPanel ESP32 display).

DWIN make touch screens ( COB UART LCD Module Manufacturers - China COB UART LCD Module Factory & Suppliers ), they are programmed in a different way using the DGUS software, they send & receive variables via Serial, this might be an option (https://www.youtube.com/watch?v=w5iFwVktWjQ&t=307s).

Below is my current code that I created using Squareline Studio for the ESP-32-2432S028, then editing as required. It uses 4 potentiometers for the inputs from the Nano to test the ESP-32-2432S028 code. It's not perfect coding but it works :slight_smile:

Current Code.zip (585.8 KB)

I bet it is working. Try this:
volatile int x = 0;

I'm curious to see how far you get with this because the ESP8266 could act as an I2C slave only if the master was also an ESP8266. See: ESP8266 I2C doesn't respond - Arduino Stack Exchange . Maybe it is now better with the ESP32.

If a variable is changed in an interrupt, and you want to use it in your loop, you must declare the variable as volatile or the value in the loop will not change.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.