//Including necessary libraries and initializing LCD screen
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
//Activating display and initializing the serial monitor (this is used to check the raw output from the sensor //and monitor the need for recalibration)
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
void loop() {
int volt = analogRead(A0);
//Convert the sensor output to gausses. The sesnor output for a 0G field is 491 and the output increases or //decreases 140 mV per gauss.
float gauss = float((volt - 491.0)/0.14);
float disp;
//Ensure the displayed value is positive
disp = abs(gauss);
//Print the calculated B field magnitude
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Flux Density in");
lcd.setCursor(0,1);
lcd.print("Gausses: ");
lcd.print(disp);
//Print the sensor output to serial
Serial.println(volt);
//Delay to increase reading stability
delay(1000);
}