I2C communication btw esp8266 and arduino nano

I want to do I2C communication between esp8266 and arduino nano
but it not work
the connections i made
GND to GND
D1 or (GPIO 5) (esp8266) to A5 (Arduino nano)
D2 or (GPIO 4) (esp8266) to A4 (Arduino nano)
I connect both of them separately with my computer

it shows me nothing in the serial monitors

the arduino code is this:
'''
#include <Wire.h>

#define SDA_PIN A4 // SDA pin
#define SCL_PIN A5 // SCL pin

void setup() {
Wire.begin(8);
Serial.begin(9600);
}

void loop() {
Wire.requestFrom(8, 6); // Request 6 bytes from device with address 8

while (Wire.available()) {
char c = Wire.read();
Serial.println(c);
}
Serial.println(".");
delay(1000);
}
'''

also the esp8266 code is below:
'''
#include <Wire.h>

#define SDA_PIN 4 // D2
#define SCL_PIN 5 // D1
#define SLAVE_ADDRESS 8

void setup() {
Wire.begin(SDA_PIN, SCL_PIN);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop() {
delay(1000);
}

void requestEvent() {
String sendData = "Hello!";
Wire.write(sendData.c_str());
Serial.println("Sent: " + sendData);
}
'''

kindly help me to sort this I want to do I2C communications between them

can either be configured as a I2c endpoint?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Why is all of the code commented out ?

Have you taken into account that the Nano runs t 5V and the ESP8266 runs at 3.3V ?

I don't know how configure both for I2C as i am just uploading codes and make connections

Thanks for sharing now check the codes again
esp8266 code is:
<
#include <Wire.h>

#define SDA_PIN 4 // D2
#define SCL_PIN 5 // D1
#define SLAVE_ADDRESS 8

void setup() {
Wire.begin(SDA_PIN, SCL_PIN);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop() {
delay(1000);
}

void requestEvent() {
String sendData = "Hello!";
Wire.write(sendData.c_str());
Serial.println("Sent: " + sendData);
}
/>

Arduino nano code is:
<
#include <Wire.h>

#define SDA_PIN A4 // SDA pin
#define SCL_PIN A5 // SCL pin

void setup() {
Wire.begin(8);
Serial.begin(9600);
}

void loop() {
Wire.requestFrom(8, 6); // Request 6 bytes from device with address 8

while (Wire.available()) {
char c = Wire.read();
Serial.println(c);
}
Serial.println(".");
delay(1000);
}
/>
now check them

i2c is primarily intended to access multiple hardware devices using a shared set of connections, where a request is made for the data at a specific address in the device. there are other approaches to communicate between devices where information is no address (within a device) based

why not use the serial interfaces?

Is that a classic Arduino Nano with a ATmega328P microcontroller ?

Have you found the button <CODE/> ?
The sketch should be between three backslash single quotes.

```
your sketch
```

The SDA should be connected to SDA.
The SCL should be connected to SCL.
The GND should be connected to GND, and the GND is the most important.

The ESP8266 is a 3.3V processor with a 3.3V I2C bus.
The Arduino Nano is a 5V Arduino board with a 5V I2C bus.
That means you need a I2C level shifter.

There is something you can do to test the I2C bus.
Do you have other I2C devices such a sensor ? Can you find a I2C Scanner sketch ?
Run a I2C Scanner sketch on the ESP8266 and connect a sensor. The sketch should find it. Then make an empty sketch for the Nano with only Wire.begin(0x20); in setup(). Try if it can be found.

the connections are same as you mention
however i connect both of them seprately with my computer with their cables
also i didn't understand the sketch working which you ask me

Because my task is to perform I2C communication between them or use arduino nano and esp32-cam but with esp32-cam i am confused with it's connections so i choose esp8266 and arduino nano

is this an school or work assignment, if so why? certainly not a device constraint since other methods are possible, even more efficient.

is it your own choice? again see above

i think the problem is one of the devices must be configured (if possible) as a I2c slave.

Dear it is work assignment
I think there may be a problem with arduino nano
I'll try this with another arduino
but I checked simple blinking examples and simple write commands are working on this arduino

As previously asked

isn't i2c open-collector requiring external pull-ups?

The Wire.begin() does not look correct. Nowhere do you tell the ESP8266 what its I2C address is, and normally you would not specify an address for the master device.

Dear check these codes here i mention the slave address in both devices

Arduino nano code:
'''
#include <Wire.h>

void setup() {
Wire.begin(8);
Wire.onReceive(reader);
Wire.onRequest(request);
Serial.begin(9600);
}

void loop()
{
delay(10);
}

void reader(int how)
{
for(int i=0;i<how;i++)
{
char c = Wire.read();
Serial.print(c);
}
Serial.println(".");
}

void request()
{
Wire.write(9);
}
'''

ESP8266 code:
'''
#include <Wire.h>

void setup() {
Wire.begin(D1, D2);
Serial.begin(115200);
}

void loop() {
Wire.beginTransmission(8);
Wire.write("Hello");
Wire.endTransmission();
delay(1000);

Wire.requestFrom(8, 5); // takes 5 byte data from device address 8

while (Wire.available()) {
char a = Wire.read();
Serial.println(a);
}

char d = Wire.read();
Serial.println(d);

delay(1000);
}
'''

in these codes i use 8 slave device address
but with these codes it shows me nothing in their serial monitors

Dear check these codes here i mention the slave address in both devices

Arduino nano code:
'''
#include <Wire.h>

void setup() {
Wire.begin(8);
Wire.onReceive(reader);
Wire.onRequest(request);
Serial.begin(9600);
}

void loop()
{
delay(10);
}

void reader(int how)
{
for(int i=0;i<how;i++)
{
char c = Wire.read();
Serial.print(c);
}
Serial.println(".");
}

void request()
{
Wire.write(9);
}
'''

ESP8266 code:
'''
#include <Wire.h>

void setup() {
Wire.begin(D1, D2);
Serial.begin(115200);
}

void loop() {
Wire.beginTransmission(8);
Wire.write("Hello");
Wire.endTransmission();
delay(1000);

Wire.requestFrom(8, 5); // takes 5 byte data from device address 8

while (Wire.available()) {
char a = Wire.read();
Serial.println(a);
}

char d = Wire.read();
Serial.println(d);

delay(1000);
}
'''

in these codes i use 8 slave device address
but with these codes it shows me nothing in their serial monitors
here i use Wire.begin with slave address and in esp8266 with it's SDA and SCL pins configuration

Don't know dear

@ahmed431644

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

@ahmed431644

stop posting code as pure text !

read this manual how to post code as a code-section

With googling I found this issue:

at the bottom of this issue-thread there are two demo-codes that use a reduced clock-speed.

I have seen other information that I2C does not work between a ESP8266-SLAVE and other microcontrollers.

It does only work between two ESP8266
Again: you seem to ignore this question:

[/quote]

[/quote]

best regards Stefan