Components and supplies
Arduino Nano R3
Project description
Code
Arduino Sketch
arduino
1/* 2 This sketch acts as a Magnetic Field Meter, by Marco Zonca, 3/2021 3 Arduino Nano as 5V MPU, UGN3503UA magnetic sensor, i2c Oled 128x32 display, a LED, 4 one button, an active buzzer and a few resistors; 5 6 Magnetic Field Meter, output in milliTesla (mT) 7 8 from sensor +- 1.3 mV = +- 1 Gauss = +- 0.1 mT 9 from sensor 2.5V = 0 (0-2.5V = -2500 to 0 mV, 2.5-5V = 0 to 2500mV) 10*/ 11 12#include <Wire.h> 13#include <Adafruit_SSD1306.h> 14 15Adafruit_SSD1306 display = Adafruit_SSD1306 (128, 32, &Wire, -1); 16 17float aVal=0; 18float aZero=0; 19float aRead=0; 20float VL=0; 21float Gauss=0; 22float mTesla=0; 23float fTot=0; 24float fNr=0; 25float fMax=0; 26float fMin=0; 27float fAvr=0; 28 29unsigned long fprevMillis=0; 30 31const unsigned long fMillis=500; // update display and average calc every 0.5 seconds 32const int aValPin=14; 33const int aLedPin=10; 34const int aBuzPin=9; 35const int aButton=15; 36 37String mT = " -0.0 mT"; 38 39void setup() { 40 //Serial.begin(9600); 41 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Display Address 0x3C for 128x32 pixels 42 mT.reserve(20); 43} //setup() 44 45void loop() { 46 aRead=analogRead(aValPin); 47 if (aZero==0) aZero=aRead; // initial auto zero 48 if (digitalRead(aButton)==LOW) aZero=aRead; // forced zero with button 49 aVal=(map(aRead,0,1023,0,5000)-map(aZero,0,1023,0,5000)) / 1.3; // to milliVolt, to Gauss 50 if (aVal != 0) { 51 fTot=fTot+aVal; 52 fNr=fNr+1; 53 } 54 if (aVal>fMax) fMax=aVal; // mem max 55 if (aVal<fMin) fMin=aVal; // mem min 56 if (millis() > (fMillis+fprevMillis)) showVal(); 57} //loop() 58 59void showVal() { 60 if (fMax != 0) { 61 fTot=fTot-fMax; // average without max 62 fNr=fNr-1; 63 } 64 if (fMin != 0) { 65 fTot=fTot-fMin; // average without min 66 fNr=fNr-1; 67 } 68 if (fNr > 0) { 69 aVal=fTot/fNr; // average value 70 } else { 71 aVal=0; 72 } 73 if (aVal != aZero) { 74 Gauss=aVal; 75 mTesla=Gauss/10; // to milliTesla 76 VL=abs((aVal) / 10); 77 if (VL>255) VL=255; 78 analogWrite(aLedPin,VL); // lights led 79 analogWrite(aBuzPin,VL); // beeps buz 80 //Serial.print(roundOneDec(mTesla)); 81 //Serial.println("mT"); 82 } else { 83 Gauss=0; 84 mTesla=0; 85 analogWrite(aLedPin,0); // off led 86 analogWrite(aBuzPin,0); // off buz 87 } 88 display.clearDisplay(); // output to display 89 display.setTextColor(SSD1306_WHITE); 90 display.setTextSize(1); 91 display.setCursor(1,1); 92 display.print("Magnetic Field Meter"); 93 display.setTextSize(2); 94 display.setCursor(8,16); 95 mT=" "+String(roundOneDec(mTesla),1)+" mT"; 96 display.print(mT.substring(mT.length()-9)); // right alignment 97 display.display(); 98 99 fprevMillis=millis(); 100 fTot=0; 101 fNr=0; 102 fMin=0; 103 fMax=0; 104} //showVal() 105 106float roundOneDec(float f) { 107 float y, d; 108 y = f*10; 109 d = y - (int)y; 110 y = (float)(int)(f*10)/10; 111 if (d >= 0.5) { 112 y += 0.1; 113 } else { 114 if (d < -0.5) { 115 y -= 0.1; 116 } 117 } 118 return y; 119} //roundOneDec() 120
Arduino Sketch
arduino
1/* 2 This sketch acts as a Magnetic Field Meter, by Marco Zonca, 3/2021 3 Arduino Nano as 5V MPU, UGN3503UA magnetic sensor, i2c Oled 128x32 display, a LED, 4 one button, an active buzzer and a few resistors; 5 6 Magnetic Field Meter, output in milliTesla (mT) 7 8 from sensor +- 1.3 mV = +- 1 Gauss = +- 0.1 mT 9 from sensor 2.5V = 0 (0-2.5V = -2500 to 0 mV, 2.5-5V = 0 to 2500mV) 10*/ 11 12#include <Wire.h> 13#include <Adafruit_SSD1306.h> 14 15Adafruit_SSD1306 display = Adafruit_SSD1306 (128, 32, &Wire, -1); 16 17float aVal=0; 18float aZero=0; 19float aRead=0; 20float VL=0; 21float Gauss=0; 22float mTesla=0; 23float fTot=0; 24float fNr=0; 25float fMax=0; 26float fMin=0; 27float fAvr=0; 28 29unsigned long fprevMillis=0; 30 31const unsigned long fMillis=500; // update display and average calc every 0.5 seconds 32const int aValPin=14; 33const int aLedPin=10; 34const int aBuzPin=9; 35const int aButton=15; 36 37String mT = " -0.0 mT"; 38 39void setup() { 40 //Serial.begin(9600); 41 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Display Address 0x3C for 128x32 pixels 42 mT.reserve(20); 43} //setup() 44 45void loop() { 46 aRead=analogRead(aValPin); 47 if (aZero==0) aZero=aRead; // initial auto zero 48 if (digitalRead(aButton)==LOW) aZero=aRead; // forced zero with button 49 aVal=(map(aRead,0,1023,0,5000)-map(aZero,0,1023,0,5000)) / 1.3; // to milliVolt, to Gauss 50 if (aVal != 0) { 51 fTot=fTot+aVal; 52 fNr=fNr+1; 53 } 54 if (aVal>fMax) fMax=aVal; // mem max 55 if (aVal<fMin) fMin=aVal; // mem min 56 if (millis() > (fMillis+fprevMillis)) showVal(); 57} //loop() 58 59void showVal() { 60 if (fMax != 0) { 61 fTot=fTot-fMax; // average without max 62 fNr=fNr-1; 63 } 64 if (fMin != 0) { 65 fTot=fTot-fMin; // average without min 66 fNr=fNr-1; 67 } 68 if (fNr > 0) { 69 aVal=fTot/fNr; // average value 70 } else { 71 aVal=0; 72 } 73 if (aVal != aZero) { 74 Gauss=aVal; 75 mTesla=Gauss/10; // to milliTesla 76 VL=abs((aVal) / 10); 77 if (VL>255) VL=255; 78 analogWrite(aLedPin,VL); // lights led 79 analogWrite(aBuzPin,VL); // beeps buz 80 //Serial.print(roundOneDec(mTesla)); 81 //Serial.println("mT"); 82 } else { 83 Gauss=0; 84 mTesla=0; 85 analogWrite(aLedPin,0); // off led 86 analogWrite(aBuzPin,0); // off buz 87 } 88 display.clearDisplay(); // output to display 89 display.setTextColor(SSD1306_WHITE); 90 display.setTextSize(1); 91 display.setCursor(1,1); 92 display.print("Magnetic Field Meter"); 93 display.setTextSize(2); 94 display.setCursor(8,16); 95 mT=" "+String(roundOneDec(mTesla),1)+" mT"; 96 display.print(mT.substring(mT.length()-9)); // right alignment 97 display.display(); 98 99 fprevMillis=millis(); 100 fTot=0; 101 fNr=0; 102 fMin=0; 103 fMax=0; 104} //showVal() 105 106float roundOneDec(float f) { 107 float y, d; 108 y = f*10; 109 d = y - (int)y; 110 y = (float)(int)(f*10)/10; 111 if (d >= 0.5) { 112 y += 0.1; 113 } else { 114 if (d < -0.5) { 115 y -= 0.1; 116 } 117 } 118 return y; 119} //roundOneDec() 120
Downloadable files
PCB bottom solder face
PCB bottom solder face
PCB top components
PCB top components
PCB bottom components
PCB bottom components
Fritzing proto board schematic
Fritzing proto board schematic
PCB OVERVIEW
PCB OVERVIEW
Fritzing proto board schematic
Fritzing proto board schematic
PCB bottom solder face
PCB bottom solder face
PCB bottom components
PCB bottom components
PCB top solder face
PCB top solder face
PCB top components
PCB top components
PCB OVERVIEW
PCB OVERVIEW
Comments
Only logged in users can leave comments