I'm having 2 arduino uno's connected by i2C. The slave reads a sensor, stores the data and sends it to the Master upon request.
Everything works perfectly fine when I power both arduinos via the USB port (there is no serial communication between the slave and the PC).
But when I power the slave arduino by 9V-battery via the power plug (not using Vin!) the I2C values the Master receives are completly nonsense.
I disabled the internal pull-up (which are activated automatically with wire.begin) for the slave arduino and I don't use any external pull-up at the moment.
9V batteries are only over 9V for a very small portion of their discharge curves.
Check the voltage of the battery.
These also usually have around 300mAh of capacity...where maybe 20-30mAh is over 9V...an UNO uses quite a bit of current and you will easily go through that 20-30mAh over 9V fairly rapidly.
According to this guy:
It is using near 50mA already doing absolutely nothing. So after half an hour, you'd already be dipping below 9V where you will notice timing (and therefore communications issues).
Running some 50-100mA modules on it AND running a script...maybe using 150mA in total, means youd stay over 9V for maybe 5-10 minutes on a 9V battery and exhaust the 300mAh completely in 2 hours.
Connect the grounds of the two arduinos. When they are both on USB the grounds are connected through the computer. When the slave is on a battery the grounds are not connected.