Mains power detection circuit

Circuit Diagram

Circuit Diagram

RLC circuit

RLC circuit

RLC circuit

Breadboard circuit

Code used for microcontroller:

The uC used was a pic18f14k50. Code compiled using xc8 compilter. Read analog channel function taken from batchloaf’s template code post. Here is the source code:

#include <xc.h>
#include <stdio.h>
#include <delays.h>


void setup(void);
unsigned int read_analog_channel (void);


int voltage;

void main(void)
		if(read_analog_channel() > 310) LATCbits.LATC4 = 1;
		else LATCbits.LATC4 = 0;
void setup(void)
    OSCCON = 0b01100011; //internal oscillator block, 8MHz clock speed.
    TRISC = 0b00000001; 
    TRISB = 0b00000000;
    LATB = 0b00000000;
    LATC = 0b00000000;
    ADCON2 = 0b00000011;// a/d conversion clock (dedicated clock)
    ANSELbits.ANS4 = 1; // front right ir 
    ADCON0 = 0b00010000; // chose an4 analog channel

unsigned int read_analog_channel (void)
    voltage = 0;
    ADCON0bits.ADON = 1;
    Delay1TCY();// 8us Delay for capacitor charge
    ADCON0bits.GO = 1;
    while(ADCON0bits.GO); // wait for conversion to complete
    voltage = ADRESH;
    voltage = (voltage<<2) + (ADRESL>>6);
    return voltage;

11 thoughts on “Mains power detection circuit

  1. Pingback: Mains Power Detector For A Thing For Internet

  2. Pingback: Mains Power Detector For A Thing For Internet - Tech key | Techzone | Tech data

    • Hi Ted, You could use a 10mH inductor with a capacitor of around 700uF. Or you could use a 7mh inductor with a 1000uF capacitor, there are loads of other possible combinations also. This calculator is very useful : . In response to your other comment, the peak detector capacitor is 10uF and the diode is a plain old IN4001. If the output of the peak detector is too choppy a resistor (around 10k should do it) can be placed in series with the diode to slow down the rate that the capacitor discharges.

  3. Pingback: Mains Power Detector For A Thing For Internet | Hack The Planet

  4. Very interesting idea of using the inductor to pickup the magnetic field of the power cord. This was how they tuned radio coils in the day and also how “non conductive voltage testers” work. I am designing a home power monitor and this is great as your induced voltage will be proportional to the current in the mains cord.

    Only downside to using an inductor for more accurate current sensing is you would need a calibration factor and an electrician to amp-clamp each circuit to get that calibration factor. Much cheaper than the non-invasive current monitors for about $9/piece

    • The calibration factor is a problem with the idea for sure. One possible way around it is if you knew how much power the device attached to the cable draws for example a 60W bulb etc. Also, while it may not be possible to know the exact current flowing in the cable, you could tell the difference between two different states of current draw, for example a toaster with two separate toasting coils. If you turn on one coil for one slice of toast you get a certain current draw and then it jumps up to a higher value if the other coil is turned on

Leave a Reply