I2C communication not Work

Hi guys, I'm working on a project that uses I2C communication to send data between ATmega328 and ESP32U. i am using 4.7k pullup and 3.3V vdd, and i tried simple code but it doesn't work. no data received at all.

below is sender code using atmega328

#include <Wire.h>
#include <ArduinoJson.h>

#define ESP32_ADDRESS 20  // Alamat I2C ESP32
#define I2C_CLOCK_SPEED 50000  // Kecepatan clock I2C dalam Hz

void setup() {
  Serial.begin(115200);
  Wire.begin();  // Inisialisasi I2C
  Wire.setClock(I2C_CLOCK_SPEED);
}

void loop() {
  // Membuat objek JSON
  StaticJsonDocument<32> jsonDocument;
  jsonDocument["sensor"] = "temperature";
  jsonDocument["value"] = random(20, 30);

  // Mengubah objek JSON menjadi string
  String jsonString;
  serializeJson(jsonDocument, jsonString);

  // Mengirim data JSON ke ESP32
  Wire.beginTransmission(ESP32_ADDRESS);
  Wire.write(jsonString.c_str());
  Wire.endTransmission();

  delay(10);  // Mengirim data setiap 5 detik
}

and this receive code

#include <Wire.h>
#include <ArduinoJson.h>

#define I2C_SLAVE_ADDRESS 20  // Alamat I2C slave yang digunakan
#define I2C_CLOCK_SPEED 50000  // Kecepatan clock I2C dalam Hz

void setup() {
  Serial.begin(115200);
  Wire.begin(I2C_SLAVE_ADDRESS);  // Inisialisasi mode I2C sebagai slave
  Wire.setClock(I2C_CLOCK_SPEED);
  Wire.onReceive(receiveEvent);
  Serial.println ("Serial OK");
}

void loop() {
  // Kode di sini untuk loop utama program, jika diperlukan
}

void receiveEvent(int byteCount) {
  String receivedData = "";
  while (Wire.available()) {
    char c = Wire.read();
    receivedData += c;
  }

  // Parsing data JSON
  StaticJsonDocument<32> jsonDocument;
  DeserializationError error = deserializeJson(jsonDocument, receivedData);
  if (error) {
    Serial.print("Failed to parse JSON: ");
    Serial.println(error.c_str());
    Serial.println (receivedData);
    return;
  }

  // Mendapatkan nilai dari JSON
  const char *sensor = jsonDocument["sensor"];
  int value = jsonDocument["value"];

  // Menampilkan data yang diterima
  Serial.print("Received JSON - Sensor: ");
  Serial.print(sensor);
  Serial.print(", Value: ");
  Serial.println(value);
}

this is my schematic for communication betwen two micros

Your schematic shows 3.3K pullup and a 328PB NOT a 328.
Which are correct?
Also clock of 50000 may not work try 100000

Stable operation of the 328PB is not guaranteed at 3.3V with a 16MHz clock

Print out the length of jsonString, I'm getting 35 bytes here, the I2C buffer is limited to 32 bytes.

It would be easier to just send the number, then generate the json on the ESP8266.

Almost everyone gets into a lot of trouble when using the I2C bus between Arduino boards.
Can you use a Serial port ?
Running JSON on a Arduino Uno is not preferred. It might use too much memory.
The I2C bus is for small packages of binary data. It is not meant to be used to send variable length text.
Please do not use the String object in a interrupt routine. The receiveEvent() runs in a interrupt, and the String object uses the heap.
Some say that it is better not to use the String object on a Arduino Uno at all.

The Arduino Uno has a ATmega328P microcontroller, it can blink leds and read buttons. The I2C bus can read a few bytes from a sensor, it is not a communication bus.

I have seen similar projects. Can you do your project with just the ESP32 ? You can add extra chips for more Input/Output pins. That is a lot easier.

If you really need the Arduino Uno, can you tell why you need it ?
The ESP32 has a spare UART port: Serial2
You can use that to communicate with other boards, if you have to.

Hello
the Atmega 328 is with 5V logik level. The ESP32 3.3V. So you can not exchange signals 1 by 1 from a 5V to a 3.3V System. The logik voltage will not fit.
See:
https://forum.arduino.cc/t/when-to-use-level-shifter/620394

Best regards Mascho11

I would like to suggest to use Arduino UNO Board-Level Shifters-ESP32 Board to check that the sketches work and then move to PCB-based controllers.

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