How to tell if mains voltage is on with microcontroller

arduinomainsvoltage

I want to be able to tell if the mains voltage is on or off with an Arduino.

How could I do this safely with minimal components (or components from Radio Shack)?

Best Answer

The simplest way I can think of is to buy a 5V "wall-wart" power transformer.

Simply connect the output of the 5V power supply to one of the arduino input pins, and the grounds from each device together.

I would recommend placing a resistor in series with the 5V input to the arduino, so if you accidentally set the arduino pin as an output, it wouldn't "fight" the power supply (which could damage the arduino). Something like 1-10KΩ should be fine.


One thing to be aware of is not all wall-wart power transformers are regulated.

Basically, some wall-warts have ICs in them that make sure they always output the same voltage. Other have no ICs for regulation, and as such, the output voltage varies depending on the amount of current you are drawing from the power supply.

Realistically, either type would work fine. However, if you purchase an unregulated 5V wall-wart, it may put out as much as 6-9V when you have no load, or a very small load (e.g. the arduino input) connected.
A simple resistor divider would suffice to make the voltage appropriate for the arduino input pins. However, it is a good idea to measure the wall-wart output voltage before you connect it to anything, to determine what kind you have.