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;


  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

    1. wattnotions

      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.

      1. Ted

        Thanks for writing back. I’m going to use a PICAXE as the micro controller; the code
        with be much simpler then the ‘C’ above. Great project!!

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

  4. uminded

    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

    1. wattnotions

      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

  5. Pingback: Mains Power Detector For A Thing For Internet | Ad Pub

    1. wattnotions

      Only had a few resistor sizes available so I modeled the circuit in LTspice and experimented with different values of resistors until I got a reasonable output voltage of a few volts. I then estimated the gain by dividing the output voltage (around 4V) by the input voltage ( around 20-25mV) and that gives a value of between 160-200. Hope that answers your question but if it doesn’t, if you could tell me exactly how you are calculating the gain we might be able to figure out why we get different answers.

      1. Sam

        I’m using the standard calculation for calculating gain in an instrumentation amp but I think my trouble might be coming from the fact that the two R3′s are not equal. Av = Vo/V2-V1 = (1+2R1/Rg)(R3/R2) where R1=10K, Rg=260, R2=10K and R3 is 3M and 69K. I get the same gain that you get in the first part but then they are not equal for the second part using either of the R3 values.

Leave a Reply

%d bloggers like this: