MinuteMaid is a simple programmable timer to control a mains-voltage appliance, with direct power supply (it powers itself from the mains without using a transformer). I couldn’t find exactly what I wanted so here’s a workable solution to this problem. It’s all documented after the usual:
The information and methods described herein are provided “AS-IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies, that could be damaging to your devices. Proceed with caution, and although it is highly unlikely that accidents will happen because of following advice or procedures described in this document, the author does not take any responsibility for any damage claimed to be caused by doing so.
I’ll add an extra one since this particular design runs from and drives mains voltages:
BE VERY CAREFUL: POTENTIALLY LETHAL VOLTAGES ARE PRESENT IN THIS DEVICE WHEN IN USE.
This device is a proof of concept that presents several safety risks. PLEASE READ THE SAFETY NOTES CAREFULLY.
My design goals were relatively simple:
- KISS design
- Small footprint
- Can be built “by hand”
- Must run from mains, no separate PSU, with very low standby power drain
- Must be programmable
- Must be very easy to use (single button operation)
- Must handle up to 10A loads
- Must be reasonably safe (who would have thought?)
Here’s the schematic of this design:
This design requires some explanation; unusually, I won’t explain from top to bottom, but instead I’ll start with the signal-level logic.
Note: design and calculations are based on mains voltage of 230VAC@50Hz. To operate from 110VAC@60Hz, this design will need minor changes in some components values. All calculations are detailed and explained, and based on RMS values for AC.
This designs uses a MCU which is interfaced via a single push-button for operation, one LED for visual feedback, a jumper for in-field tunability, and a power-relay to control the main load. This combination allows for a variety of use cases, since the whole control logic is under the MCU’s supervision. An example program is provided below.
The brains of this design is a PIC10F200, which was chosen because it fits the bill in terms of costs, ability to run from a wide range of voltages, sufficient number of I/O, power usage and availability. Given the simplicity of the program I intend to run on it, I didn’t feel necessary to upgrade to the 202 version, but if your control logic requires it, it’s a drop-in replacement with double the memory size. This MCU offers 4 Input-Output pins, plenty enough for the design.
The low-voltage parts of the circuit operate on two distinct voltages: 24V for the “output logic” (relay and LED operation), and 5V for the “input logic” (MCU operation). Practical considerations require to keep the current drain of the control system as low as possible, within a few milliamps, especially on the +5V rail, for reasons that will become obvious later.
Due to the above power considerations, typical NPN transistors (BC547) are used in a switching configuration to control the LED and relay coil. The LED switch must drive a typical maximum of 20mA Ic, likewise for the coil (assuming 500mW coil in the worst case scenario, though this design uses a 250mW coil). Assuming a worst case transistor gain (hFE) of 100, this means that the base current required for the transistor to drive enough collector current is 0.2mA (assuming non-saturation). The suggested 10kΩ bias resistors R8 and R9 deliver 0.5mA from the 5V rail, which is plenty enough to ensure adequate switching.
Ballast resistors R10 and R11 provide two current levels for driving the LED, allowing for two levels of brightness, which is useful to convey information: under “low” level (through R11 only), the circuit is powered and in standby mode. When the PIC drives GP0 (pin 5) high, T1 switches on and the equivalent resistor driving the LED drops to about 2kΩ: the LED becomes much brighter. If preferred, R11 can be entirely ommited to have a classic “on/off” effect.
Note: the suggested resistor values provide a LED current of about 1mA in the low state and 12mA in the high state, which worked nicely with the yellow LED I used, while keeping current drain at very low values. You may want to experiment for your particular LED type. I’ve generally noticed that a 1:10 ratio from high to low gives good results, and that most LEDs will run at half their rated current with no significant loss in light output. If you don’t have a 24VDC supply to test from, you can experiment from 5VDC and then multiply the resistor values by 5, choosing the closest match in the standard series.
The push button is connected to the PIC’s GP1 (pin 4) via a 10kΩ pullup resistor (R7), and a 1kΩ pulldown resistor (R6). This configuration ensures a maximum current drain of about 0.5mA when the button is pressed, which is acceptable given the momentary nature of the button operation. Switch debouncing is done in software.
The jumper is connected to GP3 (pin 8) via a 47kΩ pullup (R4) which limits current drain to 0.1mA when the jumper is bridged (the higher value reflects the fact that the jumper is more likely to be in a permanent position). R5 ensures nothing bad happens if GP3 is driven as an output by the PIC and C5 ensures voltage level stability given the very high impedance of this section (not to mention that the PIC has its own pullup resistors).
Control power requirements
The above allows us to compute the maximum power usage of the control logic, which will be necessary for the next step:
- For the 5V rail, about 3mA:
- 0.1mA (jumper bridged)
- 0.5mA (button pressed)
- 1mA for the PIC itself
- 0.5mA for each of the BC547 triggers
- For the 24V rail, about 22mA:
- 12mA (LED “high” current)
- 10mA (250mW relay coil)
We can thus see that the design requires about 25mA to run, which we’ll round up to 30mA to have some leeway. This is where we can start explaining the leftmost part of the schematic.
This section provides adequate power to the logic circuit via two low voltage rails, powered straight from the mains voltage via a transformerless capacitive power supply. This type of power supply is current-driven, so everything must start with the desired output current: we are using the impedance of capacitor C1 to lower the mains voltage to a workable level, which we then rectify with a classic rectifier bridge (D1-D4, 1N4004) and clamp to 24V with a Zener diode (D5). So, we want to determine the value of C1 so that - while drawing about 30mA - we can lower the mains 230V down to about 30V (the rectifier bridge adds another another 1.4V offset on top of the 24V Zener - and 30mA through R2 will induce another 4.5V drop). Ignoring phase relations, we have:
Z = 1/(2πfC)
where Z is impedance in Ohms, f is frequency in Hertz, and C is capacitance in Farads. We also know that I=U/Z with I in Ampères, U in Volts and Z in Ohms.
We know U (230-30 = 200V), we know our target I (30mA), we want C. It comes:
Z = U/I
1/(2πfC) = U/I
C = I / (2πfU)
Inserting our target values, we find:
C = 0.03 / (2π * 50 * 200)
C ≃ 477nF
Choosing the closest value in the standard series gives us a 470nF capacitor. This capacitor must be a Class X2 device. With a +/- 20% capacity tolerance, the resulting maximum available current will be between 24 and 35mA (at 230V nominal), which should be “just” fine given our margins. If necessary it is always possible to reduce the “high” LED current to match the enveloppe. R1 is a bleeder resistor, used to drain the capacitor when power is removed (the capacitor could otherwise expose nearly the full mains voltage at its poles, which could be very dangerous). 1MΩ will ensure a speedy discharge of the capacitor: the time constant of this pair is 0.47s, it will take about 1 second for the capacitor to discharge to a safe value (24V) even if the rest of the circuit doesn’t draw any power. Leakage is limited to about 5mW. This resistor must be rated for mains voltage (standard resistors are typically rated for 50V).
R2 is a used as a soft-starter to limit transient current spike at power on (when the capacitor is entirely discharged, if plugged at the peak of the AC voltage swing it would potentially let the initial voltage spike through and this would in turn generate a current inrush spike in the rest of the circuit). Of course, real life components are not perfect, so inrush current will be finite anyway, but adding this resistor will ease the stress of the components in the circuit, in particular that of the D5 Zener, as well as damp mains transient spikes (to which the capacitor is mostly transparent). The value of this resistor should be low enough to be negligible in front of the capacitor impedance (we don’t want it to take a significant part in the voltage drop and waste power) but high enough to sufficiently reduce the initial spike. A good rule of thumb appears to be R = 3 / I, where I is the steady state maximum current. It should also be able to sustain the power dissipation due to the current that flows through it (the total current consumed by the circuit). To be on the safe side, I recommend using a 1/2W resistor here, though 1/4W should work. Two 1/4W resistor of double the designed value can be used in parallel to achieve the same effect. The transient inrush current can mostly be ignored in terms of power dissipation, as that transient will last for less than a cycle and the resistor can accomodate such a very high burst provided it lasts a very short amount of time (a few milliseconds).
The beauty of using a capacitor instead of a resistor is in the very low amount of active power wasted (as heat), since in the capacitor, voltage and current are out of phase. The capacitor impedance is also essentially dependent on mains frequency, and is thus stable and predictable. This setup is fairly resilient to mains voltage variations: from 220V to 240V the current variation is less than 3mA, thus the circuit should operate under most conditions. This type of design is only practical with low current applications, and it works well in the present case.
Of course, on the ugly side we have the power factor which is abysmal, the efficiency which is rather awful and the EMI radiation which is nothing to be proud of! But it’s an acceptable tradeoff given the practicality and cost of this design, and given the very low power used compared to the controlled load (expected to be in the kW range).
D5 ensures that the voltage never goes above 24V, and will in the process sink the excess current not consumed by the rest of the circuit. It is important to understand that this design works reliably because of D5: D5 clamps the voltage drop across the complex impedance formed by C1 and R2. This is why a constant amount of current flows through this impedance, and the excess current not consumed by the remaining parts of the circuit must be dissipated by D5. Thus, proper calibration of C1 and R2 with respect to D5 is very important. As a reminder, the maximum theoretical current a Zener diode can sink is I = P/U. This 24V, 1.3W diode can thus sink a maximum of about 54mA before being destroyed. Our absolue worst case (approx 40mA if on the high end of capacity tolerance and mains voltage) falls within safety limits (assuming no part of the circuit consume any current, which will never be the case). C2 smoothes the output of D5 (during the wave cycle, the voltage coming from the rectifier oscillates). The relay and LED don’t need very stable voltages to operate properly, so this design yields satisfying results in that context.
We then have another Zener, D6 and its accompanying smoothing caps (C3, C4) to provide a stable and smooth 5V (5.1V actually) rail (smoother and more stable than the 24V rail, in fact). The value of the buffer resistor R3 separating the 24V and 5V is infered from the maximum expected current drain on the 5V rails, which we round up to 5mA: we have a voltage drop of 24 - 5 = 19V, and we want to limit the current to 5mA (rounded). U=RI gives us:
R = 19/0.005
R = 3800Ω
The closest value in the standard series is a 3.9kΩ resistor. The excess current will be dissipated (wasted) by D6, which is why it’s relatively important to properly calibrate R3.
Remaining bits of interest
This pretty much sums it up. D7 is a typical flyback diode, to protect the circuit from coil kickback. VR1 is a varistor used to protect the relay from kickback from inductive loads (the same phenomenon that the flyback diode protects against, except since this part of the relay drives alternating current under mains voltage, a single diode is not suitable and a TVS is overkill). F1 is a polyfuse, which will trigger if anything goes wrong in the circuit. K1 is a 250mW, 24VDC power relay rated for 10A under 240VAC. This enables driving 2kW loads, which covers a useful range of applications.
By design, the “power supply” of this design will not provide a very stable voltage, especially at the onset. It will take time to converge towards the expected +24V and +5V, but it nonetheless reaches operational levels in about 0.5s.
Here’s a very simple one-sided, hand-routed layout that fits on credit-card-sized PCB.
- The Solder Side (printed side against the board)
- The Silkscreen (for parts placement)
- The Part list.
This PCB will nicely fit in a 90x90mm junction box. I highly recommend that the mains be connected via a fuse holder between mains “Live” source (coming from the wall socket) and the PCB’s “L” pin. This fuse holder will hold a 10A fuse, to protect the relay and the user should anything go wrong. Once again, C1 must be Class X2, rated 400VAC or more. These capacitors are designed to ensure safety in the event of a catastrophic failure. R1 is meant to be mounted under the capacitor (these large caps can normally accomodate a small resistor between their base and the PCB: there is enough clearance). The polyfuse F1 should ideally be mounted “raised” above the PCB surface for optimal cooling (this really is more about nitpicking than anything, though :).
Due to the potentially significant current flowing in the L and Lout pins and circuit tracks, I recommend that a very generous amount of solder tin be layered over both those tracks from the plug to the lugs of the relay.
There is no mention of GND on the schematic, but instead there is a V0 reference. This is because this device DOES NOT use an isolated power supply, and this has several consequences:
- NEVER TOUCH ANY PART OF THE CIRCUIT WHEN CONNECTED TO MAINS POWER,
- ONLY use this device INSIDE an electrically isolated container, such as a plastic junction box,
- ALWAYS use an electrically isolated switch rated to match MAINS voltage for the push-button.
DANGER: if the Neutral/Live 230V input connections are inverted (e.g. due to the misorientation of the input plug in the socket), the following will happen:
- The low voltage section of the circuit WILL FLOAT ABOUT 200VAC above actual 0V ground reference level,
- The controlled appliance will be PERMANENTLY CONNECTED TO THE LIVE POLE, EVEN WHEN TURNED OFF.
The device and appliance will still operate correctly, but the risk of electrical shock is increased.
In any case, this bears repeating: NEVER TOUCH ANY PART OF THE CIRCUIT WHEN IT IS CONNECTED TO MAINS. This device is only proposed as a proof of concept and should not be used by inexperienced personel.
This design works well, and is quite versatile, provided that one writes an adequate program for the PIC. Here’s a sample program that will monitor the button, and latch the relay for 6h (or 12h if the jumper is bridged) when the button is first pressed. If the button is held for more than about 1s during the latch period, the relay will be released and the device will return to its idle state. The C program is self explanatory and can be easily converted to other languages. MPLAB’s xc8 compiler generates relatively suboptimal code, but I also provide a slightly optimized version in assembly. Both programs do exactly the same thing.
Sample timer program
Pictures of a working prototype
I used an illuminated switch (and a slightly different PCB layout).