Hi all,
I'm trying to connect an ESP32 with an nRF52-DK (nRF52832) through I2C. I'm using the ESP32 I2C Slave example (https://www.arduino.cc/reference/en/libraries/esp32-i2c-slave/).
For the ESP32 I'm using a code based on the slave_sender.ino and the master_reader.ino for the nRF52-DK.
When I connect the ESP32 to the pins SCL(P.027) and SDA(P.026) of the nRF52-DK I don't receive anything and if (when connected) I reset the ESP32 I receive this error
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT) flash read err, 1000 ets_main.c 371
the rest of the time I receive "slave not found"
The code in ESP32 is:
#include <Arduino.h> #include <Wire.h> #include <WireSlave.h> #define I2C_SDA 12 #define I2C_SCL 13 #define I2C_SLAVE_ADDR 0x04 int table[] = {0, 0, 0}; void requestEvent(); void setup() { Serial.begin(115200); WireSlave.begin(I2C_SDA, I2C_SCL, I2C_SLAVE_ADDR); Serial.printf("Slave joined I2C bus with addr #%d\n", I2C_SLAVE_ADDR); } void loop() { int x = 120; int y = 1200; // gets converted to an strange # because is larger than 255 int z = 3; table[0] = x; table[1] = y; table[2] = z; WireSlave.onRequest(requestEvent); // register event } // function that runs whenever the master sends an empty packet. // this function is registered as an event, see setup(). // do not perform time-consuming tasks inside this function, // do them elsewhere and simply read the data you wish to // send inside here. void requestEvent() { uint8_t Buffer[3]; Buffer[0] = table[0]; Buffer[1] = table[1]; Buffer[2] = table[2]; WireSlave.write(Buffer, 3); }
In the nRF52-DK the related part of the code is:
#include <Arduino.h> #include "I2Cdev.h" #include "Wire.h" #include <WireCrc.h> #include <WireSlave.h> int table[] = {0, 0, 0, 0}; #define I2C_SLAVE_ADDR 0x04 #define SDA_PIN 12 #define SCL_PIN 13 #define MAX_SLAVE_RESPONSE_LENGTH 64 void setup(){ Serial.begin(115200); Wire.begin(); } void loop{ ESP32_Connect(); } void ESP32_Connect(){ static unsigned long lastReadMillis = 0; // request data from Slave every 1000 ms if (millis() - lastReadMillis > 1000) { // first create a WireSlaveRequest object // first argument is the Wire bus the slave is attached to (Wire or Wire1) WireSlaveRequest slaveReq(Wire, I2C_SLAVE_ADDR, MAX_SLAVE_RESPONSE_LENGTH); // optional: set delay in milliseconds between retry attempts. // the default value is 10 ms slaveReq.setRetryDelay(5); // attempts to read a packet from an ESP32 slave. // there's no need to specify how many bytes are requested, // since data is expected to be packed with WirePacker, // and thus can have any length. bool success = slaveReq.request(I2C_SLAVE_ADDR); if (success) { for (int i = 0; i < 4; i++) //organizes the data from the slave in the table { uint8_t c = slaveReq.read(); // receive a byte as character table[i] = c; } //displays the data Serial.print('\n'); Serial.print(table[0]); Serial.print('\t'); Serial.print(table[1]); Serial.print('\t'); Serial.print(table[2]); Serial.print('\t'); Serial.print(table[3]); Serial.print('\n'); delay (500); } else { // if something went wrong, print the reason Serial.println(slaveReq.lastStatusToString()); } lastReadMillis = millis(); } }