Comunication between two arduino board

</>hello everyone, I'm working on a project where I have 2 arduino uno, i2c, 16x2 lcd, acs712, dht11. One arduino is master and the other slave, to arduino master I connected acs712 and dht11. To arduino slave I connected i2c and lcd.Arduino the master reads the data from the 2 sensors and transmits them to the slave, it transmits them via i2c to the lcd and displays them on it. My problem is that I cannot display the parameters read by the sensors on the lcd. The Arduino master and the slave are connected to each other thus, pins A5 to A5 and A4 to A4, GND to GND. Below I send you the master and slave codes and what they display on the serial monitor. Any justice is welcome, thank you.Code master:/ /#include <Wire.h>
#include <DHT.h>

#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int ACS712_PIN = A0;
const int slaveAddress = 8; // Adresă nouă pentru slave

void setup() {
Serial.begin(9600);
Serial.println("Initialized Serial");

dht.begin();
Wire.begin();
Serial.println("Initialized DHT and Wire");
}

void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
float current = analogRead(ACS712_PIN) * (5.0 / 1023.0);

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Current: ");
Serial.print(current);
Serial.println(" A");

Wire.beginTransmission(slaveAddress); // Adresa slave-ului este 9
Wire.write((byte*)&temperature, sizeof(float)); // Trimite temperatura
Wire.write((byte*)&humidity, sizeof(float)); // Trimite umiditatea
Wire.write((byte*)&current, sizeof(float)); // Trimite curentul
Wire.endTransmission();
Serial.println("Data sent to slave");

delay(1000);
}/ /
Serial monitor:nitialized Serial
Initialized DHT and Wire
Temperature: 25.00 C, Humidity: 58.00 %, Current: 2.47 A Cod slave:/ /#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
Serial.begin(9600);
Serial.println("Initialized Serial");

lcd.begin(16, 2);
lcd.backlight();
Serial.println("Initialized LCD");

Wire.begin(8); // Adresa acestui dispozitiv slave este 8
Wire.onReceive(receiveEvent);
Serial.println("Initialized I2C");
}

void loop() {
// Nu este nevoie de cod aici, deoarece totul este gestionat în funcția de recepție
}

void receiveEvent(int howMany) {
Serial.print("Receiving data... (howMany = ");
Serial.print(howMany);
Serial.println(")");

if (howMany >= 12) { // Verificăm dacă sunt suficiente bytes disponibile
float temperature = 0, humidity = 0, current = 0;

// Citim temperatura
delay(10); // Adăugăm o mică întârziere pentru sincronizare
int availableBytes = Wire.available();
Serial.print("Available bytes before reading temperature: ");
Serial.println(availableBytes);

if (availableBytes >= sizeof(float)) {
  Serial.println("Reading temperature...");
  for (int i = 0; i < sizeof(float); i++) {
    ((char*)&temperature)[i] = Wire.read();
  }
  Serial.print("Temperature received: ");
  Serial.println(temperature);
} else {
  Serial.println("Failed to read temperature");
  return; // Dacă nu putem citi temperatura, nu continuăm
}

// Citim umiditatea
delay(10); // Adăugăm o mică întârziere pentru sincronizare
availableBytes = Wire.available();
Serial.print("Available bytes before reading humidity: ");
Serial.println(availableBytes);

if (availableBytes >= sizeof(float)) {
  Serial.println("Reading humidity...");
  for (int i = 0; i < sizeof(float); i++) {
    ((char*)&humidity)[i] = Wire.read();
  }
  Serial.print("Humidity received: ");
  Serial.println(humidity);
} else {
  Serial.println("Failed to read humidity");
  return; // Dacă nu putem citi umiditatea, nu continuăm
}

// Citim curentul
delay(10); // Adăugăm o mică întârziere pentru sincronizare
availableBytes = Wire.available();
Serial.print("Available bytes before reading current: ");
Serial.println(availableBytes);

if (availableBytes >= sizeof(float)) {
  Serial.println("Reading current...");
  for (int i = 0; i < sizeof(float); i++) {
    ((char*)&current)[i] = Wire.read();
  }
  Serial.print("Current received: ");
  Serial.println(current);
} else {
  Serial.println("Failed to read current");
  return; // Dacă nu putem citi curentul, nu continuăm
}

// Afișăm datele pe LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");

lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");

lcd.setCursor(8, 1);
lcd.print("I: ");
lcd.print(current);
lcd.print("A");

Serial.println("Data displayed successfully!");

} else {
Serial.print("Not enough data received, only ");
Serial.print(howMany);
Serial.println(" bytes available");
}
}/ /
Serial monitor:Receiving data... (howMany = 12)
Available bytes before reading temperature: 12
Reading temperature...
Temperature received: 25.00
Available bytes before reading humidity: 8
Reading humidity...
Humidity received: 62.00
Available bytes before r

Please edit your post to add code tags.

Instructions can be found in the "How to get the best out of this forum" post.

1 Like

Follow @jremington suggestion, the way it appears on my system is just plain gibberish.

I spotted the error already. Use 1 Uno. Problem solved.

I can't do this because in my project I have to highlight the communication between 2 Arduino via I2C and SPI

It's not true, you don't have to do that.

What happens if you do not do it? You get no marks? Do marks have any value if awarded by someone who insists that you have to do something dumb to get the marks?

Anyway, an Arduino can't be both i2c master and slave at the same time. So the arduino connected to the LCD must be the master.

I am working on a project to highlight the communication between 2 arduino using i2C and SPI interfaces. The two arduino are connected to each other by pins A4,A5 and Gnd.La Arduino master I have linked 2 sensors ,an acs712 and a dht11. To arduino slave I connected i2c and lcd 16x2. I had thought of the project as follows, the master to take the data read by the 2 sensors, send them to the slave, send them through i2c to the lcd and display them. I also made connections for spi between the 2 arduins, pins 13, 12.....10 being connected to each other. Does such a project have a chance of realization?

You already had a topic on the same project. Please do not cross-post, it wastes people's time.

I've merged your two topics.

Not with your level of knowledge.
In general, your entire project is a complete mess. Why connect two arduinos if one can do everything? Moreover, if you still want to use two, why connect them simultaneously via both I2C and SP - this is complete nonsense.

And another thing - you posted the code in a message without tags, which makes it very difficult to read. When you were asked to correct it, you ignored it. This is disrespectful to the participants. If you want someone to help you, do it the right way.

How do I post code in a message with tags?

see the post #2

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