# I2C communication btw esp8266 and arduino nano

**URL:** <https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048>\
**Category:** Programming\
**Created:** [October 31, 2023, 10:11am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048 "2023-10-31T10:11:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 10:11am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/1 "2023-10-31T10:11:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 31, 2023, 10:16am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/2 "2023-10-31T10:16:07Z")

</div>

can either be configured as a I2c endpoint?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 31, 2023, 10:17am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/3 "2023-10-31T10:17:05Z")

</div>

> [@ahmed431644](#):
>
> the arduino code is this:

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)&nbsp;to make it easier to read and copy for examination   
  
[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum](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 ?

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 10:36am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/4 "2023-10-31T10:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 10:40am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/5 "2023-10-31T10:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 31, 2023, 11:04am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/6 "2023-10-31T11:04:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [October 31, 2023, 11:05am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/7 "2023-10-31T11:05:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:19am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/8 "2023-10-31T11:19:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:24am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/9 "2023-10-31T11:24:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 31, 2023, 11:31am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/10 "2023-10-31T11:31:18Z")

</div>

> [@ahmed431644](#):
>
> Because my task

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.

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:38am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/11 "2023-10-31T11:38:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 31, 2023, 11:40am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/12 "2023-10-31T11:40:19Z")

</div>

As previously asked

> [@UKHeliBob](#):
>
> Have you taken into account that the Nano runs at 5V and the ESP8266 runs at 3.3V ?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 31, 2023, 11:43am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/13 "2023-10-31T11:43:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [October 31, 2023, 11:45am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/14 "2023-10-31T11:45:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:50am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/15 "2023-10-31T11:50:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:53am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/16 "2023-10-31T11:53:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmed431644](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@ahmed431644](https://forum.arduino.cc/u/ahmed431644)\
**Post date:** [October 31, 2023, 11:53am UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/17 "2023-10-31T11:53:30Z")

</div>

Don't know dear

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [October 31, 2023, 12:16pm UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/18 "2023-10-31T12:16:40Z")

</div>

@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

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

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

Thank you.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 31, 2023, 12:18pm UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/19 "2023-10-31T12:18:13Z")

</div>

@ahmed431644

## stop posting code as pure text !

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

> [@Make experienced users help: two ways to quickly post code as a code-section](https://forum.arduino.cc/t/make-experienced-users-help-two-ways-to-quickly-post-code-as-a-code-section/937298/):
>
> Hi newcomer, This thread wants to show you how you can speed-up getting helpful answers with small effort. If you manage at least to post this way. You have the sympathy of the experienced users. If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project. always post your full sketch. As a code-section from the very first to the very last line A code-section has a small height =\> keeps your posting "short" but e…

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 31, 2023, 12:40pm UTC](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048/20 "2023-10-31T12:40:13Z")

</div>

With googling I found this issue:

> <https://github.com/esp8266/Arduino/issues/5762>
>
> \### Basic Infos
> 
> \- \[x\] This issue complies with the \[issue POLICY doc\](https:/…/github.com/esp8266/Arduino/blob/master/POLICY.md).
> \- \[x\] I have read the documentation at \[readthedocs\](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
> \- \[x\] I have tested that the issue is present in current master branch (aka latest git).
> \- \[x\] I have searched the issue tracker for a similar issue.
> \- \[x\] If there is a stack dump, I have decoded it.
> \- \[x\] I have filled out all fields below.
> 
> \#### Platform
> 
> \- Hardware: \[ESP-12F\] XinaBox CW01
> \- Core Version: \[2.5.0\] Latest board file in Arduino
> \- Development Env: \[Arduino IDE 1.8.8\]
> \- Operating System: \[MacOS ver 12.0.3\]
> 
> \### Settings in IDE
> 
> \- Module: \[Generic ESP8266 Module\]
> \- Flash Mode: \[DOUT\]
> \- Flash Size: \[4MB\]
> \- lwip Variant: \[v2 Lower Memory\]
> \- Reset Method: \[ck\]
> \- Flash Frequency:	\[26Mhz\]
> \- CPU Frequency: \[80Mhz\]
> \- Upload Using: \[SERIAL\]
> \- Upload Speed: \[115200\] (serial upload only)
> 
> 
> \### Problem Description
> 
> While ESP8266(I2C Master) to ESP8266(I2C Slave) works, with a pinch†, I cannot get ESP8266 to work as an I2C Slave towards Raspberry Pi, Micro:Bit and others.
> 
> The sketch below works perfectly as an ESP8266 I2C slave used with a ESP8266 I2C Master, but not otherwise.
> 
> I can detect‡, the slave on a RPi using \`i2cdetect -y 1\`, but can't communicate with the slave. I have tried frequency change, clock stretching in Arduino, 3 different libraries on the RPi, and I have tried in MicroPython on a Micro:Bit to no avail.
> 
> † In order to have the ESP8266 Master to work, I have to call the \`Wire.begin()\` with an address, like a Master address, that is not standard, but otherwise it won't work for me.
> ‡ \`i2cdetect -y 1\` only works 80% of the time, and while seemingly high, it is not 100% of the time like with all other I2C slaves.
> 
> \### \[MCVE\](https://stackoverflow.com/help/mcve) Sketch
> 
> \`\`\`cpp
> \#include \<Wire.h\>
> void requestEvent() {
> Wire.write("At your service!");
> }
> void setup() {
> Wire.begin(2,14,0x08);           
> Wire.onRequest(requestEvent);
> }
> void loop() {
> }
> \`\`\`
> 
> \### Debug Messages
> 
> \`\`\`
> Debug messages go here
> \`\`\`
> 
> More attempts: Just to make sure I haven't lost it, I tried with another WiFi board, the Arduino MKR1000, and the above code (except for the pin allocations), and it worked perfectly. 
> 
> Here is the RPi code, btw:
> \`\`\`python
> import pigpio
> pi = pigpio.pi()
> handle = pi.i2c\_open(1,8,0)
> (count, data) = pi.i2c\_read\_device(handle, 16)
> print(data)
> pi.i2c\_close(handle)
> pi.stop()
> \`\`\`

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:

> [@UKHeliBob](#):
>
> As previously asked
> 
> > [@UKHeliBob](#):
> >
> > Have you taken into account that the Nano runs at 5V and the ESP8266 runs at 3.3V ?

> [@UKHeliBob](#):
>
> Have you taken into account that the Nano runs at 5V and the ESP8266 runs at 3.3V ?

[/quote]

> [@UKHeliBob](#):
>
> Have you taken into account that the Nano runs at 5V and the ESP8266 runs at 3.3V ?

[/quote]

best regards Stefan

[Next page](https://forum.arduino.cc/t/i2c-communication-btw-esp8266-and-arduino-nano/1184048.md?page=2)
