Can not see the magnetometer values in the Serial Plotter

I am trying to read the magnetometer data.
When I use the following I get 0 values in the Serial Monitor

#define SAMPLE_MAGNETOMETER
....
....
#ifdef SAMPLE_MAGNETOMETER
SensorXYZ mag(SENSOR_ID_MAG);
#endif
....
....
#ifdef SAMPLE_MAGNETOMETER
mag.begin();
#endif
....
....
#ifdef SAMPLE_MAGNETOMETER
ei_printf("%f, %f, %f,"
,mag.x()
,mag.y()
,mag.z()
);
#endif

When I use
Serial.println(String("magnetometer: ") + mag.toString());

I see the values. I suppose something is not good with the
#ifdef SAMPLE_MAGNETOMETER
ei_printf("%f, %f, %f,"
,mag.x()
,mag.y()
,mag.z()
);
#endif

BTW I have also activated the gyroscope in a similar way and reading those data in the serial Monitor and Serial Plotter without a problem.

Did you properly define the ei_printf() function?

yeah

void ei_printf(const char *format, ...);

I am using exactly the same way for acceleromenter, gyroscope, orientation (I just change the names), and it works perfectly well with them. Its only the magnetometer that it doesnt do the trick.

Hi,
To add code please click this link;

I'm not sure why you started in BOLD letters.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi again.

I am just copying the nicla sense ingestion code here from EI with some modifications.
Apologies for the first message
Regards

#define SAMPLE_ACCELEROMETER
#define SAMPLE_GYROSCOPE
#define SAMPLE_ORIENTATION
// #define SAMPLE_ENVIRONMENTAL
// #define SAMPLE_ROTATION_VECTOR

#define SAMPLE_MAGNETOMETER



/**
 * Configure the sample frequency. This is the frequency used to send the data
 * to the studio regardless of the frequency used to sample the data from the
 * sensor. This differs per sensors, and can be modified in the API of the sensor
 */
#define FREQUENCY_HZ 5


/* Include ----------------------------------------------------------------- */
#include "Arduino.h"  // not needed ?
#include "Arduino_BHY2.h"
#include "Nicla_System.h"  //not needed ?


/* Constants --------------------------------------------------------------- */
#if (FREQUENCY_HZ <= 0)
#error "FREQUENCY_HZ should have a value greater dan 0"
#endif
#define INTERVAL_MS (1000 / FREQUENCY_HZ)
#define CONVERT_G_TO_MS2 9.80665f


/* Forward declerations ---------------------------------------------------- */
void ei_printf(const char *format, ...);


/* Private variables ------------------------------------------------------- */
static unsigned long last_interval_ms = 0;

#ifdef SAMPLE_ACCELEROMETER
SensorXYZ accel(SENSOR_ID_ACC);
#endif
#ifdef SAMPLE_GYROSCOPE
SensorXYZ gyro(SENSOR_ID_GYRO);
#endif
#ifdef SAMPLE_ORIENTATION
SensorOrientation orientation(SENSOR_ID_ORI);
#endif
#ifdef SAMPLE_MAGNETOMETER
SensorXYZ mag(SENSOR_ID_MAG);
#endif

#ifdef SAMPLE_ENVIRONMENTAL
Sensor temp(SENSOR_ID_TEMP);
Sensor baro(SENSOR_ID_BARO);
Sensor hum(SENSOR_ID_HUM);
Sensor gas(SENSOR_ID_GAS);
#endif


void setup() {
  /* Init serial */
  Serial.begin(115200);
  Serial.println("Edge Impulse sensor data ingestion\r\n");

  /* Init & start sensors */
  BHY2.begin(NICLA_I2C);
#ifdef SAMPLE_ACCELEROMETER
  accel.begin();
#endif
#ifdef SAMPLE_GYROSCOPE
  gyro.begin();
#endif
#ifdef SAMPLE_ORIENTATION
  orientation.begin();
#endif
#ifdef SAMPLE_ROTATION_VECTOR
  rotation.begin();
#endif
#ifdef SAMPLE_MAGNETOMETER
  mag.begin();
#endif
#ifdef SAMPLE_ENVIRONMENTAL
  temp.begin();
  baro.begin();
  hum.begin();
  gas.begin();
#endif
}

void loop() {

  BHY2.update();
  delay(INTERVAL_MS);

#ifdef SAMPLE_ACCELEROMETER
  ei_printf("%f, %f, %f,", (accel.x() * 8.0 / 32768.0) * CONVERT_G_TO_MS2, (accel.y() * 8.0 / 32768.0) * CONVERT_G_TO_MS2, (accel.z() * 8.0 / 32768.0) * CONVERT_G_TO_MS2);
#endif
#ifdef SAMPLE_GYROSCOPE
  ei_printf("%f, %f, %f,", (gyro.x() * 8.0 / 32768.0) * CONVERT_G_TO_MS2, (gyro.y() * 8.0 / 32768.0) * CONVERT_G_TO_MS2, (gyro.z() * 8.0 / 32768.0) * CONVERT_G_TO_MS2);
#endif
#ifdef SAMPLE_ORIENTATION
  ei_printf("%f, %f, %f,", orientation.heading(), orientation.pitch(), orientation.roll());
#endif


#ifdef SAMPLE_MAGNETOMETER
  ei_printf("%f, %f, %f,", mag.x(), mag.y(), mag.z());
#endif




#ifdef SAMPLE_ROTATION_VECTOR
  ei_printf("%f, %f, %f, %f,", rotation.x(), rotation.y(), rotation.z(), rotation.w());
#endif
#ifdef SAMPLE_ENVIRONMENTAL
  ei_printf("%.2f, %.2f, %.2f, %.2f,", temp.value(), baro.value(), hum.value(), gas.value());
#endif

  ei_printf("\r\n");
}

/**
* @brief      Printf function uses vsnprintf and output using Arduino Serial
*
* @param[in]  format     Variable argument list
*/
void ei_printf(const char *format, ...) {
  static char print_buf[1024] = { 0 };

  va_list args;
  va_start(args, format);
  int r = vsnprintf(print_buf, sizeof(print_buf), format, args);
  va_end(args);

  if (r > 0) {
    Serial.write(print_buf);
  }
}

You have "BHY2.update();" in loop() but I don't see "BHY2.begin();" in setup(). I don't know if it is necessary but it is in the examples.

Oops. I see it now. Nevermind.