DIY -Spirit PI- ESP32 + Smartphone Sensitive Metal Detector
A sensitive metal detector which is a great example of cooperation between a microcontroller and an Android smartphone.
Components and supplies
1
Transistor NPN (BC547 or similar)
1
LM741
1
PNP transistor BC557
1
Power MOSFET IRF740
2
Diode 1N4001
1
LM7805 Voltage regulator
1
ESP32
Tools and machines
1
Soldering kit
Apps and platforms
1
Arduino IDE
Project description
Code
Code
cpp
..
1#include "EEPROM.h" 2#include "BluetoothSerial.h" 3#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) 4#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it 5#endif 6BluetoothSerial SerialBT; 7#define EEPROM_SIZE 64 8#define pulsePine 13 9 10#define analogPin A0 11#define batLevPin A7 12int timer = 200; 13bool clientConnected = true; 14 15char dataG; 16//**************IMPORTANT******************* 17//****************************************** 18const int delayTime = 60; 19//***************************************** 20//***************************************** 21//EEPROM variables 22int addr_duty = 0; 23int addr_freq = 1; 24int stored_value; 25int duty_cycle; 26int duty_cycle_temp; 27int freq; 28int freq_temp; 29int duty_def_value = 13; 30int freq_def_value = 60; 31//Balance variables 32int value_count = 0; 33int value_count_def = 100; 34int balance_value = 0; 35int balance_value_temp = 0; 36 37//**** 38unsigned long startMillis; 39unsigned long currentMillis; 40long period = 100000; //the value is a number of microseconds 41//Measuring of level of the battery 42float resistencia1 = 100000; //Resistencia de 100K para medir la tencion (Voltios)/Resistance of 100k for test volts 43float resistencia2 = 47000; //Resistencia de 47k para medir la tencion (Voltios)/Resistance 47k for test volts 44float const arefVolt = 3.6f; //pin "3.3v" SET EXACT VALUE HERE 45float voutv; 46float vinv; 47int raw_bat_data; 48unsigned long startMillisVolts; 49unsigned long currentMillisVolts; 50long periodVolts = 1000; //the value is a number of microseconds 51int sensorValue = 0.0f; 52 53 54 55void setup() { 56 SerialBT.begin("ESP32_Spirit_PI-2"); //Bluetooth device name 57 Serial.begin(115200); 58 SerialBT.register_callback(callback); 59 60 if (!EEPROM.begin(EEPROM_SIZE)) 61 { 62 Serial.println("failed to initialise EEPROM"); delay(1000000); 63 } 64 65 66 readFromStorage(addr_duty); 67 duty_cycle = stored_value; 68 readFromStorage(addr_freq); 69 freq = stored_value; 70 if(duty_cycle == 255){ 71 writeToStorage(duty_def_value,addr_duty); 72 readFromStorage(addr_duty); 73 duty_cycle = stored_value; 74 } 75 76 if(freq == 255){ 77 writeToStorage(freq_def_value,addr_freq); 78 readFromStorage(addr_freq); 79 freq = stored_value; 80 } 81 82 83 84 85 pinMode(pulsePine, OUTPUT); 86 87 88} 89 90 91void loop() { 92 93currentMillis = micros(); 94 currentMillisVolts = millis(); 95 96 if(SerialBT.available()>0) 97 { 98 dataG = SerialBT.read(); 99 setDutyAndFreq (dataG); 100 101} 102 103 104 105 106if (currentMillis - startMillis >= period && clientConnected) 107 { 108 109 period = 1000000 / freq; 110 // Serial.println(period); 111 digitalWrite(pulsePine, HIGH); 112 duty_cycle_temp = duty_cycle * 10; 113 delayMicroseconds(duty_cycle_temp); 114digitalWrite(pulsePine, LOW); 115 // sensorValue = analogRead(analogPin); 116delayMicroseconds(delayTime); 117sensorValue = analogRead(analogPin); 118sensorValue = sensorValue / 10; 119 120sendData(); 121 122 123startMillis = currentMillis; 124 } 125 // Lectura voltios 126 if (currentMillisVolts - startMillisVolts >= periodVolts) 127 { 128lecturaVoltios(); 129//Serial.println("Lectura voltios"); 130startMillisVolts = currentMillisVolts; 131 } 132 133} 134 135 136 137 void writeToStorage(int valor,int addr) 138 { 139 EEPROM.write(addr, valor); 140 EEPROM.commit(); 141 142 143 } 144 int readFromStorage(int addr) 145 { 146 stored_value = EEPROM.read(addr); 147 148 return stored_value; 149 150 } 151 void setDutyAndFreq (char valor) 152 { 153 //"n" valor para aumentar duty cycle 154 //"m" valor para disminuir duty cycle 155 //"j" valor para aumentar la frequencia 156 //"k" valor para des,inuir la frequencia 157 //"+" valor para aumentar el balance 158 //"-" valor para desminuir el balance 159 if(valor == 'n') 160 { 161 // Serial.println("n Recived"); 162 readFromStorage(addr_duty); 163 duty_cycle = stored_value; 164 duty_cycle = duty_cycle + 1; 165 writeToStorage(duty_cycle,addr_duty); 166 167 } 168 else if(valor == 'm') 169 { 170 // Serial.println("m Recived"); 171 readFromStorage(addr_duty); 172 duty_cycle = stored_value; 173 duty_cycle = duty_cycle - 1; 174 writeToStorage(duty_cycle,addr_duty); 175 176 } 177 else if(valor == 'j') 178 { 179 // Serial.println("j Recived"); 180 readFromStorage(addr_freq); 181 freq = stored_value; 182 freq = freq + 10; 183 writeToStorage(freq,addr_freq); 184 185 } 186 else if(valor == 'k') 187 { 188 // Serial.println("k Recived"); 189 readFromStorage(addr_freq); 190 freq = stored_value; 191 freq = freq - 10; 192 writeToStorage(freq,addr_freq); 193 194 } 195 else if(valor == 'p') 196 { 197 // Serial.println("m Recived"); 198 199 writeToStorage(0,addr_freq); 200 writeToStorage(0,addr_duty); 201 202 } 203 204 205 206 207 } 208 //Volt function 209void lecturaVoltios(){ 210 vinv=0.0f; 211 voutv=0.0f; 212 for (int i=0;i < 10;i++){ 213 214 voutv = (analogRead(batLevPin) * arefVolt) / (4095); //Lee el voltaje de entrada 215 216 vinv += ( (resistencia1 + resistencia2)* voutv) / resistencia2 ; //Fórmula del divisor resistivo para el voltaje final 217 if(vinv < 0.9){ 218 vinv=0.0f; 219 } 220 221 222 223} 224vinv = vinv/10; 225 226 227} 228void sendData() 229 230 { 231 /* Serial.print("<"); 232 Serial.print(sensorValue); 233 Serial.print("/"); 234 Serial.print(freq); 235 Serial.print("/"); 236 Serial.print( duty_cycle); 237 Serial.print("/"); 238 Serial.print( vinv); 239 Serial.print(">"); 240 Serial.println();*/ 241 String dataG = "<"; 242 dataG +=sensorValue; 243 dataG +="/"; 244 dataG +=freq; 245 dataG +="/"; 246 dataG +=duty_cycle; 247 dataG +="/"; 248 dataG +=vinv; 249 dataG +=">"; 250 /* bluetooth.print("<"); 251 bluetooth.print(sensorValue); 252 bluetooth.print("/"); 253 bluetooth.print(freq); 254 bluetooth.print("/"); 255 bluetooth.print( duty_cycle); 256 bluetooth.print("/"); 257 bluetooth.print( vinv); 258 bluetooth.print(">");*/ 259 SerialBT.println(dataG); 260 Serial.println(dataG); 261 } 262 void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){ 263// Callback function implementation 264 if(event == ESP_SPP_SRV_OPEN_EVT){ 265 Serial.println("Client Connected"); 266 clientConnected = true; 267 } 268 269 if(event == ESP_SPP_CLOSE_EVT ){ 270 Serial.println("Client disconnected"); 271 // SerialBT.flush(); 272 // SerialBT.end(); 273 clientConnected = false; 274 delay(1000); 275 ESP.restart(); 276 277 } 278}
Downloadable files
Schematic
..
Kicad export schematic.jpg

Comments
Only logged in users can leave comments