How to Make a Magnetic Force Field Change Detector
by Zsolt Fabian in Circuits > LEDs
8131 Views, 53 Favorites, 0 Comments
How to Make a Magnetic Force Field Change Detector
This is a very sensitive DIY magnetic force field change detector/sensor. Using HMC5883L magnetometer module with arduino nano. It also makes sound signals when the magnetic force field changes around the device.
Watch the video here:
You Will Need
- 6 LEDs: 2 red, 2 green, 2 yellow
- 1 prototype PCB (circuit board)
- arduino nano (or micro / UNO)
- HMC5883L magnetometer <---- $2 on ebay
- some wire
- some socket
-
9V battery adapter (optional since you can power it from USB too)
Optional for sound signal:
- potentiometer (change the sound signal or turn it off)
- piezo speaker
Optional: level shifter (5V - 3.3V) I don't use this because it works fine for me without it. But be aware 5V may destroy your HMC5883L module!
Solder up the legs of your arduino and your HMC5883L module.
Attach the Leds and the Sockets on to the Board
- Attach the leds and the sockets on to the board
- Try to follow my circuit design or use your own layout
My code uses Digital Pin: 5, 7, 8, 10, 11, 12 for signal the magnetic change strength
PIN 5, 7 ---> Red LEDs (Low strength)
PIN 8, 10 ---> Green LEDs (Medium strength)
PIN 11, 12 ---> Yellow LEDs (High strength)
I use digital PIN 6 for the optional sound signal
HMC5883L Wiring
Wire the HMC5883L module as above on the image!
SDA ---> A4
SCL ---> A5
VCC ---> 3.3V
GND ---> GND
Potentiometer and Piezo Wiring (Optional)
Wire the potentiometer and piezo with D6 pin as on the image above!
You can change the sound signal with the potentiometer or turn it off completely.
Source Code and Libraries
You will need Wire.h and HMC5883L.h libraries in arduino IDE
You can find HMC5883L.h here:
https://github.com/jarzebski/Arduino-HMC5883L
Watch the video about the final device:
ARDUINO SOURCE CODE:
#include
#include
HMC5883L compass;
void setup() { Serial.begin(9600);
//LED pins pinMode (12, OUTPUT); pinMode (11, OUTPUT); pinMode (10, OUTPUT); pinMode (8, OUTPUT); pinMode (7, OUTPUT); pinMode (5, OUTPUT);
// sound pin pinMode (6, OUTPUT);
// Initialize HMC5883L Serial.println("Initialize HMC5883L"); while (!compass.begin()) { Serial.println("Could not find a valid HMC5883L sensor, check wiring!"); delay(500); }
// Set measurement range compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode compass.setMeasurementMode(HMC5883L_CONTINOUS); // Set data rate compass.setDataRate(HMC5883L_DATARATE_15HZ);
// Set number of samples averaged compass.setSamples(HMC5883L_SAMPLES_8);
// starting intro
digitalWrite (5, HIGH); delay(50); digitalWrite (5, LOW); digitalWrite (7, HIGH); delay(50); digitalWrite (7, LOW); digitalWrite (8, HIGH); delay(50); digitalWrite (8, LOW); digitalWrite (10, HIGH); delay(50); digitalWrite (10, LOW); digitalWrite (11, HIGH); delay(50); digitalWrite (11, LOW); digitalWrite (12, HIGH); delay(50); digitalWrite (12, LOW); digitalWrite (11, HIGH); delay(50); digitalWrite (11, LOW); digitalWrite (10, HIGH); delay(50); digitalWrite (10, LOW); digitalWrite (8, HIGH); delay(50); digitalWrite (8, LOW); digitalWrite (7, HIGH); delay(50); digitalWrite (7, LOW); digitalWrite (5, HIGH); delay(50); digitalWrite (5, LOW);
analogWrite(6, 10); delay(50); analogWrite(6, 50); delay(50); analogWrite(6, 150); delay(50); analogWrite(6, 0);
}
int Xaxis; int Yaxis; int Zaxis; int sumAxis; int lastSumAxis;
void loop() { Vector raw = compass.readRaw(); Vector norm = compass.readNormalize();
Xaxis = norm.XAxis; Yaxis = norm.YAxis; Zaxis = norm.ZAxis;
Xaxis = (Xaxis / 1) * 1; Yaxis = (Yaxis / 1) * 1; Zaxis = (Zaxis / 1) * 1;
sumAxis = Xaxis + Yaxis + Zaxis;
if (lastSumAxis > sumAxis + 5 || lastSumAxis < sumAxis - 5) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (5, HIGH); analogWrite(6, 110); } else { digitalWrite (5, LOW); analogWrite(6, 0); }
if (lastSumAxis > sumAxis + 10 || lastSumAxis < sumAxis - 10) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (7, HIGH); } else { digitalWrite (7, LOW); }
if (lastSumAxis > sumAxis + 20 || lastSumAxis < sumAxis - 20) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (8, HIGH); } else { digitalWrite (8, LOW); }
if (lastSumAxis > sumAxis + 40 || lastSumAxis < sumAxis - 40) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (10, HIGH); } else { digitalWrite (10, LOW); }
if (lastSumAxis > sumAxis + 80 || lastSumAxis < sumAxis - 80) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (11, HIGH); } else { digitalWrite (11, LOW); }
if (lastSumAxis > sumAxis + 160 || lastSumAxis < sumAxis - 160) // when the Hall sensor detects a magnetic field, Arduino LED lights up { digitalWrite (12, HIGH); } else { digitalWrite (12, LOW); }
Serial.print(" Sum Axis = "); Serial.print(sumAxis); Serial.println(); lastSumAxis = sumAxis;
delay(20); }