Mains frequency meter
Inspired by but not really based on the Time for Tea artwork. My take on it uses a edge-view meter movement originally calibrated for a 4-20 mA loop system and a PIC. It's mounted in an old cigar box and reads from 49.8 to 50.2 Hz with an overrange and underrange indicator.
All images on this page are low resolution versions. Click them for bigger versions.
The C code for this project is in a single file here, for online reading there's a pretty highlighted version generated by http://hilite.me/. On startup it parks the meter at each end of the scale and in the middle so that the meter movement can easily be setup. The meter is mounted upside down due to space constraints therefore the low frequencies at the left-hand side actually correspond to maximum PWM from the PIC. One end of the scale is set with the mechanical zero and the other by adjusting the pot.
In the main loop it blinks an LED at 1/64th of the mains frequency to confirm it's alive, measures each mains cycle and performs a simple rolling average that contains 1/2 of the most recent value, 1/4 of the previous, 1/8th of the one before that, etc. It then rescales for a narrow range around 50Hz and writes it to the PWM output. In use it was found that the meter had a lot of fast movement tracking small cycle-by-cycle changes and a 1000 uF capacitor was added directly accross the meter movement to smooth these out.
The power supply to the PIC is derived from a small 7.5V transformer and a linear regulator. The -ve of this floating supply is connected directly to mains neutral. RA4 is used as the frequency input. It is connected to mains live via a series string of high-value resistors and a clamping arrangement. This give a much sharper rising edge than clocking from the low voltage transformer output. The original plan had been for a capacitive dropper type power supply but the 10 mA full-scale meter movement largely ruled that out. The PIC is clocked at 4 MHz with a crystal giving a 1 us instruction cycle.
My notes from the time do not include all the component values but I have added in what I consider to be sensible values.