LIS3MDL library problem

Hello.

I am creating the program for my Arduino project. I have some sensors, including a magnetometer, the LIS3MDL or AltIMU-10 v5. I am having a problem:

In this code, when I use the mag.read(); function, I don't get any data, but when I eliminate the line, I get everything except the magnetometer data.

#include "DHT.h"
#define PINDHT 2
#define TIPODHT DHT11
DHT dht(PINDHT, TIPODHT);

#include <Wire.h>
#include <Adafruit_BMP085.h>
#define presionNivelMar_hPa 1013.25
Adafruit_BMP085 bmp;

#include <BH1750FVI.h>
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);

#include <LIS3MDL.h>
LIS3MDL mag;
char report[80];

const int MPU_ADDR = 0x68;
int16_t acelerometro_x, acelerometro_y, acelerometro_z;
int16_t giroscopio_x, giroscopio_y, giroscopio_z;
int16_t temperatura;
char tmp_str[7];
char* convert_int16_to_str(int16_t i) {
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

int temt6000 = A0;
float luz;
int valor_luz;


void setup() {
  Serial.begin(9600);
  pinMode(temt6000, INPUT);
  dht.begin();
  bmp.begin();
  LightSensor.begin();
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  mag.enableDefault();
}


void loop() {
  int valor_luz = analogRead(temt6000);
  luz = valor_luz * 0.0976;
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  uint16_t lux = LightSensor.GetLightIntensity();
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_ADDR, 7*2, true);
  acelerometro_x = Wire.read()<<8 | Wire.read();
  acelerometro_y = Wire.read()<<8 | Wire.read();
  acelerometro_z = Wire.read()<<8 | Wire.read();
  temperatura = Wire.read()<<8 | Wire.read();
  giroscopio_x = Wire.read()<<8 | Wire.read();
  giroscopio_y = Wire.read()<<8 | Wire.read();
  giroscopio_z = Wire.read()<<8 | Wire.read();
  
  Serial.print("Temperatura DHT11: ");
  Serial.print(t);
  Serial.println(" ºC");

  Serial.print("Humedad DHT11: ");
  Serial.print(h);
  Serial.println(" %");

  Serial.print("Intensidad de luz ambiental TEMT6000: ");
  Serial.print(luz);
  Serial.println(" %");

  Serial.print("Intensidad de luz ambiental GY30: ");
  Serial.print(lux);
  Serial.println(" lux");

  Serial.print("Temperatura BMP180: ");
  Serial.print(bmp.readTemperature());
  Serial.println(" ºC");

  Serial.print("Presión barométrica BMP180: ");
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");

  Serial.print("Presión barométrica al nivel del mar (calculado) BMP180: ");
  Serial.print(bmp.readSealevelPressure());
  Serial.println(" Pa");

  Serial.print("Altitud BMP180: ");
  Serial.print(bmp.readAltitude());
  Serial.println(" m");

  Serial.print("Altitud real (calculado) BMP180: ");
  Serial.print(bmp.readAltitude(presionNivelMar_hPa * 100));
  Serial.println(" m");

  Serial.print("Temperatura GY251: ");
  Serial.println(temperatura/340.00+36.53);

  Serial.print("Velocidad X GY521: ");
  Serial.println(convert_int16_to_str(acelerometro_x));

  Serial.print("Velocidad Y GY521: ");
  Serial.println(convert_int16_to_str(acelerometro_y));

  Serial.print("Velocidad Z GY251: ");
  Serial.println(convert_int16_to_str(acelerometro_z));

  Serial.print("Giroscopio X GY251: ");
  Serial.println(convert_int16_to_str(giroscopio_x));

  Serial.print("Giroscopio Y GY251: ");
  Serial.println(convert_int16_to_str(giroscopio_y));

  Serial.print("Giroscopio Z GY251: ");
  Serial.println(convert_int16_to_str(giroscopio_z));

  snprintf(report, sizeof(report), "M: %6d %6d %6d", mag.m.x, mag.m.y, mag.m.z);
  Serial.println(report);
  
  Serial.println();
  mag.read();
}

Do you know any solutions to get the magnetometer data while keeping the rest of the data? Thank you.

How do you know, and what actually happens?

Have you tested the magnetometer alone, with a simple library example?

I have tested it alone and it works like that. I am using common SCL and SDA ports on the Arduino UNO for the sensors that need those ports (I2C bus). I believe the mag.read(); function interferes with the I2C bus.

Please answer this question:

How do you know, and what actually happens?

Please accurately define your entire setup. Enumerate the devices that are connected to the Arduino, along with their individual I2C addresses, which all have to be different.

Are you saying that maybe the address to the magnetometer might be the same as another sensor? In that case, how do I differentiate them?

There are only 127 I2C addresses. Most sensors allow you to choose from a few options.

So, what did you choose?

I2C tutorial: Basics of the I2C Communication Protocol

I have run the I2c scanner Arduino example. They're all different addresses

I do not see a mag.init() call inside setup().

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