Introduction: AC Mains Detector Circuit

About: Make your life more lazy and awesome by the touch of electronics. Check out awesome projects and learn how to build them easily and cheaply.

I needed a circuit that can detect whether the AC power is on or not for one of my projects. A quick Google search returns nothing useful sadly, but if you change the search keywords, we find a product that looks like it might just do the job, although the looks of it didn’t match the price tag it carries. It doesn’t have many parts, only if somehow I could reverse engineer it. But a good fellow does exactly that in his webpage. It also has 2 more ways to detect AC power for a microcontroller, but I was interested in this one only as it uses much less parts, is safe and can be easily built. So, without any further ado let’s understand the circuit and build it.

Another circuit: https://www.electroschematics.com/mains-voltage-sensor/

Supplies

Please find list and link of the parts used below.

INDIA:

DB107 Rectifier - https://amzn.to/3wJPa6H

PC817 Opto-Coupler - https://amzn.to/3wK6jNx

150k 2W Resistor - https://www.electronicscomp.com/150k-ohm-2-watt-resistance

US:

DB107 - https://amzn.to/3MHOJAr

PC817 - https://amzn.to/3yTRzOY

150k 2W Resistor - https://amzn.to/3lysUYe

UK:

DB107 - https://amzn.to/3NuG44r

PC817 - https://amzn.to/3PCBiny

150k 2W Resistor - https://amzn.to/3PvRFC6

Step 1: Watch the Video!

Please watch the video that has all the steps explained in detail.

Step 2: The Circuit

Alright, let’s understand the circuit first. The 150k resistor is in series with one of the AC terminals. Its function is to drop most of the AC voltage across it, so that at the output only a couple of volts are delivered bringing the AC voltage close to what we use in DC circuits. Please see the attached picture to understand what the first stage of our circuit looks like. To calculate current, let’s assume the load is a short circuit. After using Ohm’s law, we see that the current flowing is around 1.5mA. Now we can use Ohm’s law again to calculate the voltage across the resistor which equals to around 225V in my case where the input voltage is 230V. Now we can easily calculate the open circuit load voltage which will come to 5V now after using KVL. The power rating of the resistor can also be easily calculated. This concludes the first stage of the circuit, but of course it’s not accurate. There’s a lot of things that come into play while doing AC calculations like power factor, but its good for a start.

After that, the 5V AC in converted to 5V DC using a rectifier. At the output of the rectifier, there is a 5.1V Zener diode to limit the voltage if something ever goes wrong at the input side. Now we can use this DC voltage as an input to our microcontroller. But it is not completely safe yet. The AC and DC side are not isolated, meaning if something goes wrong at the AC input side, there’s a good chance it can directly affect our DC input to the microcontroller and can blow-up the microcontroller. Also, what if you are using a 3.3V microcontroller? We cannot use this in that circumstance.

To solve all these problems, we use an opto-coupler at DC output, which work at the same voltage levels as your microcontroller. Also, it isolates the AC from DC side making it completely safe. The capacitor is for decoupling and an LED in series is to visually see if the AC input is there.

Step 3: The PCB

After creating the circuit diagram, I converted it to PCB and arranged the components in a way similar to the product I saw online. As I did not already have all the parts, I ordered them online and parallelly I ordered the PCBs through PCBWay to save time. After 7 days, I received the PCBs. As you can see it is packaged very safely and the quality of the PCBs is as expected. I gathered the components and noticed that I used wrong footprint for the screw terminal, and also, I couldn’t find MB6M rectifier, so I had to buy a different one. But it's nothing that we can’t solve with love, a bit of love is all we need.

I soldered the screw terminals somehow and it’s doing its job perfectly. At the output of the Zener diode, we measure around 5.2V. The resistor is successfully dropping most of the input voltage and at the output of the rectifier we measure only 5V, which is aligned with our expectations.

After completing the circuit, we measure nothing at the output pin, because we must apply input for it first. After soldering the headers and applying 5V to it we can measure 5V at the output header when AC power is off. This is because we have pulled the collector of the transistor inside the optocoupler high with a 10k resistor. So normally it will output whatever input you provide to it. Once the transistor turns on, the current will choose the least resistance path and we will measure 0V at the output terminal.

Step 4: Order PCBs Through PCBWay

I uploaded the gerber to PCBWay website using Quick-Order option, selected the mask color and board thickness and ordered the PCBs. I received the PCBs in 10 days. As you can see in the pictures, the quality is very good and this will make out circuit look professional.

Use the gerber file located here to order the PCBs: https://1drv.ms/u/s!AjUh8ozTC7_yoMI19bipn3XRcV9OoA?e=XJvpyX

Step 5: Done!

Hope you learned something from this Instructables. If you liked it, please share and subscribe. Till next time :)