ESP 32 read data from arduino nano via i2c


I would like to ask you for advice. In my project I need to use ESP32 as master and arduino nano as slave. The master will read the values via i2c communication. To give an idea arduino nano (slave) will be 3-9 pieces as needed.

For your convenience, I enclose the master and slave code. For example, if I use arduino uno (master) and arduino nano (slave) the code works. If I use ESP32 as master code it does not work … Esp32 sends a signal, arduino nano receives a signal and sends values, but ESP32 no longer accepts / reads values …

I read about similar issues on this forum but bokužel nothing helped. I also tried logic level converters but also nothing.

Thank you in advance for your reply

Arduino_master_i2c.ino (4.3 KB)

Arduino_slave_i2c.ino (1.47 KB)

You should indeed use a level shifter. It is possible that you have damaged the ESP32 already.

The Arduino as a Slave uses clock stretching to run the requestEvent(). The Master must allow clock stretching. I don’t know how that is implemented in the ESP32.

A Wire.requestFrom() should not be used together with Wire.beginTransmission() or Wire.endTransmission().

Remove the Serial functions from the requestEvent. That is a interrupt handler and the Serial functions use interrupts themself. The Serial functions also enlarges the clock stretching.

Using a DTH22 in a Arduino running as I2C Slave is not okay. The DTH22 uses a protocol with timing, and the interrupts are turned off for that. Turning off the interrupts for some time directly influences the reliability of the I2C communication.
Als the NeoPixel and the OneWire use a timing protocol and they also turn off interrupts.

Can you try to find if the ESP32 supports clock stretching ? If it does than you must fix all these problems that I mentioned. I think the ESP32 supports clock stretching up to 12.5 ms. That is enough for the Arduino Nano.

To test if the Wire.requestFrom() is succesful, you can check if the returned value is the same as the number of requested bytes.

When a variable is used in a interrupt and also in the loop() then it must be made ‘volatile’.

When something is not working, you better make it as simple as possible. For example by transferring a single byte.

Thank you for answer.

So it would be better to connect esp32 and arduino over a serial communication (Tx, Rx)? Can you recommend an easier way to communicate for my use?

Thank you

The DHT22 is not accurate for the humidity. When it was shipped in the wrong plastic, it can be even more inaccurate. You better use an other sensor.

You already have a level shifter. The Wire library should be used in the right way. I think the ESP32 supports clock stretching. When also an other sensor is used, then you have fixed all problems.

What is the distance between the ESP32 and the Arduino Nano boards ?
A rule of thumb is maximum 50 cm for the I2C bus. That means with ten Arduino Nano boards, each one can have wires of 5 cm.

Are you thinking about SoftwareSerial ? The SoftwareSerial library for a Arduino Nano uses almost the complete Arduino Nano. There is not much left to do something else, perhaps blinking a led.
The combination of the DHT22 library together with the SoftwareSerial is even worse.

The Arduino Leonardo or (Pro)Micro has a extra hardware Serial port. If you use that, you need level shifters and you have to find a good way to transfer data. For example with a start-byte and a stop-byte.

What are the Arduino Nano boards doing in your project ?

Thank you again for your advice.

Let me introduce you my project, I’m a little advanced in programming, but I’m still learning. So please be patient if you continue to advise me.

My friend is building an apiary and asked me for software management. So I thought to use ESP32 (the sim module) as the main control unit that would also send data to the cloud. It should also measure the temperature, pressure, humidity (BME280) of the outdoor environment. After i2c should collect data from other devices. The first arduino nano should take care of power / control of the energy from the solar panels. Thereafter, about 3-9 arduino nano should be used to monitor individual beehives. This would include measuring the weight of the hx711 module, temperature and humidity (for now DHT22) and a digital display for listing the current hive weight.

So far everything worked for me. ESP32 connection with other arduino nano does not work. I think I would use the Arduino mega 2560 for the mini instead of ESP32.

However, if you advise me of another option, I will be grateful.

Hold your thumb and finger as if holding an egg. That is 5 cm. When you have ten hives, that is how far each of them can be from the central I2C Master. The I2C bus is not designed to go through a cable. There are chips that translate the I2C bus into a twisted pair signal for longer distances.

When doing something with Wifi, then the ESP32 is a good choice. The updates in the last months have improved it, and it works a lot better.

When using long cables, then the best choice (since decades) is to use RS-485 with twisted pair. Then you also need a Arduino with a spare hardware serial port. The Nano board does not have that. There is a official "Arduino MKR 485 Shield".

There are libraries for a wireless network, even for a "mesh" network. The Nano can not do that. You have to use a MKR/Zero/M0 board.

You could use Wifi for everything. Put a ESP32 at every hive. The ESP32 might need a peak of 250mA, so you have use cables for power.

There are (too) many options and (too) many wireless communcation modules. Can you try to find an existing project for hives or for a greenhouse ?