Nicla and Portenta I2C communication

Hi everyone, I am trying to implement the following test code from this Arduino tutorial. I uploaded the NiclaShieldHost & NiclaShieldController sketches (Found in File>Examples>Arduino_Pro_Tutorials>Nicla Sense ME as a MKR Shield) to the Portenta & Nicla, resp....

NiclaShieldHost:

#include "Wire.h"

#define NICLA_I2C_ADDRESS 0x1A

void setup(){
    Serial.begin(9600);
    while (!Serial);

    Wire.begin(); // Join the I2C bus as a Host
    Serial.println("Host started");
}

void loop(){

    I2CWrite(1); // Request Acceleration X
    int16_t acX = getData16();

    I2CWrite(2); // Request Acceleration Y
    int16_t acY = getData16();

    I2CWrite(3); // Request Acceleration Z
    int16_t acZ = getData16();

    Serial.print("ACCELERATION :");
    Serial.print("  X: ");
    Serial.print(acX);
    Serial.print("  Y: ");
    Serial.print(acY);
    Serial.print("  Z: ");
    Serial.print(acZ);
    Serial.println();

    delay(2500);
}

void I2CWrite(int command){
    Wire.beginTransmission(NICLA_I2C_ADDRESS);
    Wire.write(command);
    Wire.endTransmission();
    delay(100);
}

int16_t getData16(){
    int16_t data = 0;

    Wire.requestFrom(0x1A, 2);

    while (Wire.available()){
        for (int i = 0; i < 2; i++){
            data |= Wire.read() << (8 * i);
        }
    }

    return data;
}

NiclaShieldController:

#include "Nicla_System.h"
#include "Arduino_BHY2.h"
#include "Wire.h"

SensorXYZ accel(SENSOR_ID_ACC);
SensorXYZ gyro(SENSOR_ID_GYRO);
Sensor temp(SENSOR_ID_TEMP);

uint8_t command = 0x00;
void requestHandler(); //request event callback
void receiveEvent(int howMany); //receive event callback

void setup(){
    BHY2.begin();

    //Configure Sensors
    accel.configure(1, 0);
    gyro.configure(1, 0);
    temp.configure(1, 0);

    //Give LED feedback to the user
    nicla::leds.begin();
    nicla::leds.setColor(green);

    Wire.begin(0x1A); // join i2c bus with address #0x1A , do not use 0x60 nor 0x6B, the MKR board has those i2c devices

    Wire.onRequest(requestHandler); // Callback triggered from `Wire.requestFrom()` done by the Host

    Wire.onReceive(receiveEvent); // Callback triggered from `Wire.beginTransmission()` done by the Host
}

void loop(){
    BHY2.update(10);
}

void I2CWrite16(int16_t data){
    Wire.write((byte)(data & 0xFF));
    Wire.write((byte)((data >> 8) & 0xFF));
}

void receiveEvent(int howMany){

    nicla::leds.setColor(blue);
    while (Wire.available()){
        command = Wire.read();
    }

    nicla::leds.setColor(off);
}

void requestHandler(){
    nicla::leds.setColor(green);

    int16_t dataX;
    int16_t dataY;
    int16_t dataZ;

    switch (command){
        
    //Update readings command
    case 0:
        break;

    case 1:
        dataX = accel.x();
        I2CWrite16(dataX);
        Serial.println(accel.toString());
        break;

    case 2:
        dataY = accel.y();
        I2CWrite16(dataY);
        break;

    case 3:
        dataZ = accel.z();
        I2CWrite16(dataZ);
        break;

    default:
        break;
    }

    nicla::leds.setColor(off);
}

In the linked tutorial, they attach the Nicla board as a shield but it also says that the example will work with the ESLOV (I2C connector) so I chose the latter.

My problem is that I'm getting 0 for all 3 accelerometer readings irrespective of testing motion...

10:30:50.675 -> Host started
10:30:52.881 -> ACCELERATION :  X: 0  Y: 0  Z: 0
10:30:57.590 -> ACCELERATION :  X: 0  Y: 0  Z: 0
10:31:02.346 -> ACCELERATION :  X: 0  Y: 0  Z: 0
10:31:07.057 -> ACCELERATION :  X: 0  Y: 0  Z: 0
10:31:11.799 -> ACCELERATION :  X: 0  Y: 0  Z: 0

The Nicla is always showing solid green LED.

Remove the "Serial.println(accel.toString());" line from the switch statement in the NiclaShieldController.

I am getting data now but it eventually stops for some reason and goes back to printing all 0s:

12:55:12.918 -> ACCELERATION :  X: 537  Y: 537  Z: 537
12:55:15.788 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:18.605 -> ACCELERATION :  X: 537  Y: 537  Z: 537
12:55:21.509 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:24.360 -> ACCELERATION :  X: 537  Y: 537  Z: 537
12:55:27.166 -> ACCELERATION :  X: 537  Y: 2650  Z: 2650
12:55:30.024 -> ACCELERATION :  X: 538  Y: 538  Z: 537
12:55:32.874 -> ACCELERATION :  X: 537  Y: 537  Z: 537
12:55:35.779 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:38.642 -> ACCELERATION :  X: 537  Y: 537  Z: 537
12:55:41.491 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:44.342 -> ACCELERATION :  X: 539  Y: 537  Z: 537
12:55:47.201 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:50.054 -> ACCELERATION :  X: 539  Y: 539  Z: 538
12:55:52.911 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:55.763 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:55:58.620 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:56:01.470 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:56:04.328 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:56:07.231 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:56:10.083 -> ACCELERATION :  X: 538  Y: 538  Z: 538
12:56:13.942 -> ACCELERATION :  X: 539  Y: 0  Z: 0
12:56:18.650 -> ACCELERATION :  X: 0  Y: 0  Z: 0
12:56:23.361 -> ACCELERATION :  X: 0  Y: 0  Z: 0
12:56:28.118 -> ACCELERATION :  X: 0  Y: 0  Z: 0
12:56:32.827 -> ACCELERATION :  X: 0  Y: 0  Z: 0
12:56:37.534 -> ACCELERATION :  X: 0  Y: 0  Z: 0
12:56:42.297 -> ACCELERATION :  X: 0  Y: 0  Z: 0

Also the LED on the Nicla stops flashing...

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