Hi! I have an Arduino Nano board and 2 HMC5883L magnetometers(correct me if I'm wrong) but when wiring them and connecting them to the board the Serial Monitor stops displaying any info and the TX led stops blinking. This happens only when connecting the SCL on the HMC5883 to the A5 pin on the board, this happens with both sensors and my other Arduino Jade Nano+ board, tried using the internal resistors or 10k ones but the result is the same. If I open the Serial Monitor without connecting the A5 pin everything is ok, it's displaying the same values over and over because the pin is not connected, and when connecting the pin while the Serial Monitor is running causes it to stop. What is happening, I'm tired of this crappy sensors.....
Post your code and schematic.
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <Wire.h>
#include <Servo.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
Servo EP;
int Variation = 90;
int TargetHeading;
int CurrentHeading;
int Deviation;
void setup() {
Serial.begin(9600);
mag.begin();
pinMode(A0, INPUT);
EP.attach(9);
}
void AcquireTargetHeading(){
TargetHeading = analogRead(A0);
TargetHeading = map(TargetHeading, 0, 1023, 0, 360);
Serial.println(TargetHeading);
}
void AcquireHeading(){
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
float declinationAngle = 0.109;
heading += declinationAngle;
if(heading < 0)
heading += 2*PI;
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
CurrentHeading = int(headingDegrees);
}
void GuidanceSystem(){
Deviation = TargetHeading - CurrentHeading;
Variation = 90 + Deviation;
constrain(Variation, 80, 110);
EP.write(Variation);
}
void loop() {
AcquireTargetHeading();
AcquireHeading();
GuidanceSystem();
delay(20);
}
Here's the wiring, YES I know the board pins are not soldered, but I just got this new board...
I don't have these sensors to test here, so we'll try debugging in parts.
You have 3 functions that use this module. right?
AcquireTargetHeading();
AcquireHeading();
GuidanceSystem();
First comment, (use // before calling the functions), the three lines and see if it runs with the module connected to A4 and A5.
And see if the serial works normally.
// AcquireTargetHeading();
// AcquireHeading();
// GuidanceSystem();
Well now it doesn't work even with the pin disconnected...
Sorry, the print is inside the function.
Use this code for debug.
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <Wire.h>
#include <Servo.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
Servo EP;
int Variation = 90;
int TargetHeading;
int CurrentHeading;
int Deviation;
void setup() {
Serial.begin(9600);
mag.begin();
pinMode(A0, INPUT);
EP.attach(9);
}
void AcquireTargetHeading(){
TargetHeading = analogRead(A0);
TargetHeading = map(TargetHeading, 0, 1023, 0, 360);
Serial.println(TargetHeading);
}
void AcquireHeading(){
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
float declinationAngle = 0.109;
heading += declinationAngle;
if(heading < 0)
heading += 2*PI;
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
CurrentHeading = int(headingDegrees);
}
void GuidanceSystem(){
Deviation = TargetHeading - CurrentHeading;
Variation = 90 + Deviation;
constrain(Variation, 80, 110);
EP.write(Variation);
}
void loop() {
// AcquireTargetHeading();
// AcquireHeading();
// GuidanceSystem();
delay(200);
Serial.println("test");
}
Well it just prints "test" infinitly...
Now release one function at a time and test.
You mean removing the "//"?
Yes, One line at a time and test.
Well I "released" the first one, "AcquireTargetHeading()" and nothing is shown on the Serial Monitor, and the time when it was saying "test" repeatedly the A5 was disconnected..
OK, now comment the first and release the second.
Same
With the sensor off and the 2nd and third lines commented out, what prints on the serial?
"test" and random numbers of what I can assume are the missing potentiometer values...
You must solder the pin headers to the Nano. The circuit will not be reliable if you do not do this. Maybe this is causing your problem.
Running the sensor at 3.3v with a 5v Nano may be causing problems.
Are you connecting both sensors at the same time?
The pins are soldered on the other board I have, I mentioned it in the post
No.