I2C giving strange outputs without including seemingly obsolete commands

Hey all,

I'm new to I2C and have been testing it out to try and get a firm grip on it before I implement it into a larger project.

I was testing out the Wire.onRequest() function and found it wasn't giving expected outputs after I removed Serial.print() functions I was no longer using, despite me not even running Serial.begin() on the slave.

Master code-

#include <Wire.h>


#define SENSORSLAVEADDRESS  0x20

byte collisionByte = 255;


void setup() {
  Serial.begin(9600);
  delay(100);

  Wire.begin();

  Serial.print("READY");
  Serial.println(" ");
  delay(2500);
}

void loop() {
  Wire.requestFrom(SENSORSLAVEADDRESS, 1);
  if (Wire.available()) {
    collisionByte = Wire.read();
  }
  Serial.print(collisionByte);
  Serial.println(" ");
  delay(2500);
}

This is the slave code that gives the expected output-

#include <Wire.h>


#define SENSORSLAVEADDRESS  0x20

byte frontUltrasound = 1;
byte backLeftUltrasound = 2;
byte backRightUltrasound = 4;
byte leftInfrared = 8;
byte rightInfrared = 16;
// front ultrasound                 1st bit
// back left ultrasound             2nd bit
// back right ultrasond             3rd bit
// left infrared                    4th bit
// right infrared                   5th bit


void setup() {
  // put your setup code here, to run once:
  Wire.begin(SENSORSLAVEADDRESS);

  Wire.onRequest(objectDetect);
}

void loop() {
  // put your main code here, to run repeatedly:
  frontUltrasound = 0;
  backLeftUltrasound = 0;
  backRightUltrasound = 0;
  leftInfrared = 0;
  rightInfrared = 0;
  Serial.println(" ");                                          //IF U REMOVE THESE SERIAL PRINT IT DOESNT WORK??
  delay(1000);
  frontUltrasound = 1;
  Serial.println(" ");
  delay(1000);
  backLeftUltrasound = 2;
  Serial.println(" ");
  delay(1000);
  backRightUltrasound = 4;
  Serial.println(" ");
  delay(1000);
  leftInfrared = 8;
  Serial.println(" ");
  delay(1000);
  rightInfrared = 16;
  Serial.println(" ");
  delay(1000);
}

void objectDetect(){
  Wire.write(frontUltrasound | backLeftUltrasound | backRightUltrasound | leftInfrared | rightInfrared);
}

When this code is running the master board outputs the byte increasing and then returning to 0 as expected.

This is the slave code I'm confused about. It is identical to the one above with the exception of the Serial.println() but doesn't give the expected output-

#include <Wire.h>


#define SENSORSLAVEADDRESS  0x20

byte frontUltrasound = 1;
byte backLeftUltrasound = 2;
byte backRightUltrasound = 4;
byte leftInfrared = 8;
byte rightInfrared = 16;
// front ultrasound                 1st bit
// back left ultrasound             2nd bit
// back right ultrasond             3rd bit
// left infrared                    4th bit
// right infrared                   5th bit


void setup() {
  // put your setup code here, to run once:
  Wire.begin(SENSORSLAVEADDRESS);

  Wire.onRequest(objectDetect);
}

void loop() {
  // put your main code here, to run repeatedly:
  frontUltrasound = 0;
  backLeftUltrasound = 0;
  backRightUltrasound = 0;
  leftInfrared = 0;
  rightInfrared = 0;
  delay(1000);
  frontUltrasound = 1;
  delay(1000);
  backLeftUltrasound = 2;
  delay(1000);
  backRightUltrasound = 4;
  delay(1000);
  leftInfrared = 8;
  delay(1000);
  rightInfrared = 16;
  delay(1000);
}

void objectDetect(){
  Wire.write(frontUltrasound | backLeftUltrasound | backRightUltrasound | leftInfrared | rightInfrared);
}

When this code is running the master board outputs 31 every time it makes a request. Interestingly, in my testing I found that whatever the final state of the loop is will be the output (e.g. if I set the variables to 0 at the end of the loop instead of at the beginning the master board outputs 0 every time).

Both the master and the slave are arduino UNO R3.

I'm not sure why one of these works while the other doesn't but would love to learn what's going on.

If there's anything else of use I can provide please let me know.

Remove all the delay() functions form the loop() function of the Slave and try again.

1 Like

31 is correct:
==> 1|2|4|8|16 = 0001 1111 = 31

I run your codes and the MAster's SM shows 31 at 1-sec interval.

Master Sketch:

#include <Wire.h>

#define SENSORSLAVEADDRESS  0x20
byte collisionByte = 255;


void setup()
{
  Serial.begin(9600);
  delay(100);

  Wire.begin();

  Serial.print("READY");
  Serial.println(" ");
  delay(2500);
}

void loop()
{
  Wire.requestFrom(SENSORSLAVEADDRESS, 1);
  collisionByte = Wire.read();
  Serial.print(collisionByte);//1|2|4|8|16 = 0001 1111 = 31 correct
  Serial.println(" ");
  delay(2500);
}

Slave Sketch:

#include <Wire.h>
#define SENSORSLAVEADDRESS  0x20
byte frontUltrasound = 1;
byte backLeftUltrasound = 2;
byte backRightUltrasound = 4;
byte leftInfrared = 8;
byte rightInfrared = 16;

void setup()
{
  Wire.begin(SENSORSLAVEADDRESS);
  Wire.onRequest(objectDetect);
}

void loop() 
{
  frontUltrasound = 0;
  backLeftUltrasound = 0;
  backRightUltrasound = 0;
  leftInfrared = 0;
  rightInfrared = 0;
  frontUltrasound = 1;
  //00000001|00000010|00000100|00001000|00010000
  //0001 1111 = 31
  backLeftUltrasound = 2;
  backRightUltrasound = 4;
  leftInfrared = 8;
  rightInfrared = 16;
  delay(1000);
}

void objectDetect() 
{
  Wire.write(frontUltrasound | backLeftUltrasound | backRightUltrasound | leftInfrared | rightInfrared);
}

Outout on Master's Serial Monior:

31 
31 
31 
31 
31 

Hey thanks for taking the time to test my code. Did you get a chance to test the other slave code?

My main concern is trying to work out why the codes give different outputs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.