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