[solved] Power consumption in sleep mode because Wire.begin()

Hello everybody!
I have Pro Mini 3.3V. I want to minimize the power consumption in sleep.
I noticed that due Wire.begin(); in setup() part power consumption in sleep mode longer.

For example, two sketchs:

  1. With this sketch Arduino uses 0.08 mA (green LED was removed):
#include <avr/sleep.h>
#include <Wire.h>

void tosleep () {
ADCSRA = 0; 
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_bod_disable();
sleep_cpu();
}

void setup () {
  Wire.begin();
}

void test () {
    Wire.beginTransmission(0x68);
  Wire.write(0x0E);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 2);
}

void loop () {
  delay (1000);
  tosleep();
  }
  1. If I remove Wire.begin(); Arduino uses 0.062 mA (18 µA less), but without this function “test” does’t work :confused: :
#include <avr/sleep.h>
#include <Wire.h>

void tosleep () {
ADCSRA = 0; 
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_bod_disable();
sleep_cpu();
}

void setup () {
  
}

void test () {
  Wire.beginTransmission(0x68);
  Wire.write(0x0E);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 2);
}

void loop () {
  delay (1000);
  tosleep();
  }

Is there a way to disable the Wire during sleep mode? I could not find Wire.end() or something like that.

There is a 'Wire.end()' member function in the "Wire" library. The definition:-

void TwoWire::end(void)
{
    twi_disable();
}

Edit: And it calls:-

void twi_disable(void)
{
  // disable twi module, acks, and twi interrupt
  TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));

  // deactivate internal pullups for twi.
  digitalWrite(SDA, 0);
  digitalWrite(SCL, 0);
}

Then when you call 'Wire.begin()' again, it will clear the buffers, then call 'twi_init()', which re-enables pullups, initialises the prescaler, then enables the twi module, acks and interrupt.

N.B. It's well worth digging into the library files when you have this sort of question. There's a wealth of knowledge in those files.

It works! Thank you, OldSteve!

OldSteve: N.B. It's well worth digging into the library files when you have this sort of question. There's a wealth of knowledge in those files.

I promise I'll remember that for the future!

fersin: It works! Thank you, OldSteve!

Excellent. I thought it would, but wasn't 100% sure since I haven't done it myself. :)

@fersin, Could you please post your corrected example code? I think it will be very useful for many others, like me ;-) to see how it works. Thanks!