We are now going to dive into the core of this project and configure the project so that it can detect EMF activity around the antenna.
The following is the complete code for this project:
// Required libraries #include <Wire.h> #include <LiquidCrystal_I2C.h> // Number of readings #define NUMREADINGS 15 // Parameters for the EMF detector int senseLimit = 15; int probePin = 0; int ledPin = 7; int val = 0; int threshold = 200; // Averaging the measurements int readings[NUMREADINGS]; int index = 0; int total = 0 int average = 0; // Time between readings int updateTime = 40; // Create LCD instance LiquidCrystal_I2C lcd(0x27,20,4); void setup() { // Initialise LCD lcd.init(); // Set LED as output pinMode(ledPin, OUTPUT); // Print a welcome message to the LCD lcd.backlight(); lcd.setCursor(0,0); lcd.print("EMF Detector Started"); delay(1000); lcd.clear(); } void loop() { // Read from the probe val = analogRead(probePin); Serial.println(val); // Check reading if(val >= 1){ // Constrain and map with sense limit value val = constrain(val, 1, senseLimit); val = map(val, 1, senseLimit, 1, 1023); // Averaging the reading total -= readings[index]; readings[index] = val; total += readings[index]; index = (index + 1); if (index >= NUMREADINGS) index = 0; average = total / NUMREADINGS; // Print on LCD screen lcd.setCursor(0,1); lcd.print(" "); lcd.setCursor(0,0); lcd.print("EMF level: "); lcd.setCursor(0,1); lcd.print(average); // Light up LED if EMF activity detected if (average > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } // Wait until next reading delay(updateTime); } }
Now let's see the details of this code. It starts by including the required libraries:
#include <Wire.h> #include <LiquidCrystal_I2C.h>
Then, we define the number of readings that we want to do at every iteration in order to make sure that we get the average EMF reading:
#define NUMREADINGS 15
We will then define some parameters for the project, such as the different pins on which the components are connected:
int senseLimit = 15; int probePin = 0; int ledPin = 7; int val = 0; int threshold = 200;
After that, we will define some variables that will be used to take the average of the readings:
int readings[NUMREADINGS]; int index = 0; int total = 0; int average = 0;
We will also set an update time that we will leave between each EMF reading. By default, we set it to 40
milliseconds:
int updateTime = 40;
We will also create an instance of the LCD screen:
LiquidCrystal_I2C lcd(0x27,20,4);
After that, in the setup()
function of the sketch, we will initialize the LCD screen:
// Initialise LCD lcd.init(); // Set LED as output pinMode(ledPin, OUTPUT); // Print a welcome message to the LCD lcd.backlight(); lcd.setCursor(0,0); lcd.print("EMF Detector Started"); delay(1000); lcd.clear();
Then, in the loop()
function of the sketch, we will get the value of the analog pin on which the antenna is connected:
val = analogRead(probePin);
After that, we will constrain the reading to the limit value that was defined previously, and then map it again between 1
and 1023
:
val = constrain(val, 1, senseLimit); val = map(val, 1, senseLimit, 1, 1023);
Then, we will take an average of the readings:
total -= readings[index]; readings[index] = val; total += readings[index]; index = (index + 1); if (index >= NUMREADINGS) index = 0; average = total / NUMREADINGS;
Finally, we will display the average on the LCD screen and also light up the LED if the average reading is higher than the threshold:
lcd.setCursor(0,1); lcd.print(" "); lcd.setCursor(0,0); lcd.print("EMF level: "); lcd.setCursor(0,1); lcd.print(average); // Light up LED if EMF activity detected if (average > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
We will also wait for a given amount of time between each reading:
delay(updateTime);
Note that you can find the complete code in the GitHub repository of the project at https://github.com/marcoschwartz/arduino-secret-agents.
It's now time to test the project. You can copy the complete code and paste it into your Arduino IDE, or just get it from GitHub.
Then, upload the code to your project. After a while, you should see that the LCD screen is being initialized and is blank. This is because it is waiting to detect a significant EMF activity.
I, for example, approached my phone with the antenna and got the following result:
If you can see something on your screen, congratulations, you just built an EMF bug detector! Now, I invite you to try with other devices, such as a surveillance camera, any Wi-Fi device, a radio controller, and so on.
3.135.247.181