Today we’ll be making a simple circuit to detect whether a blackout has occurred and when it has we can be alerted, it should be non-contact so we don’t need to plug it into a power point.
From my previous projects I’ve noticed that the ADC in MCUs can be quite sensitive, if you leave the ADC ungrounded and do a reading it fluctuates. When you connect a floating wire and then touch the wire or move it around you can see even more fluctuations.
We can use this to our advantage, by placing a high value resistor like 1 Megaohm to ground then when some interference is detected it will fluctuate from reading 0 (ground). After some testing, it’s best to read the ADC multiple times in a short amount of time rather than just reading once and relying on that reading. We’ll be using it to detect AC interference by placing it near power points or power cords.
We can use some wire in a small coil like fashion to help pick up more interference.
Here’s our schematic.
#define ledPin (1<<PB0) // LED #define adcPin 3 // Analog input 3 (PB3) ... DDRB |= ledPin; // Set outputs PORTB |= ((1<<PB4) | (1<<PB2) | (1<<PB1)); // Turn on pull-up resistors on other ports to save power int timeOff = 0; // Uncomment the line below to test for interference sources, LED will light when interference is detected. //testInterference(); while(1) { int adcValue = 0; int x = 0; while (x < 10 && adcValue == 0) { // Try to get an ADC reading of the AC interference _delay_ms(5); // Wait a little while before ADC reading adcValue = analogRead(adcPin); x++; } if (adcValue == 0) { // No AC interference detected this run timeOff += 2; // Increment 2 seconds } else { timeOff = 0; } if (timeOff >= 10) { // No AC interference detected for more than 10 seconds PORTB |= ledPin; watchdogSleep(T250MS); PORTB &= ~ledPin; watchdogSleep(T250MS); } watchdogSleep(T2S); }
In the code, we first read the ADC 10 times in a row. If the ADC isn’t 0 any time in the loop then we break out of the loop. If no interference is detected we increase the time that the power has been off by 2 (because we’ll delay 2 seconds later on). If the power off time has been more than 10 seconds we start blinking the LED and then we sleep for 2 seconds.
In the video above, the first half we show the LED turning on when interference is detected so we know it’s working and then in the second half we show the LED turning on when no interference is detected for 10 seconds (power goes out).
Download v1.0: Non-Contact_Blackout_Detector_v1.0
[…] from insideGadgets had an interesting project last year on non-contact AC detection and I wanted to try it out and learn how it works. He hooked one leg of a 1M ohm resistor to GND […]