-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magnetometre.ino
116 lines (95 loc) · 3.04 KB
/
Magnetometre.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Open Source Magnetometer Arduino Code
* based on Adafruit SSD1306 example program
*
* by Lionel Birglen
* Polytechnique Montreal
* March 2021
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
//Custom variables for magnetometer
int compte=30;
float zero;
int calibration=0;
float mini=0.87; //Minimal voltage output by the sensor, adjusted after experimenting
float maxi=4.20; //Maximal voltage output by the sensor, adjusted after experimenting
float gain=0.0014; //Gain of SS49E sensor : 1.4mV/Gauss
void setup() {
//Initialize serial communication
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
//Display that calibration is running
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println("Calibration...");
//read 30 data points to compute the zero G offset, sensor must be away from magnetic sources
calibration=analogRead(A0);
for (int i = 1; i < compte; i++) {
calibration=calibration+analogRead(A0);
delay(2);
}
Serial.println(calibration);
display.print("Zero = ");
display.println(calibration);
display.display();
//Wait 2 seconds
delay(2000);
}
void loop() {
int capteur;
//Read the sensor
capteur=analogRead(A0);
//Read the sensor "compte-1" more times
for (int i = 1; i < compte; i++) {
capteur=capteur+analogRead(A0);
delay(2);
}
//Average the values read both before and after calibration is taken into account
float tension=capteur*(5.0/1023.0)/compte;
float mesure=(capteur-calibration)*(5.0/1023.0)/compte;
//Clear and setup the display
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print(tension);
display.println("V");
display.setTextSize(2);
//Voltages can be below or above calibration value depending if a north or south pole is measured
if (mesure>0) {
display.print("+");
}
else {
display.print("-");
}
//Display the Gauss value
display.print(abs(mesure)/gain,1);
display.println("G");
//Warn if saturation is reached, displayed value is unreliable
if (tension<mini || tension>maxi) {
display.print("SATURATION");
}
//Display everything and wait 20ms
display.display();
delay(20);
}