Line Monitor

I have two computers, each with their own UPS. One UPS is an APC and the other is an Energizer. When I upgraded from Windows XP to Windows 7, the software supplied with the UPSs did not work anymore and updated software was not available. This meant that when the power fails, the UPS can no longer signal the computer to shut down before battery power runs out.

Once I figured out how to attach projects to a USB port (see my USB page), this problem became a no-brainer. A microcontroller with a UART could be used to monitor a signal derived from the power line and interact with a program running in the computer, letting it know when the power fails. After a short time the program can shut the computer down.

Line Monitor Schematic

I chose a PIC16F688 for this project because it has relatively few I/O pins and still has a UART. Interfacing it to the FTDI UB232R is simplicity itself. The USB module supplies power to the PIC. The only other thing needed is a signal indicating if there is line power or not. I used a PC817 opto-isolator driven from a wall wart power supply, connected to the PIC's RC0.

Line Monitor board top

Line Monitor board bottom

Line Monitor outsideLine Monitor inside

The photos show two monitors on one circuit board, one for each computer, sharing the line monitoring part of the circuit. The wall wart drives both optoisolators in series. The apparent 8-pin IC in the middle is actually 2 4-pin optoisolators plugged into one socket. The board slips into guide slots in the case. Three holes have been drilled to provide access to the sockets, plus three small holes in the cover for the LEDs. The only mistake I made was to use those sockets included with the UB232Rs; they pop out of the sockets too easily when you plug in a cable, so I had to secure them with some hot glue.

For this project there are two programs, both called LineMon1, one that runs in the computer (host) and the other running in the PIC (remote). The host is the master, polling the remote every 10 seconds with a one byte message (at 9600 baud). The remote replies with a one byte response, indicating that the power is up or down. LED D3 is lit as long as there is polling activity.

You need to know which COMx the project uses, so before you attach this project for the 1st time, open DeviceManager and look at Ports to see which COMx are already in use. Then after attaching the project look again to see what's new.

The host program I provide is for Windows 32-bit O/S, and should run on any modern Windows computer. (Also runs fine on Windows 64 bit O/S.) Place the executable anywhere you like on your hard drive. Then create an shortcut/icon for it in your startup folder. Edit the command line to add the COMx parameter; once you attach the USB cable, Windows will install the FTDI driver for you. Example:

Linemon1 properties


The host program displays a dialog box when monitoring. It can be minimized, which leaves an icon (lightning bolt) on the taskbar. If you click the icon, the window will reappear. When the power down message is received, this window closes and another opens. There is a countdown message, which ticks down for [timeout] seconds (the default value is 120 but you can change it anytime on the main dialog box). When the interval expires, the computer is shut down. There is a cancel button so you can end the program and shut the computer down yourself if you want. If power returns before the timer expires, the monitoring window reappears.

2015-11 Update: I have been running this project on 2 computers for many years, without a glitch. That is, until I upgraded my computers to a modern motherboard with USB 3 ports. Now occasionally when Linemon1 starts up on the PC, it fails to communicate with the remote, for no reason I can determine. I ran in to similar problems with my USB Clock, which exhibited similar problems, until I added a synchronization step to the code. Debugging Linemon1 shows no problem; all the serial I/O functions are called as expected and return no errors, but there is simply no communication (as far as the remote is concerned). I finaly added a syncronization step which will hopefully solve the problem.

Download PIC C source code for Line Monitor ( SourceBoost BoostC)

Download Host C source code for Line Monitor (MS Visual Studio 2008)
(2011-02 Updated to fix a too-early startup problem.)
(2015-11 Updated to add synchronization step, see above.)

Back to VE3LNY's PIC Project Page