Skip to content

Instantly share code, notes, and snippets.

@carlosefr
Last active February 17, 2023 00:36
Show Gist options
  • Save carlosefr/dbd5ff54f216aba5999dafe965c180c4 to your computer and use it in GitHub Desktop.
Save carlosefr/dbd5ff54f216aba5999dafe965c180c4 to your computer and use it in GitHub Desktop.
Simple electromagnetic field detector with an Arduino
/*
* Pin A0 -> 2MΩ resistor -> GND
* Pin A0 -> antenna (a piece of wire a few centimeters long)
*
* Pin 11 -> LED -> 330KΩ resistor -> GND
*/
static const char sensorPin = 0;
static const char ledPin = 11;
static const char samples = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int sense = 0;
for (int i = 0; i < samples; i++) {
sense += analogRead(sensorPin);
delay(2);
}
sense /= samples;
sense = constrain(sense, 1, 100);
sense = map(sense, 5, 100, 0, 255);
analogWrite(ledPin, sense);
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment