Unable to send data via I2C in nano BLE 33 and BlE sense board

Hi All,

I am trying to communicate with AD5933 PmodIA sensor via i2c using Arduino nano BLE 33 boards (I have both sense board and non-Sense board). I am not able to send data to the sensor via I2C. I went through the tutorial: https://docs.arduino.cc/tutorials/nano-33-ble-sense/I2C to see if my I2C communication works. Here I figured out I can't send data via I2C to turn on and off LEDs but I can use them to receive data, when I used them along with an UNO board. So they can act as reader but not as writer. When I try to use them as writer the serial monitor gets stuck after I try to send the data first time. Same thing happens when I try to communicate with my PmodIA. The serial monitor gets stuck once I try to send something via I2c and the data is also not being sent. Can anyone help me? Previously I thought the issue was with AD5933 library but now I'm sure it is not that. Does anyone know what to do here?

Reading from an I2C client requires that the master sends its address first. I.e. the hardware seems to work. Please show your code.

Writer's code:
#include <Wire.h>
extern TwoWire Wire1;

void setup() {
Serial.begin(9600);
while (!Serial);
Wire1.begin(); // join i2c bus (address optional for writer)
}

void loop() {
Serial.print("Enter 0 to turn off led, enter 1 to turn it on ");
char ledVal[0];
readSerial(ledVal);
Serial.println(ledVal);
Wire1.beginTransmission(8); // transmit to device #8
Wire1.write(ledVal); // sends the given value
Wire1.endTransmission(); // stop transmitting
delay(500);
}

/* Read input serial */
int readSerial(char result[ ]) {
int i = 0;
while (1) {
while (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar == '\n') {
result[i] = '\0';
Serial.flush();
return 0;
}
if (inChar != '\r') {
result[i] = inChar;
i++;
}
}
}
}
This works when used on UNO

That's not valid code. Please use Code Tags </> to present valid code!

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Wire.begin(); // join i2c bus (address optional for writer)
}

void loop() {
  Serial.print("Enter 0 to turn off led, enter 1 to turn it on ");
  char ledVal[0];
  readSerial(ledVal);
  Serial.println(ledVal);
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(ledVal);        // sends the given value
  Wire.endTransmission();    // stop transmitting
  delay(500);
}

/* Read input serial */
int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

Sorry. Can you see it now?

Much better, but it definitely is different from the previous post using Wire1.

I also don't understand:

An empty array?

Wire1 was with some changes but this is the original code I found from the tutorial I mentioned. Yeah I think that is declaring an empty array that will be used when user enters 0 or 1 and will be sent to the reader. The code works fine when used with UNO. Only Nano Board cannot be used as Writer.

I cannot see how an empty array (size 0) can be used to hold anything. Any messages from the compiler?

No. The compiler doesn't give any error.

There are some complex reasons why the compiler doesn't complain about the zero length array

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Writing and reading from that array will give undefined behaviour.

Your array should be at least 2 bytes, one for the data and one for the null which you add.

 //char ledVal[0];
 char ledVal[2];

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