# Sensing AC voltage using microcontroller

Sensing AC voltage using microcontroller is nothing but checking whether AC supply is present or not using microcontroller. while working with real time situations or Home automation, we come to situation where we want to know whether the AC voltage or supply is present or not. AC supply or voltage detection, you can do by using the following circuit. Where optocoupler is providing the isolation between AC supply and Microcontroller. Before understanding the working of above circuit, we will first understand , what is optocoupler?
Optocoupler:
Optocoupler is also known as Opto-isolator. It’s use to provide isolation between two circuits. Opto coupler consists of LED and Photo transistor , shown below. Output of optocoupler is High when input supply is not present and Output of optocoupler is LOW when input supply is present . Selecting R1 and R2 values:
R1 value selection:
R1=(230-0.7)/IF
where 0.7 V = voltage drop across LED
IF= Forward current (Current through LED when AC supply is present) , you will get value of this from datasheet. For optocoupler 4N35 , maximun  value of Forward current  is 50mA.
Value of R1 is calculated for IF of 1mA
R1=(230-0.7)/1mA
R1=229.3 Kohm
R2 value selection:
R2=(Vdc-0.3)/IC
Vdc-Supply voltage of microcontroller.
0.3 V – Saturation voltage of photo transistor (voltage across photo transistor when current flows through it)
IC= Collector current (Current flowing through photo transistor when light falls on it). For optocoupler 4N35 , Maximum value of collector current is 100mA.
Value of R2 is calculated for IC of 1mA
R2=(5-0.3)/1mA // Vdc is taken as 5 volt
R2=4.7Kohm

In below circuit output  of optocoupler is connected to I/O pin of microcontroller and ground of optocoupler is connected to ground of microcontroller. Configure I/O pin of microcontroller as input pin.

Case 1: When AC supply is present
Whenever 230 volt is present at the input of optocoupler, output of it would be LOW and microcontroller will receive logic 0 as input. whenever microcontroller will receive logic 0 means 230 volt or AC supply is present. Case 2: When AC supply is absent
Whenever 230 volt is not present at the input of optocoupler, output of it would be HIGH and microcontroller will receive logic 1 as input. whenever microcontroller will receive logic 1 means 230 volt or AC supply is not present. ### 2 Responses

1. Dalton Vono says:

Hi! Did you try this circuit? Normally, the maximum reverse voltage at a led of an optocoupler is around 5, 6 Volts. The negative semi cicle of AC line will damage the optocoupler!!

• admin says:

Hey thanks for the comment. I have tried this circuit. Do not forget current limiting resistor is also connected in the series of opto coupler. So AC line will not damage the opto coupler.