ESP32 I2C slave not working

ianmway
Posts: 1
Joined: Thu Feb 29, 2024 2:31 pm

ESP32 I2C slave not working

Postby ianmway » Thu Feb 29, 2024 2:42 pm

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.

Code: Select all

// 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

Code: Select all

// 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.

Code: Select all

// 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

Code: Select all

// 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);
}

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

Re: ESP32 I2C slave not working

Postby lbernstone » Thu Feb 29, 2024 7:50 pm

Your call to Wire.begin has the parameters in the wrong order.

Code: Select all

bool begin(uint8_t slaveAddr, int sda, int scl, uint32_t frequency);

Who is online

Users browsing this forum: No registered users and 70 guests