Hi guys… Do help out this newbie here… i recently purchased this HMC5883L Magnetometer and i wired it to my Photon… however… whenever i run the code… the Photon just breathese cyan after 10s… does this mean the Magnetometer is spoilt? I’ve wired it according this code below, and all it shows on serial monitor is
Sensor: HMC5883
Driverr Ver: 1
Unique ID: 12345
Max Value: 800.00uT
Min Vlaue: -800.00uT
Resolution: 0.20uT
Its the same print even when I disconnect the HMC and run the code (I would expect the line “Ooops, no HMC5883 detected … Check your wiring!”, but it does not print this).
Can anyone advise??? I am worried I damaged the HMC when i was soldering, cause I am a noob at soldering so I think the component took some heat…
Below is the code i used. I only connected the 4 pins(SDA, SCL, VCC, GND).
(Note: I just troubleshooted and realise its the mag.getEvent(&event) that causes it… when i comment that line and assign value to x,y,z manually, the photon run fine (but the values are not the magnetic value of coz). Is it because the HMC is spoilt therefore there is no data coming from it or smth???)
ANY HELP WOULD BE SO MUCH APPRECIATED :)))))
Below is the code
/***************************************************************************
This is a an example for the Adafruit HMC5883 magnetometer/compass library
Note from @Fidel: The example was not ported with the library, so I tried it with some help and now it works, but I found the calculated heading is far from correct...
Connections:
Module Photon
DRDY Data Ready pin: "To speed up readings"...
SDA SDA (D0)
SCL SCL (D1)
Gnd Gnd
Vcc 3v3
***************************************************************************/
// Libraries include for use with Particle Lib (not Web IDE)
#include "math.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883.h"
#define PI 3.1415926535897932384626433832795
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup(void)
{
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.print("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
//mag.getEvent(&event); //works when i comment this. Turns cyan when i run this
event.magnetic.x = 10; //manual values i used for troubleshooting
event.magnetic.y = 10;
event.magnetic.z = 10;
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Add your 'Declination Angle' to your heading, ('Error' of the magnetic field in your location) Find yours here: http://www.magnetic-declination.com/ In Belgium: +0° 47' E = 0.015 rad (360° = 2 PI rad)
float declinationAngle = 0.015;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500); // Slow down serial monitor readings!
In order to make the Particle code more compatible with Arduino libraries such adaptions should not be needed anymore when you now add #include <Arduino.h> to your main project.
This should take care of actually defining ARDUINO 100 for the whole project.
Disclaimer: I have not tested this, but if you can report back if this worked, we’d appreciate the feedback
To prevent the device from losing cloud connection change this in your code
if(!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.print("Ooops, no HMC5883 detected ... Check your wiring!");
while(1) Particle.process; // <-- to keep cloud connection alive
}
What device are you using?
How are you programming/flashing the device?
Assuming it’s a Particel device - since is the Particle forum - these devices have a system firmware on-board.
And the usere firmware should match that system version.
If you are using Web IDE you can set the target system version like shown here
On the device, but what have you selected as application firmware target (in the drop down)?
You can have 0.6.3 on the device, but if you can still build an e.g. 0.5.0 application which would not allow #include <Arduino.h> while 0.6.3 does.
BTW, if you get an error message when compiling, it's always good to post that too (best the SHOW RAW output surrounding the actual error message)
I just realised your original answer which i copied and pasted has a spelling mistake, the include works when i fix it:
#include <Adruino.h>
#include <Arduino.h>
But i get the same result as the original poster, flashing green LED.
Code below:
/***************************************************************************
This is a an example for the Adafruit HMC5883 magnetometer/compass library
Note from @Fidel: The example was not ported with the library, so I tried it with some help and now it works, but I found the calculated heading is far from correct...
Connections:
Module Photon
DRDY Data Ready pin: "To speed up readings"...
SDA SDA (D0)
SCL SCL (D1)
Gnd Gnd
Vcc 3v3
***************************************************************************/
// Libraries include for use with Particle Lib (not Web IDE)
#include "math.h"
#include <Arduino.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883.h"
#define PI 3.1415926535897932384626433832795
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup(void)
{
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1) Particle.process; // <-- to keep cloud connection alive
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
void loop(void)
{
Serial.print("Loop start");
/* Get a new sensor event */
sensors_event_t event;
Serial.print("Loop start1");
//mag.getEvent(event);
mag.getEvent(&event);
Serial.print("Loop start2");
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Add your 'Declination Angle' to your heading, ('Error' of the magnetic field in your location) Find yours here: http://www.magnetic-declination.com/ In Belgium: +0° 47' E = 0.015 rad (360° = 2 PI rad)
float declinationAngle = 0.015;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
Serial.print("Loop end");
delay(500); // Slow down serial monitor readings!
}
It won't change a lot as long the I2C addresss issue isn't solved. The original code does in fact produce two objects - one with the default ID -1 and a second with the custom ID.
anyway guys, sorry i took so long to reply, have been bogged down for last week and haven’t been able to touch my project.
The problem still persist after i included the include <arduino.h>, it still flashes green haiz… so yea haven’t been able to fugure out that one…
my friend gave me a LSM303 and the library code works, I will stick to LSM303 for now for compass purpose. Thanks so much nonetheless for all the help you guys gave!