I'm using an optocoupler (MOC3021) to sense the On/Off state of an electrial appliance using a microcontroller ATmega16L. How do i go about doing this? My mains supply specs are 230V, 50Hz. How do I design the surrounding circuit and select component values, like the resistors?
EDITED on 13th June 2012 Note: This is the first time I'm solving a circuit like this. Please send any helpful feedback. (including things I did wrong or any improvements)
Referring to the above schematic. The idea is to use this circuit to determine whether the load is on or off. The output pin from the optocoupler connects to an external interrupt of the Microcontroller I'm using which is ATmega16L. The interrupt will Monitor the state of the load. After monitoring I can toggle the state of the load using a relay (relay acts as a Control mechanism) which connects to the same microcontroller.
Now, I tried calculating the resistor values for R1, R2 and Rc. Note, microcontroller's VIL(max) = 0.2xVcc = 660mV and VIH(min) = 0.6xVcc = 1.98V and VIH(max) = Vcc+0.5 = 3.8V.
To calculate Rc is quite easy. When the transistor is not conducting the output is high (at 3.3V). When the transistor conducts the output is pulled low. so from microcontroller's point of view, output high means load is switched OFF and output low means load is switched ON.
Looking at the datasheet for SFH621A-3, using 34% minimum CTR at IF = 1mA. Therefore, at 1mA input, the output is going to be 340uA. So in order for the microcontroller to detect low voltage from the output of the optocoupler can I use resistor value of 1Kohm? So that the output from the optocoupler will have a voltage of 340mV (which is below VIL(max))
More on this later, been a long day.
EDITED on 15th June 2012
Note: Solving for resistors on the power line (R1 and R2). Please check my calculations and any appropriate feedbacks.
Aim: the aim is to keep the LEDs *ON** for maximum period of time in a 10mS half period (20mS full period of 50Hz). Lets say LEDs have to be ON for 90% of the time, that means LEDs require at least 1mA of current for 90% of the time for that half period which means LEDs will be active for 9mS in a 10mS half period. So, 9mS/10mS = 0.9 * 180(half period) = 162 degrees. This shows the current will be 1mA between 9deg and 171deg (and less than 1mA from 0deg to 9deg and 171deg to 180deg). Did not consider ON time to be 95% as working with whole numbers is neat and 5% doesn't make any difference not in this application at least.
Vpeak-peak = 230V x sqrt(2) = 325V. Taking tolerances into account. Minimum tolerance of 6%. 325 x 0.94 (100-6) x sin(9) = 47.8V
So, R1 ≤ (47.8V - 1.65V) / 1mA = 46.1 Kohms Choosing a value one smaller than 46.1 Kohms of 39 Kohms (e12 series). Now that a smaller value resistance is chosen compared to what was calculated, means current through the diodes will be greater than 1mA.
Calculating new current: ((325V x 110%) - 1.25V)/39 Kohms = 9.1mA (too close to max If of diodes). Coming back to this in a moment [Label - 1x]
First calculate the power ratings of the resistor (considering 39 Kohm) ((230 + 10%)^2) / 39K = 1.64 Watts (too high).
Going back to calculation [Label - 1x] Lets choose two 22 Kohm resistors. Together they add upto 44 Kohm which is quite close 46.1 Kohm(calculated above)
checking power rating of the two resistors combined: ((230 + 10%)^2) / (2 x 22) Kohm = 1.45W. choose 22 Kohm resistors each with 1W power rating.
Now, after all this the initial CTR was 34% which means 1mA in will be 340µA out. But now because of 2x22 Kohm resistors the current will be slightly more on the output. That means higher potential across the pull up resistor Rc. Would there be an issue to get a volt drop below 500mV on the output of the optocoupler??