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;

This entry was posted in Hardware, Ideas, Microcontrollers, pic microcontrollers and tagged , . Bookmark the permalink.

15 Responses to 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

  3. Ted says:

    What values would be needed for 60Hz? Also, what Op-Amps are you using, if that’s what they are

    • wattnotions says:

      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.

  4. Ted says:

    Ignore the Op-Amp Question! derrrr!!!

  5. Ted says:

    What’s the value of the Peak Detector, (Diode0Cap)?

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

  7. uminded says:

    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

    • wattnotions says:

      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

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

  9. Sam says:

    how are you calculating your gain? I am not coming up with the same numbers that you have in the video

    • wattnotions says:

      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.

      • Sam says:

        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