ESP8266 master / Attiny85 Slave i2c problem

Hi, I am trying to make a weather station with an ATTiny85 counting the rain gauge bucket tips, then starting an ESP8266 every 3 minutes using a latching circuit. The ESP is connect via i2c to BME280 sensor and the ATTiny. The ESP reads the sensor, and the bucket tips from ATTiny and then sends to MQTT server over wifi. It then shuts itself down.

The problem I am having is with i2c. If if disconnect the SDA cable from the Attiny85 everything works fine, and it runs happy for days, however the ESP can not get the rain bucket tips from the ATTiny.

If I leave the SDA cable connected, it will work for a while (a few mins to 20 mins) then the ESP hangs, until I disconnect power. Pressing reset button on ESP reboots ESP but hangs again straight away.

I have noticed there is power going through the ATTiny SDA pin, and via SDA back to ESP when it is shutdown, so I am thinking this is somehow corrupting the ESP program/HW.

I did read somewhere that lowering the clock speed on i2c of ESP might fix it.. however it did not work.

Wire.setClockStretchLimit(1500);

I have tried without resistors on SDA line.

Does anyone know how to fix this. Or is there a way to stop power going to the ESP SDA pin from ATTiny via SW or HW when not needed - when ESP is shut down. Or is there some other way I can send the rain data to the ESP from the ATTiny ?

Thanks

1 Like

This a really great post

Right, check what the ATtiny supports (400000?).

Are you really using 220R pullup resistors? That's way too low, try 2k2 instead.

Are all devices on the I2C bus powered by the same voltage? If not you'll need a level shifter.

The combination might not work.
The ATtiny as a Slave should be no problem and the ESP8266 as a Master should be no problem, but the combination might be a problem.

Is the ATtiny running at 3.3V as well ?
How long are the wires for the I2C bus ? The I2C bus is not supposed to go through a cable.

Can you use Serial/UART instead of I2C ?

Here is a similar question about hardware to count rain pulses: Seperate IC to Count Pulse Input?
There seems to be no good solution, but it is an old topic.

It is powered by a 18650 battery - 4.2V.
The ATTiny has a 3.3V regulator, and the ESP is powered via AP2112 regulator with an enable pin.

The pull up resistors are both 10K.

I did try 4000 for i2c bus but made no difference.

If you mean Wire.setClockStretch() then this is the wrong place.

Everything is using 3.3V.
The wires for the i2c bus are max 120mm long from ESP to ATTiny.

I have seen a project that uses an ATTiny and ESP for similar thing, however the ESP is only put into deep sleep. LineaMeteoStazione

I used i2c as it was easier to implement due to i2c sensors.

I am also wanting to connect up a anemometer to ATTiny, so a digital counter IC wont work.

I used the ATTiny mostly because of power usage. I originally used a ESP32 but it power draw was to much, even with deep sleep.

This current setup uses 2mA max most of the time, except for a few seconds every 3 minutes when it goes up to 80 -90 mA.

What are you talking about then. If CPU clock, its running at 8MHz

I had read that the i2c bus speed could be a problem and to drop it to 1500 using Wire.setClockStretchLimit(1500);

ESP8266 to Nano I2C Problems.

It would be a great project too if it worked properly !!!

Or The function is?:
Wire.setclock(clockFrequency); //clockFrequency is in Hertz

Thanks, will give it a try.
Should I put on both ATTiny and ESP ?

It is the I2C-Master that generates the SCL pulses of the I2C Bus. So, you change only in the Master sketch.

I get this error:

Compilation error: 'class TwoWire' has no member named 'setclock'; did you mean 'setClock'?

I am using #include <Wire.h>

Sorry, for the typing mistake; it should be:
Wire.setClock(clockFrequency); //clockFrequency is in Hertz

So the combination of a ESP32 and a ATtiny85 does work, even when the ESP32 goes into deep sleep. Good to know.

The ESP8266 can not be compared to the ESP32. Both the hardware and software are different. Can you use a ESP32 ?

Going into deep sleep is different than turning it off.
The SDA and SCL are idle high.
If the ESP32 goes into deep sleep, the SDA and SCL remain high, the I2C bus is in a normal idle state.

If a device is connected to the I2C bus and that device has no power, then the I2C bus is out of order. It does no longer work, because SDA and SCL are pulled low by the device without power. It is even worse, because when the SDA turns low, that indicates the start of a I2C session.
This will always cause a problem. You can find many problems on this forum with I2C devices that have different power sources which may or may not be turned on.
There are ways to unlock a stuck I2C bus: https://github.com/Koepel/How-to-use-the-Arduino-Wire-library/wiki/How-to-unlock-a-stuck-I2C-bus.
However, the combination of things is so bad, please don't do that. The bad combination: a ESP8266, turning off power of a I2C device, trying to unlock the I2C bus in software.

I suggest to use a ESP32 and a ATtiny85 and deep sleep mode for the ESP32. You are already pushing your luck with that.

START condition of I2C bus is marked by asserting LOW on SDA line while SCL is still HIGH.

I have tired to use a ESP32 in the past for similar project, however the problem I have always come up against it power usage even when in deep sleep. The sensors and attached devices are always sucking power. I wanted something as compact as possible and I can use for multiple projects. This one for rain gauge, temp, humidity and pressure, another where I have a anemometer in a different location, for monitoring garage door etc.
With the ATTiny and everything else turned off with latching circuit its down to 2mA's when only ATTiny running, and I could problem get that lower, which with another project I couldn't get it below 40mA with sensors and ESP32 in deep sleep.
So the aim is to use the latching circuit and turn everything off except ATTiny.

Is there some kind of HW solution I can put on the SDA line to stop power going to ESP but still enable communication - similar to latching circuit ?

Thanks. I actually read the info online about Wire.setClock(clockFrequency), and then typed it wrong too. I will try it now.

The I2C bus was not designed for this.

If you use Serial/UART communication with protection resistors in between (and maybe diodes), then you can turn off and on devices. You can add a checksum and a start and stop byte to be sure that you get the right data.

Ok, Thanks for the info, I will serial RX/TX a try.