Detecting AC Voltage Using Arduino

AC voltage detector is used as a sensor whether there is a voltage of 220 VAC coming into the circuit (just for detecting, not for measuring). These detectors are widely used for controlling system-AC generator in a building, commonly called AMF (Automatic Mains Failure). AMF usually sold expensive, but this time I will try to make simple AMF with the Arduino, which would certainly reduce away cost of production. Because of still in the research progress, this article will only discuss about the AC voltage detection instead of AMF making. In the AMF system anyway, if there is AC voltage is detected off, the system will automatically turn on the generator after a few seconds.

Detecting AC Voltage Using Arduino Materials

Okay, please prepare AC voltage sensor first, i.e. optocoupler H11AA1. Previously, I’ve discussed all about required components, the circuit and how build AC voltage detector system. This time I will explain about how to handling logic of the detector/sensor output using Arduino as controller. Materials needed for experiments are : AC Voltage detector using optocoupler H11AA1 and Arduino IDE (Integrated Development Environment) software. If all materials are ready then upload the following sample skecth.

/ *
Detecting AC Voltage Using Arduino
 * /
#define ACsensor 2
#define LED 13

ACstatus int = 0;

void setup () {
    pinMode (ACsensor, INPUT);
  // Initialize the LED as an output:
  pinMode (LED, OUTPUT);
  // Initialize serial communication:
  Serial.begin (9600);

void loop () {
  // Checking ACStatus
  delay (300);
if (digitalRead (ACsensor) == LOW && (ACstatus == 0)) {
 Serial.println ( "AC Voltage ON");
 digitalWrite (LED, HIGH);
 ACstatus = 1;
else {
  if (digitalRead (ACsensor) == HIGH && (ACstatus == 1)) {
  Serial.println ( "AC Voltage OFF");
  digitalWrite (LED, LOW);
  ACstatus = 0;

If you've copied sketch above into Arduino IDE, then upload to Arduino immediately (press 'Upload' button on toolbar or press Ctrl + U shortkey). If you've managed to upload the sketch, now the result is visible to be monitored. To see the results, there are two ways, it can be seen from internal LED built in Arduino board (connected to pin 13) or from Serial Monitor window (you can access it via menu Tools | Serial Monitor). If you use a way of looking at LED pin 13, it will flashing if AC voltage is ON and vice versa if AC voltage is OFF, LED will turns off. If you’re using Serial Monitor, there should be the status of AC voltage ON or OFF will be displayed there. If there is no display any value, please check your serial communication baudrate located in the lower right corner of Serial Monitor window). Baudrate 9600 bps figures should be in accordance with the settings in the sketch above. Please do not forget, the output from the image sensor circuit in the previous article, must be connected with pin no 2 Arduino, according to the sketch initialization in the example above. So the need to consider is the following program lines

#define ACsensor 2 => pin 2 is connected to the Arduino input and sensor output
Serial.begin (9600); => serial communication baudrate settings

Already finish. It’s fun 'playing' with the Arduino, right? Simple and fast development.
Next Post »