Need help for a project!

Hey I’m working in Arduino project, I’m using qmc5883l sensor, I have to detect magnetic field of wires in the wall but I got a problem and I need help with the code ,this is my first school project, can anyone show me what to do?
I don’t know what to do next, my values get higher when a magnet is near the sensor but I don’t this this is correct so anyone any idea?

#include <QMC5883LCompass.h>
#include <Wire.h> //I2C Arduino Library
QMC5883LCompass compass;
#define addr 0x0D //I2C Address for The HMC5883
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x01); // Set the Register
  Wire.endTransmission();
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x1D); // Set the Register
  Wire.endTransmission();
}
void loop() {

  int x, y, z; //triple axis data
  Wire.beginTransmission(addr);
  Wire.write(0x00); //start with register 3.
  Wire.endTransmission();

  //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if (6 <= Wire.available()) {
    x = Wire.read(); //MSB  x
    x |= Wire.read() << 8; //LSB  x
    z = Wire.read(); //MSB  z
    z |= Wire.read() << 8; //LSB z
    y = Wire.read(); //MSB y
    y |= Wire.read() << 8; //LSB y
  }
  float B = sqrt(pow(x, 2) + pow(z, 2)+pow(y, 2)) * 1.3 / 2048;
  // Show Values
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();
  Serial.print("B Value: ");
  Serial.println(B);
  delay(1000);
}

Post code, not pictures of code.

You should post code by using code-tags

There is an automatic function for doing this inthe Arduino-IDE just three steps 1) press Ctrl-T for autoformatting your code 2) do a rightclick with the mouse and choose "copy to forum" 3) paste clipboard into write-window of a posting

From your description it is not clear to me what you want to achieve. I don't have this sensor laying around or having your hardware wired ready to run. So the bare minimum you should provide is posting the serial-output you are getting and a detailed description how this serial out-put deviates from what you want to achieve

Growing up towards any technical profession means to learn more and more developing things on your own. Beeing grown up and beeing a very experienced technician or engineer still includes asking questions. But a different kind of questions than a general "can you show me how to do it?"

The grown-up-questions are specific about a detail of a knowledge-area that is outside of this persons knowledge right now.

So what is the first line of code you do not understand? Ask a specific question about this.

best regards Stefan

float B=sqrt(pow(x,2)+pow(z,2)pow(y,2))*1.3/2048;

I doubt that this compiles without an error message. Is there a missing plus sign?

Are there other errors?

vaj4088: float B=sqrt(pow(x,2)+pow(z,2)pow(y,2))*1.3/2048;

I doubt that this compiles without an error message. Is there a missing plus sign?

Are there other errors?

No there aren't any errors ,I forgot to add plus this time but i definitely had that plus sign when I tried.

OK compiles with no errors. What serial output do you see?

What serial output do you expect?

StefanL38: OK compiles with no errors. What serial output do you see?

What serial output do you expect?

I see values of magnetic field in x, y, z axis and the value of the intensity B, the problem is when I get the sensor near the wall in which is an electric wire I expect the value of intensity get higher ,but it doesn't!

your description

I see values of magnetic field in x, y, z axis and the value of the intensity B, the problem is when I get the sensor near the wall in which is an electric wire I expect the value of intensity get higher ,but it doesn't!

is pretty unprecise. Is all your hardware at school and unavailable until school starts again in the new year?

or are you able to run the code and post serial output?

Is the wire carrying DC or AC?

StefanL38: your descriptionis pretty unprecise. Is all your hardware at school and unavailable until school starts again in the new year?

or are you able to run the code and post serial output?

StefanL38: B Value: 0.94

StefanL38: X Value: 310

StefanL38: Y Value: -1718

StefanL38: Z Value: 377

StefanL38: B Value: 1.13

StefanL38: X Value: 480

StefanL38: Y Value: -1662

StefanL38: Z Value: 215

StefanL38: B Value: 1.11

StefanL38: X Value: 695

StefanL38: Y Value: -1318

StefanL38: Z Value: -260

This is an example of an output I get!

TheMemberFormerlyKnownAsAWOL:
Is the wire carrying DC or AC?

AC

I see this in the documentation:

DRDY - this it the 'data ready' pin output. If you want to stream data at high speed (higher than 100 times a second) you may want to listen to this pin for when data is ready to be read. Check the datasheet for more details about using the DRDY pin, we don't use it because we don't read that fast!

But I don't see any code in your program that checks for data being ready to be read. You just read and expect there to be something useful.

Paul

OK You seem to really love the ping-pong game of just providing the minimum of information you have been asked for.

So next small question ready for a short answer:

is the output you posted above from the sensor holding it away from the electrical wire or holding the sensor close to the wire

All the following questions are for not answering in this post. This is just for me to remember what I want to ask next to keep the ping-pong game of small questions and short answer going for many many rounds:

Can you hold the sensor next to a "free flying" wire of a lamp or a hair-dryer Yes or no?

can you hold the sensor next to the wire when the lamp/hair-dryer is switched off run the program ?

can you copy and paste the whole output you get in the serial monitor and paste it at once into a posting?

can you tell for which case the above posting is with information about - lamp/hair-dryer switched ____ - sensor close to the wire or away from the wire

can you hold the sensor close / away from the wire when the lamp/hair-dryer is switched on and run the program?

can you tell for which case the latest above posting is with information about - lamp/hair-dryer switched ____ - sensor close to the wire or away from the wire

as the whole thing is a magnetic sensor

do you have some kind of a magnet yes or no

can you hold this kind of magnet close to the sensor and run the program

can you copy and paste the whole output you get in the serial monitor and paste it at once into a posting?

can you add in another posting if you had the magnet hold close or away from the sensor for the last postig containing values of serial output?

got the picture? I guess you are smart enough to do all this at once

Or do you (secretly) expect somebody will post - ready to use code that is functioning - code that does check all your wiring and tell if there is a wiring error?

as a first test try this example code from GitHub that comes with the QMC5883LCompass.h library

https://raw.githubusercontent.com/mprograms/QMC5883LCompass/master/examples/xyz/xyz.ino

StefanL38: OK You seem to really love the ping-pong game of just providing the minimum of information you have been asked for.

So next small question ready for a short answer:

is the output you posted above from the sensor holding it away from the electrical wire or holding the sensor close to the wire

All the following questions are for not answering in this post. This is just for me to remember what I want to ask next to keep the ping-pong game of small questions and short answer going for many many rounds:

Can you hold the sensor next to a "free flying" wire of a lamp or a hair-dryer Yes or no?

can you hold the sensor next to the wire when the lamp/hair-dryer is switched off run the program ?

can you copy and paste the whole output you get in the serial monitor and paste it at once into a posting?

can you tell for which case the above posting is with information about - lamp/hair-dryer switched ____ - sensor close to the wire or away from the wire

can you hold the sensor close / away from the wire when the lamp/hair-dryer is switched on and run the program?

can you tell for which case the latest above posting is with information about - lamp/hair-dryer switched ____ - sensor close to the wire or away from the wire

as the whole thing is a magnetic sensor

do you have some kind of a magnet yes or no

can you hold this kind of magnet close to the sensor and run the program

can you copy and paste the whole output you get in the serial monitor and paste it at once into a posting?

can you add in another posting if you had the magnet hold close or away from the sensor for the last postig containing values of serial output?

got the picture? I guess you are smart enough to do all this at once

Or do you (secretly) expect somebody will post - ready to use code that is functioning - code that does check all your wiring and tell if there is a wiring error?

The values I posted were from a sensor away from wires, that's the problem when sensor is not near the wires I should get a value between (0.2-0.6)- that's the value of the earths magnetic field intensity, I'm dealing with this for quite a while, and if I bring sensor near a laptop or neat a magnet my values increase, I think the problem is that im multiplying "B" with 1.3/2048,or I don't know but, I know I should not get value of 1.3 when my magnet and wire is not near the sensor. And I tried those examples that you send than you, but I get the same values I get here! Thank you for your time!

did you lookup all the examples on github? there is an calibration example

if there is a calibration-example it seems nescessary to do calibration. What does the documentation of the qmc5883l sensor say?

And there seems to be another problem with definition and application. You ask about the MAGNETIC field sensing, but you never tell us there is CURRENT flowing through the wires. Magnetic field will only be there with current flowing.

If no current flow and you are getting something from the sensor, then you are sensing the ELECTRIC field, not the MAGNETIC field. If this is the case, then you need to add shielding so you ONLY get the magnetic field.

Paul

did you ever do googling with keywords like this?

QMC5883L values for earth magnet field

This website seems of interest: https://www.meccanismocomplesso.org/en/arduino-measuring-the-earths-magnetic-field-with-the-magnetometer-hmc5883l/

they are talking about adjusting a gain-value did you ever do adjusting the gain-value?

And maybe calibrating for the strength of the magnetic field requieres another measuring device to compare the measured values

anyway you have to read more about what the sensors data repreresents

StefanL38: did you lookup all the examples on github? there is an calibration example

if there is a calibration-example it seems necessary to do calibration. What does the documentation of the qmc5883l sensor say?

Yes I looked it up, I tried that I calibrated an added "compass. set Calibration(-1532, 1180, -1560, 2058, -1687, 1321);" in the code but I keep getting values more than 0.6 when sensor is away .I read the data sheet ,tells the features of the sensor , how should I connect it, but I think the problem is in converting units or I don't know how to use the calibration data that I get from that code!

Paul_KD7HB: And there seems to be another problem with definition and application. You ask about the MAGNETIC field sensing, but you never tell us there is CURRENT flowing through the wires. Magnetic field will only be there with current flowing.

If no current flow and you are getting something from the sensor, then you are sensing the ELECTRIC field, not the MAGNETIC field. If this is the case, then you need to add shielding so you ONLY get the magnetic field.

Paul

Look if no current is flowing then I should only sense magnetic field that the sensor senses from earth, when the current starts to flow it creates a magnetic field that the sensor will senses and values will get higher