Arduino Master and Slave and LCD i2c problems

I have a problem and I hope someone can help me here.
I am currently working on an alarm system with 2 arduinos.
The MEGA2560 is used as a control panel and a NANO as a control unit with code or RFID tag.
I let both arduinos communicate with each other via i2c, which Is working fine.

Now I have connected an LCD display (also via i2c) to the control unit (NANO) and problems occur.

At startup the display information looks good when I enter a code, but as soon as I get all kinds of vague characters on the display.

If I accelerate the speed of refreshing on the display, then I get these characters faster too. When I do not or hardly ever change, it also takes longer.

To exclude mistakes of myself, I took the example sketch of the arduino (master_writer and slave_receiver) and at the slave_receiver I added an LCD display i2c.
The same problems also here.
Is this a known problem or is it not possible to connect it at the same time?

So now I am really stuck with this project and therefore I hope someone can help me further.
Many thanks!

// Wire Slave Receiver
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the “Wire Master Writer” example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <LiquidCrystal_I2C.h> //LCD display
#include <Wire.h>
int Test;
String Testing;

LiquidCrystal_I2C lcd(0x25, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
lcd.begin(16,2);
lcd.backlight();
lcd.clear();

Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop() {
delay(500);
lcd.setCursor(0, 0);
lcd.print(Testing + " " + Test);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
Testing=c;
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
Test=x;
}

Is this a known problem or is it not possible to connect it at the same time?

The connection is not the problem but the multimaster setup is. You have two masters on one I2C bus, this will not work reliably. Either connect the display to the master Arduino or change the communication between the two Arduinos to something else (UART comes to mind).

void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
    Testing=c;
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
  Test=x;
}

I2C receiveEvent is called in interrupt context. You must not call any method of the Serial object in interrupt context because it depends on interrupts.

Provide a link to the LiquidCrystal_I2C library you’re using, my version (the one installable by the IDE) has a 3 parameter constructor.

Tnx for your answer.

What do you mean by 2 masters?
I have a master writer and a slave receiver.
Is the LCD i2c behaving as a master too?

A master is controlling the bus. As you're controlling the I2C LCD by your "slave receiver", that "slave" must be bus master to send commands to the LCD.

Why do you want to have that split setup? Why don't you control the LCD by the "master" writer?

Why didn't you answer my other questions?

@technischebuurman

TWI Bus compatible LCD (I2C LCD) is always a ‘slave’; it can not become a Master as it does not contain required hardware to generate SCL (Serial Clock) pulses. The Arduino NANO may work as both Master and Slave as @pylon has stated provided that the required multi-master protocols are properly written.

According to my IDE’s I2C Library (#include <LiquidCrystal_I2C.h>), the wiring function for the 16x2 (2 lines and each line containing 16 characters) LCD is:

LiquidCrystal_I2C lcd(deviceAddress,16,2); //3 parameters as stated by @pylon in Post#1

The deviceAddress is a 7-bit value, and it is: 0b0100A2A1A0 (range: 0b0100000- 0b0100111)

The last three bits (A2A1A0) are the solder bridges on the IO Extender Board (Fig-1); one or more of the bridges can be shorted to get 0 or be kept open to get 1. Mine I2C LCD is having the deviceAddress ob0100111 = 0x27). Recheck that the deviceAddress of your I2C LCD is: 0x25 = 0b0100101.


Figure-1: 8574 IO extender based 16x2 LCD

Why is your I2C LCD’s wiring function like this?:
LiquidCrystal_I2C lcd(0x25, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

You may include this I2C LCD Library in your IDE after deleting the previous one provided that your LCD is fitted with 8574 based IO Extender as shown in Fig-1.

Listen to pylon's advice. These are your options to solve the problem.
If you need to have the LCD physically to be near the NANO (thus using the MEGA to control it is a problem) or any other reasons you might have, you need to change the communication between the two.
The mega has 3 uarts so you can even retain connection with a PC.

Given below a tentative scheme for a multi-master and multi-slave TWI Bus System from which the OP may collect some hints for the solution of his problem.

Working Principle
1. MEGA acquires temperature signal from a TWI Bus compatible Digital Temperature Sensor (BMP180) and writes the temperature value in decimal format on Top Line of the I2C LCD (U2) at 3-sec interval. (MEGA acts as a TWI Bus Master.) The routine works (system must be manually reset).

2. NANO acquires temperature signal from an Analog Temperature Sensor (LM35) and writes the temperature value in decimal format on Bottom Line of I2C LCD (U2) at 3-sec interval. (NANO acts as a TWI Bus Master.) The routine works (system must be manually reset).

3. MEGA asks the NANO (the TWI Slave) via TWI Bus Protocol to send the temperature value; MEGA receives it and then shows it on the port driven local LCD (U1). (MEGA acts as a TWI Bus Master and NANO acts as a TWI Bus Slave.) The routine is under development.

I'd Rather use the Nano with a software version of Wire to control the screen as i assume you need to have the Pin code or something display directly from the nano

If you use a software Library for I2C you'd have no problem controlling a device from a slave as the slave is the master to a complete separate bus on two different pins might be a bit slow but you and me will notice and no one else would