Same code working differently on Nano 33 IoT and Mega

I am reading accelerometer data from an Adafruit LSM6DS33 sensor. I am reading this data both with a Mega and with the Nano 33 IoT boards.

With the Mega the readings seem to be working well as follows:

	7.19
	8.43
	7.96
	7.91
	9.07
	10.66
	10.11
	9.18
	8.02
	8.91
	10.08
	9.34
	8.75
	8.96
	9.55
	9.63
	10.07
	9.73
	9.37
	9.79
	9.94
	9.52
	10.04

While with the Nano 33 IoT I am getting these unwanted spikes (close to 0):

	9.85
	9.80
	0.17
	1.26
	-0.15
	0.61
	0.85
	-0.00
	0.04
	0.03
	0.00
	0.03
	9.27
	9.50
	9.57
	0.27
	9.81
	9.82
	9.71
	9.80
	8.65
	9.74
	0.00
	9.57

and when I turn the accelerometer over a certain angle the readings go berserk:

	-0.57
	-0.59
	-0.38
	-0.46
	-0.46
	-0.91
	-19.56
	-19.56
	-19.56
	0.33
	-19.56
	-19.56
	37.03
	-19.56
	0.33
	1.78
	-19.56
	2.38
	-19.56
	-19.56
	1.97
	2.35
	2.02
	-19.56
	1.99
	2.34
	2.05
	1.76
	1.44
	-19.56
	-19.56
	1.23
	-19.56

The above readings are all from the Z-axis' accelerometer.

This is the code I am using:


#include <OSCMessage.h>


// accelerometer and gryroscope libraries
#include <Adafruit_LSM6DS33.h>
Adafruit_LSM6DS33 lsm6ds;

// magnetometer library
#include <Adafruit_LIS3MDL.h>
Adafruit_LIS3MDL lis3mdl;

float thetaX, thetaY, thetaZ;


void setup() {


  //Initialize serial and wait for port to open:
  Serial.begin(115200);

  //            ----  SENSORS -----
  bool lsm6ds_success, lis3mdl_success;

  // hardware I2C mode, can pass in address & alt Wire
  lsm6ds_success = lsm6ds.begin_I2C();
  lis3mdl_success = lis3mdl.begin_I2C();

  if (!lsm6ds_success) {
    Serial.println("Failed to find LSM6DS chip");
  }
  if (!lis3mdl_success) {
    Serial.println("Failed to find LIS3MDL chip");
  }

  lsm6ds.setAccelRange(LSM6DS_ACCEL_RANGE_4_G);
  lsm6ds.setAccelDataRate(LSM6DS_RATE_104_HZ);
  lsm6ds.setGyroRange(LSM6DS_GYRO_RANGE_500_DPS );
  lsm6ds.setGyroDataRate(LSM6DS_RATE_104_HZ);
  lis3mdl.setDataRate(LIS3MDL_DATARATE_155_HZ);
  // You can check the datarate by looking at the frequency of the DRDY pin
  lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS);
  lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE);
  lis3mdl.setIntThreshold(500);
  lis3mdl.configInterrupt(false, false, true, // enable z axis
                          true, // polarity
                          false, // don't latch
                          true); // enabled!




}


void loop() {

  //delay(100);
  //printCurrentNet();

  // creating events for all sensors
  sensors_event_t accel, gyro, mag, temp;

  //  /* Get new normalized sensor events */
  lsm6ds.getEvent(&accel, &gyro, &temp);
  lis3mdl.getEvent(&mag);

  // calculation of 3D trigonometry (refer to notes)
  thetaX = atan2(accel.acceleration.x / 9.8, accel.acceleration.z / 9.8) / 2 / PI * 360 ;
  thetaZ = atan2(accel.acceleration.y / 9.8, accel.acceleration.z / 9.8) / 2 / PI * 360 ;


  Serial.print("\t");
  Serial.println(accel.acceleration.z);



  delay(100);

}

Now one important thing is that I could not calibrate the the sensor. I was using an example code from Adafruit's Sensor Calibration Library (sensor_calibration_write) which accesses either EEPROM or Flash and saves the values on either one of the types of memories found. The Nano IoT should have 256KB of Flash memory, but still this is not working for me. I still think that the sensor data shown above wouldn't be that far off and creating such spikes because it is not calibrated, but I might be wrong.

Would the Arduino Nano 33 IoT and the Arduino Mega work so differently from each other because of the different chip? Or is it really because of the calibration settings? If so why is the sensor_calibration_write example not finding any memory to save data on?

Would really appreciate any directions from your parts. Thanks.

Have you taken into account that the Nano 33 runs at 3.3V whilst the Mega runs at 5V ?

What exactly happens or doesn't happen? Can you be more specific?

Hi digitalnature,

I have a smiliar problem with another Adafruit Sensor (VEML6075).
I can't proof it but it seems like the SPI pin assignment is somehow broken for the Nano.
Which protocol you use? Have you checked if the communication is woking correctly?

Regards Alex

It should not make a difference since the both I2C connections (SDA, SCL) both have level shifting which should make them work with both 3.3v and 5v logic.

When I run this Adafruit Sensor Lab example:

#include "Adafruit_Sensor_Calibration.h"

// select either EEPROM or SPI FLASH storage:
#ifdef ADAFRUIT_SENSOR_CALIBRATION_USE_EEPROM
  Adafruit_Sensor_Calibration_EEPROM cal;
#else
  Adafruit_Sensor_Calibration_SDFat cal;
#endif

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);

  delay(100);
  Serial.println("Calibration filesys test");
  if (!cal.begin()) {
    Serial.println("Failed to initialize calibration helper");
    while (1) yield();
  }
  Serial.print("Has EEPROM: "); Serial.println(cal.hasEEPROM());
  Serial.print("Has FLASH: "); Serial.println(cal.hasFLASH());

  if (! cal.loadCalibration()) {
    Serial.println("No calibration loaded/found... will start with defaults");
  } else {
    Serial.println("Loaded existing calibration");
  }

  // in uTesla
  cal.mag_hardiron[0] = -3.35;
  cal.mag_hardiron[1] = -0.74;
  cal.mag_hardiron[2] = -40.79;

  // in uTesla
  cal.mag_softiron[0] = 0.965;
  cal.mag_softiron[1] = 0.018;
  cal.mag_softiron[2] = 0.010;  
  cal.mag_softiron[3] = 0.018;
  cal.mag_softiron[4] = 0.960;
  cal.mag_softiron[5] = 0.003;  
  cal.mag_softiron[6] = 0.010;
  cal.mag_softiron[7] = 0.003;
  cal.mag_softiron[8] = 1.080;
  // Earth total magnetic field strength in uTesla (dependent on location and time of the year),
  // visit: https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml#igrfwmm)
  cal.mag_field = 45.00; // approximate value for locations along the equator

  // in Radians/s
  cal.gyro_zerorate[0] = 0.05;
  cal.gyro_zerorate[1] = -0.01;
  cal.gyro_zerorate[2] = -0.01;

  if (! cal.saveCalibration()) {
    Serial.println("**WARNING** Couldn't save calibration");
  } else {
    Serial.println("Wrote calibration");    
  }

  cal.printSavedCalibration();
}

void loop() {

}

I am getting this:

Calibration filesys test
Failed to initialize calibration helper

I think this is a separate issue, but could also be related since I am doing the calibration in this way with the Mega. It looks like that code is not finding the Flash memory of the 33 IoT.

How is the sensor powered ?

The sensor is powered by the 5v output (by shorting the VBUS jumper on the back) of the Nano 33 IoT board.

I am using I2C. How can I check if communication is working correctly? Thanks Alex.

Hi,

a) use an Oscilloscope between GND and SCL or SDA.
SCL=clock will show a constant "signal" while SDA=data will change ...

b) Use a LED between GND and SCL or SDA.
It should light up a bit - not very bright. If not something will be wrong.

Alex

If you do not have an oscilloscope and look for a cheaper solution. Google "25MHz Logic analyzer".
You can get one for around $10 or €. They are surprisingly good for the money and fast enough for many protocols supported by Arduinos like SPI, UART, I2C, CAN ...

Have a look at Sigrok PulseView. It is a open source software for these logic analyzers. It supports decoders for many popular protocols. You can try the software without hardware with some build-in trace files.

https://sigrok.org/wiki/Downloads

1 Like

Thanks @Klaus_K, thankfully I have an oscilloscope :slight_smile:

@ademmler these are the readings from my oscilloscope:

Nano 33 IoT Clock:

Nano 33 IoT Data

The data signal does not look like it's changing but the readings on the serial monitor are looking like my original post. The only thing that was changing was the spikes going below the 0V. Are toes supposed to be there, are those the data signals?

Meanwhile these are the readings with same code and circuit, but using an Arduino Mega instead of the Nano 33 IoT..

Mega SCL:

Mega SDA:

The differences I have seen from the two boards is that the Mega goes up to 5v and the Nano 33 to 3v, and that the peaks going under 0V on the Mega are maybe slightly smaller.

The Mega readings on the serial monitor are good too.

All seem good right?

How does the signal look like when you zoom into the time scale so you can see the individual bits? The spikes could be artifacts.

Seems like your time scale is set wrong. Also they curve should be going to positive. 3,3V or 5V depending on your type of Arduino. Here is what my oscilloscope shows. Blue is clock and yellow is data.

Here is more explanation about all this:

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