FG Sensors
Published © CC BY

Grove - Ultra-sensitive magnetometer / aurora sensor

Interested in building a METAL DETECTOR that can detect 10m/33ft depth, building a SOLAR STORM or AURORA observatory?

BeginnerProtip1 hour3,976

Things used in this project

Story

Read more

Code

Grove - Magnetometer Sensor v1.0

Arduino
/*
 * Grove Magenetometer sensor v1.0
 * August 2022
*/ 

#include <Wire.h>
#include "rgb_lcd.h"

rgb_lcd lcd;

const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

/* Define names for used pins */
int sensorDig = 3;                        // D2 connector (D3 input for digital)

/* Define sensor update rate in ms */
unsigned int updateRate = 500;

/* Define sensor measure time in ms */
unsigned int measureTime = 100;

/* Define variables */
int state = 0;                            //Stores current state
volatile unsigned int intEnable = 0;      //0-counter for sensors is not incremented; 1-counter for sensors is incremented
volatile unsigned long sensorDigCnt = 0;  //Stores number of counted changes for Digital sensor
volatile int sensorAnalog = 0;            //Variable to store the value coming from the analog sensor
unsigned long prevMillis = 0;             //Stores previous millis() for calculating refresh rate


/* Setup loop */
void setup() {
  /* Setup LCD column and row number */
  lcd.begin(16, 2);
  lcd.setRGB(colorR, colorG, colorB);

  lcd.setCursor(0,0);
  lcd.print("GROVE MAG SENSOR");

  /* Setup pins */
  pinMode(sensorDig, INPUT);

  /* Setup interrupts */
  attachInterrupt(digitalPinToInterrupt(sensorDig), sensorDigHandler, RISING); 
}

/* Main loop */
void loop() {
  /* Measure current time */
  unsigned long currMillis = millis();

  /* State machine - user can add more states */
  switch(state){
    /* Calculate new sensor data and update LCD */
    case 1:     
      /* Calculate number of actual pulses per [ms]
         Divide by 2 since we measure change in signal and by measureTime to get number of pulses per [ms]*/
         
      sensorDigCnt = (sensorDigCnt);

      /* Update LCD */
      lcdUpdate(sensorDigCnt);
      
      /* Reset variables */
      state = 0;
      
    /* User can add more cases (states) here */
  }

  /* If defined sensor update rate has passed measure sensors */
  if(currMillis - prevMillis >= updateRate) {
    /* Reset variables */
    sensorDigCnt = 0;
     
    /* Save current time */
    prevMillis = currMillis;

    /* Measure number of changes in signal for defined time */
    intEnable = 1;
    delay(measureTime); 
    intEnable = 0;
    
    /* Enable state 1 */
    state = 1;
  }
  
}

/* Digital sensor interrupt handler */
void sensorDigHandler(){
  if (intEnable==1)
    sensorDigCnt++;
}

/* Function to update the LCD */
void lcdUpdate(unsigned long sensorD){
  /*Write new data to LCD */
    lcd.setCursor(0,1);
    lcd.print("     ");
    if(sensorD>9999){
      lcd.print(sensorD);
    }
    else{
       lcd.print(" ");
       lcd.print(sensorD);
    }
}

Credits

FG Sensors

FG Sensors

2 projects • 7 followers
At FG Sensors our main focus are fluxgate magnetometers, custom hardware, software solutions & research.

Comments