Currently, I am working on implementing several Nicla Sense MEs in a sensor network. For simplicity, I would like to use I2C/IIC for communications, with the Niclas as I2C peripherals/slaves and, for the time being, an Arduino Uno as the controller. In an initial testing phase, I have tried reading temperature values from one Nicla over I2C. With the exception of one very brief moment, I have not been successful in sending data from the Nicla over the I2C bus. I have tried using a very basic code for the Nicla, basically just assigning an I2C address with: "Wire.begin(0xXX)" where XX are the digits of the I2C slave address. Using the Arduino I2C scanner I am unable to find the Nicla. I have also tried a simple code where the master sends one bit ('0') and the peripheral Nicla sends the text "hello" in return. This setup works if the Nicla is the master and the uno is the peripheral/slave. If I switch the roles this doesn't work. Well, I had one brief period of approx 10-15 seconds where this actually did work, but I have yet to replicate it. I have also tried doing a slightly more advanced test, where I have the Nicla send temperature data instead.
Any kind of feedback as to how to achieve I2C communication with the Arduino Nicla Sense ME (as a peripheral/slave) would be greatly appreciated. My aim is to be able to send both environmental and motion data over I2C from more than one Nicla.
Below I have attached the sketch for the I2C Master (Uno) and the I2C peripheral (Nicla) for sending temp data:
Master code:
#include <Wire.h>
#define PER_ADDR 0xXX
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("I2C Master Initiation");
}
void loop() {
delay(500);
Serial.println("Write data to peripheral");
Wire.beginTransmission(PER_ADDR);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(PER_ADDR, sizeof(float));
while (Wire.available()) {
float temperature;
Wire.readBytes((char*)&temperature, sizeof(temperature));
Serial.println(temperature);
Serial.println("Receive data");
}
}
I2C Peripheral/Slave (Nicla)
#include <Wire.h>
#include "Arduino_BHY2.h"
#define PER_ADDR 0xXX
Sensor temperature(SENSOR_ID_TEMP);
void setup() {
Wire.begin(PER_ADDR);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Peripheral Inititation");
BHY2.begin();
Serial.println("BHY2 Initiated");
temperature.begin();
Serial.println("Temperature measurement initiated");
}
void receiveEvent(int howMany) {
while (0<Wire.available()) {
byte x = Wire.read();
}
Serial.println("Receive Event");
}
void requestEvent() {
float temp = temperature.value();
Wire.write((byte*)&temp, sizeof(temp));
Serial.println("Request event");
}
void loop() {
delay(5);
BHY2.update();
}