If you wish to undertake this project be very careful as it uses dangerous mains voltages. You have to know how to deal with.
- Everything including the arduino is in contact with the mains. Do not plug it into the PC to program without first disconnecting from the mains. You risk destroying the PC and the Arduino.
- Do not touch any part of the assembly if it is connected to the mains: there is a danger of death.
I transformed a radio-controlled socket into a timer socket by replacing the RF part with an arduino.
The program for this project was written for an Arduino pro-mini. Its size is small enough to fit in the case. But an Arduino UNO or a nano will also suit if you have space for it. Three buttons are used to set and start a timer, displays the remaining time on an OLED screen, and controls a relay based on the state of the timer. The code also handles timer interrupts for accurate time counting and saves power by turning off unused components in the ATmega328.
What I neededI often need to leave something plugged in for a while, such as a battery charger or a fan for example. I did not find to buy a timer socket, I bought remote-controlled sockets. But often I forget to turn off these outlets. So I decided to modify one. I replaced the radio circuit with an arduino for the timer function. Three buttons and a small OLED screen allow to choose and display the time and start the countdown.
First, I had to open the housing. I removed the five screws on the back and spread all parts out on the workbench. I noticed that there was room enough to accommodate an Arduino Pro Mini that I had in stock.
So I removed all unnecessary components. I only kept the relay and the power supply part (R10, R11, C6, 4x1N4004, D3). The 100nF X2 capacitor was replaced with 220nF because the current was not enough to have 24V at the time the relay closed. The C7 capacitor, originally 100uF/35V, has been replaced by a 220uF/35V to improve DC filtering.
If you want to get it working with 120V line voltage, you may need to increase the value of the X2 capacitor to 470nF. I suggest to carry out a test with the lowest value which allows correct operation and increase it to the next standardized value. Make sure that the 24V zener (D3) does not heat up too much.
Diode D2 and 24V Zener diode D3 were moved to the soldering side because they prevented the Arduino from being installed.
All components were assembled in a "quick and dirty" manner... When the hood is closed, no one knows... The main thing is that it works safely.
I used SMD parts in some places but you can put classic through-hole components instead.
The yellow ribbons on the OLED are used to center the window cutout on the display area.
The original power supply uses capacitive reactance of a capacitor to drop the mains voltage. This topology has been preserved by adapting it for a greater current consumption. The 100nF X2 capacitor has been replaced by a 220nF X2 capacitor. Two 150 ohm resistors limit the inrush current. Four diodes form a Graetz bridge. A BZX85C24 zener limits the rectified voltage. A 220uF/35V chemical capacitor provides filtering.
A small DC-DC Step Down to CN3903 converter found on Ebay or by aliexpress powers the arduino. Its quiescent current is approximately 0.36mA. To save a little current, the on-board LED and the 5V regulator of the Arduino have been removed. By inhibiting in the software the operation of the ADC, the SPI and the Timer_2 which are not used, we can still grab a little current. Finally I checked the operation of the arduino under a voltage lower than 5V. The one I used worked still at 3.9V. So I modified the DC/DC converter for an output voltage of 4.4V. This allow to gain some more mA.
Under these conditions the current consumed at the input of the converter on the 24V reach 3.5mA. When the relay closes, add another 8mA. That's a total of 11.5mA.
The reactance of the series RC is: √(R² + (1/(C × 2π× F))²) which is equal to approximately 15 kohm. The current through capacitor X2 and the two 150Ω series resistors is approximately 210V/15KΩ ≃ 14 mA.
That should be enough. In the real the measured current is 15.8mA RMS flowing trough the capacitor. This dispersion is due to the large tolerance (+/-20%) of the X2 capacitor. Note that the capacitance can go down to 176nF or reach 264nF for a new capacitor. And its value can decrease with the time...
How it worksThere are three buttons on the front. Two buttons allow you to adjust the countdown, one to increase and the other to decrease the time. The third starts the timer with a short press and stops it with a long press. In the software, the "SHORT_PRESS_TIME" parameter sets the limit of short presses to 500 ms. You can increase it according to your needs or habits.
The combination of simultaneous presses of two buttons allows you to preload one of the three predefined values. They can also be adapted in software. By default I set them to 30 minutes, 2 hours and 10 hours which I use most often.
You can change the time by pressing the + or - buttons. Note that this is possible whether the countdown is running or not.
As long as the button is pressed, the value is changed about five times per second. It starts with a change of 1 second ten times in a row. Then the change is multiplied by 5 also ten times in a row. Then again multiplied by 5 and so on. The value is limited to 900 by the parameter "maxIncrement".
The softwareAn interrupt is generated every second by an Arduino timer. Timer 1 is used because it is a 16-bit timer. It is preloaded with the value corresponding to the formula:
OCR2A=(Crystal frequency/(prescaler*ISR-calling-frequency))-1
OCR2A = (16.000.000 / (256 * 1 )) -1 = 62499
This is the theoretical value. To improve accuracy, I set a "FrequencyMeasureOutput" (pin 12) to toggle on each interrupt. This pin can be measured with a frequency meter. The duration of the high and low states must be exactly 1 second. If the period is measured, it will be 2s. If frequency is being measured, it will be 0.5 Hz. the counter preset value "OCR1A" can be gradually adjusted for the best accuracy. In fact, the accuracy of a quartz oscillator will be between 10^-4 and 10^-6 while with ceramic resonators the accuracy will drop to 10^-2 ~ 10^-3. Take into account that temperature and voltage also have an impact.
In the interrupt routine a flag is set and the state of the "frequencyMeasureOutput" pin is inverted. If the coundown is runing AND this flag is set, the coundown is decremented and the flag reset. The displayed value is updated. If the countdown reaches 0, the relay is deactivated and once the "OLEDTimeout" is reached, the OLED display is turned off.
Comments
Please log in or sign up to comment.