Receive and Send bytes differ using I2C

I am creating a project to monitor 24 batteries used in a solar array. I built a simple test with a Pro Mini master and a Micro slave using I2C. Sensors will be attached to the Micro and all processing, reporting and data storage will occur in the Pro Mini. (I will be adding additional SRAM to the mini to accomplish this.)

I can get the master and slave to talk to each other, but in this simple test, the receive bytes differ from the send bytes. For example, the slave send byte of tempA0 is 87 but the master receive byte is 96 and the send byte of voltA6 is 12 (1.2 volts x 10 to convert to an integer) but the receive byte is 15.

Am I missing something with the timing of the send/receive? I've poured over the internet and I can't find anything related to this.

I'm using pins D18 (SDA) and D19 (SLC) on the Pro Mini and pins D2 (SDA) and D3 (SLC) on the Micro. There is common ground and for this test, I am powering the Micro using VCC from the Pro Mini. They are both 5 volt boards.

The Master sketch on the Pro Mini is:

#include <Wire.h>

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

void loop() {
Wire.requestFrom(1,2);
int recTempA0 = Wire.read();
int recVoltA6 = Wire.read();
Serial.println(recTempA0);
Serial.println(recVoltA6);

delay(500);
}

The Slave sketch on the Micro is:

#include <Wire.h>

void setup() {
Serial.begin(9600);
pinMode(18, INPUT);
pinMode(4,INPUT);
digitalWrite(A0, LOW);
digitalWrite(A6, LOW);
Wire.begin(1);
Wire.onRequest(requestEvent);
}

void loop()
{
delay(100);
}

void requestEvent() {
byte tempA0 = analogRead(A0) * .4888;
byte voltA6 = analogRead(A6) * .0049 * 10;
Wire.write(tempA0);
delay(100);
Wire.write(voltA6);
delay(100);

Serial.println(tempA0);
Serial.println(voltA6);

}

I've not tried this but I found this example that shows an Arduino master requesting more than 1 byte from an Arduino slave over I2C.

In the example, the slave response is collated in a byte array which is then transmitted back to the master with 1 write command.

I wonder if the multiple writes inside the slave requestEvent() function are the issue.

Try
this example and when you've got it working, implement your sensors.

You are mixing datatypes and floating point math in these lines:

byte tempA0 = analogRead(A0) * .4888;
byte voltA6 = analogRead(A6) * .0049 * 10;

The outcome of this may not be what you want, try:

byte tempA0 = round((float)analogRead(A0) * .4888);
byte voltA6 = round((float)analogRead(A6) * .0049 * 10.0);

Danois90: Mixing datatypes ... definitely a rookie mistake. It is not the cause of the problem but something for me to keep in mind in the future. I am studying the example you send and will try and implement this evening.

markd833 - when researching this problem, I will study the link you sent. In the mean time, I commented out all but one of the sensors, I get the same result.

This Arduino to Arduino I2C Tutorial may help.

...R

Problem solved! First thank you all for the input. I learned a lot just by exploring the recommended additional links. This was the issue. I was powering the Slave from the Master. I checked and it was definitely 5V out. The connections was from Vcc on the pro mini to Vin on the Micro.

I changed to power the micro through the mini usb and the resulting numbers are now correct. After further research, I appears I need to deliver 5v+ to Vin, maybe around 7.5v to account for voltage drop, etc.

It always seems there is something new.

djacobson:
I appears I need to deliver 5v+ to Vin, maybe around 7.5v to account for voltage drop, etc.

If you want to power an Arduino with a regulated 5v power supply then connect it to the 5v pin.

...R