The MOC3021 is an optocoupler with a triac output. It's used to drive a power triac typically to switch mains operated appliances. Triacs can only be used in AC circuits.
You need an optocoupler with a transistor output, preferably one with two LEDs in antiparallel at the input. The SFH620A is such a part.
The two LEDs in antiparallel ensure that the transistor is activated on both half cycles of the mains. Many optocouplers only have 1 LED, that would work, but give you an output pulse of 10ms in a 20ms period for 50Hz. You would need to place a diode in antiparallel to the input also in that case, to protect the LED from overvoltage when reverse polarized.
Important is CTR or Current Transfer Ratio, which indicates how much output current the transistor will sink for a given LED current. CTR is often not very high, but for the SFH620A we can choose a value of 100% minimum, only that's at 10mA in, at 1mA it's only 34% minimum, so that 1mA in means at least 340\$\mu\$A out.
Let's suppose the output goes to a 5V microcontroller and that you would use the 2k\$\Omega\$ pull-up resistor shown in the diagram. If the transistor is off it won't draw current, except for a small leakage current, 100nA maximum, according to the datasheet. So that will cause a voltage drop of 200\$\mu\$V across the resistor, which is more than safe.
If the transistor is on, and it draws 340\$\mu\$A then the voltage drop across the resistor is only 680mV, and that's way too low to get a low level. We'll have to increase either the resistor's value or the current. Since we had a lot of margin on the leakage current we can safely increase the resistor value to 15k\$\Omega\$ for instance. Then 340\$\mu\$A will give a sufficiently low output voltage. (Theoretically 5.1V voltage drop, but there's only 5V available, so it will go to ground.) The voltage drop because of the leakage current is still well within limits at 1.5mV.
If we want to have a CTR of at least 34% at 1mA we have to use the SFH620A-3.
If this would be controlled from a DC source we would almost be done. Just add R1 in series with the LEDs, R2 will probably not be needed. Then R1 \$\leq\$ (\$V_{IN}\$ - \$V_{LED}\$) / 1mA.
But we have to deal with a 230V AC input signal. At the zero crossings there won't be any current, there's little we can do about that. How can we get at least 1mA for most of the cycle without wasting too much power? This is a trade-off. You can have the 1mA for just the maximum voltage, and that will give you only a small pulse, but you'll waste the least power. Or you can go for 1mA for most of the cycle, but then you'll have more current when the voltage is highest. Let's say we want at least a 9ms pulse in a 10ms half period (50Hz). That means the current has to be 1mA at a 9° phase until 171°. 230V AC is 325V peak, but we have to take a -6% tolerance into account, so that's 306V minimum. 306V \$\times\$ sin(9°) = 48V. R1 \$\leq\$ (48V - 1.65V) / 1mA = 46.2k\$\Omega\$. (The 1.65V is the LED's maximum voltage.) The closest E24 value is 43k\$\Omega\$. Then we have more than 1mA at a 9° phase, but what at the voltage's maximum. For that we have to work with the positive tolerance, max. 10%. Then peak voltage is 230V \$\times\$ \$\sqrt{2}\$ \$\times\$ 110% = 358V. Maximum current is then (358V - 1.25V) / 43k\$\Omega\$ = 8.3mA. (The 1.25V is the LED's nominal voltage). That's well below the optocoupler's limit.
We won't be able to do this with just 1 resistor. It probably can't stand the high voltage, and may have power dissipation problems too, we'll come to that in a minute. Peak voltage across the resistor is 357V. The MFR-25 resistor is rated at max 250V, so we'll need at least 2 of them in series. How about power? 230V+10% in 43k\$\Omega\$ is 1.49W. The MFR-25 is only rated at 1/4W, so two of them won't do. Now you can choose to have more of them in series, but that would have to be at least 6, or choose a higher rated resistor. The MFR1WS (same datasheet) is rated at 1W, so 2 in series will do. Remember that we'll have to divide the resistor value by 2: 21.5k\$\Omega\$, which is not an E24 value. We can choose the closest E24 value an d check our calculations, or choose an E96. Let's do the latter.
That's all, folks. :-)
edit
I suggested in comment that there's a lot more which has to be accounted for, this answer could well be 3 times as long. There's for instance the input leakage current of the AVR's I/O pin, which can be ten times as high as the transistor's. (Don't worry, I checked it, and we're safe.)
Why didn't I choose an optocoupler with Darlington output? They have a much higher CTR.
The main reason is the Darlington's saturation voltage, which is much higher than for a common BJT. For this optocoupler for instance it can be as high as 1V. For the ATmega16L you're using the maximum input voltage for a low level is 0.2 \$\times\$ \$V_{DD}\$, or 0.66V at a 3.3V supply. The 1V is too high. That's the main reason.
Another reason could be that it may not really help. We do have enough output current, it's just that the 1mA input current is so high that we need power resistors for them. Darlingtons don't necessarily solve this if they're also only specified at 1mA. At a 600% CTR we'd get 6mA collector current, but we don't need that. Can't we do anything about the 1mA in? Probably. For the optocoupler I mentioned the Electrical Characteristics only talk about 1mA. There's a graph in the datasheet, fig.5: CTR versus forward current, which shows a CTR of more than 300% at 0.1mA. You have to be careful with these graphs. While tables often give you minimum and/or maximum values, graphs usually give you typical values. You may have 300%, but it may be lower. How much lower? It doesn't say. If you build just one product you can try it, but you can't do that for every optocoupler if you want to run a 10k/year production.
It might work. Say you use 100\$\mu\$A in, and at a relatively safe CTR value of 100% you would have 100\$\mu\$A out. You would have to do the calculations again, but your major advantage will be that the input resistors will only dissipate 150mW, instead of 1.5W. It way be worth it.
Given: Cree XM-L LED.
Want: Up to 2A drive, PWM controled by PC via USB.
This can be two parts. ie actual LED drive and PC to LED drive interface. These may or may not be integrated.
A "very easy" approach is to
1. use an off the shelf USB to "output" device. "Output" may be analog level, PWM, 8 bit port etc to control ...
2. An off the shelf LED driver that uses analog or PWM input.
For example, the circuit below using a RT8482 requires an analog input level or PWM with a simple RC filter (to convert the PWM to analog). The analog could be provided by a USB to analog output I/O device (COTS) or by a USB to parallel port device (not a printer port per se) (COTS) with a simple R2R digital to analog converter (about 16 resistors plus maybe a cheap op-amp).
Many examples of R-2R ladders here - links live
Or a microcontroller with USB capability could have a relatively simple program written to provide PWM or analog output. A USB enabled Arduino or a Raspberry Pi would do this. (USB has to be slave not host mode).
LED drive:
(1) "Off the shelf" complete units that do the LED drive part of this job well are available at good prices from eg ebay, or Mouser and similar. Using such is a good default solution unless you have some reason to do otherwise.
(2) DIY LED driver.
Digikey LED drivers are found here. Alas the parametric search is poor in this case (which is unusual).
Searching using LED driver 2A gives better results.
There will be a nummber.
Example only: For $US1.52/1 in stock Digikey you get
1
Ricktek RT8482, buck or boost, LED driver.
Drives external MOSFET so LED current capability essentially unlimited.
Looks like a good start. 350 kHz for smallish inductors.
- High Voltage Capability : VIN Up to 36V, VOUT Up to
48V
Buck, Boost or Buck Boost Operation
C u r r e n t M o d e P W M w i t h 3 5 0 k H z S w i t c h i n g
Frequency
Easy Dimming : Analog, PWM Digital or PWM
Converting to Analog with One External Capacitor
Programmable Soft Start to Avoid Inrush Current
Programmable Over Voltage Protection
VIN Under Voltage Lockout and Thermal Shutdown
16-Lead WQFN and SOP Packages
RoHS Compliant and Halogen Free
A MOSFET suitable for use as M1 would be eg ONSEMI NTD4960 $US0.40/1 in stock Digikey, 30V, 9A, 9 milliohm on resistance nominal, logic gate - data sheet curves show good at 4V gate and say 4A.
ADDED:
Should I be looking at specific types of inductors for this sort of application
Inductors are very special for best results. If this is a one-off then off the shelf inductors from eg Digikey or similar are wise. We can give advice in this when final real spec is known.
I'm assuming all of the caps in this type of application would be ceramic?
Ceramic capacitors will work well for all capacitors shown. At least 10V rating. More or much more voltage OK.
D1 is Schottky and should have current rating equal or greater than LED max current.
Now I just need to figure out how to generate the PWM signal.
PWM is "easy" [tm] and may not be needed. Above LED controller example can use analog or PWM control.
USB to I/O
This USB to paraell FIFO I/O module](http://www.ftdichip.com/Support/Documents/DataSheets/DLP/usb245r-ds-v10.pdf) uses FTDI's FT245R USB-parallell FIFO interface IC - datasheet here .
Vast amounts of related FT245 information here
FT245 available from Digikey ~= $US4.50/1 from here
FT245 based module from Digikey for about $40/1 here
This page discusses a DIY USB printer port which, as you have complete control over the hardware and how it acts, could "easily" meet your need. Based on a PIC18F4550 microcontroller and not much else. All software PCB patterns, circuit etc free.
![enter image description here](https://i.stack.imgur.com/MM0gi.jpg)
Typical commercial USB to analog device
Best Answer
Converting to a "24 V signal" is missing the point. The real problem is driving a 24 V relay from a 5 V digital signal. Fortunately, that is easy. Here is one way:
You didn't say how much coil current the relay draws at 24 V, so I picked an example part I had in my system (Zettler AZ8-1CH-5DSE).
Figure the B-E drop of Q1 is about 700 mV. This means there will be 1 mA of base current when the left end of R1 is held at 5 V. To guarantee the transistor stays solidly in saturation, let's say we only ask it for a gain of 20. This means it can support up to 20 mA collector current. 13.5 mA is well below that, so no problem.
D1 is not optional, even though it looks like it doesn't do anything. The relay coil has a significant inductive component. When anything tries to shut off the current through it abruptly, the inductor will make whatever voltage it takes to keep the current flowing in the short term. Without the diode, that would require abusing the transistor. The diode gives the inductive kickback current a safe place to go while the current dies down on its own due to the resistance of the coil.
Added in response to your edit
I see you have substantially changed your question while I was writing this answer. Your original question was better, because it simply asked how to do something. That's easier to answer than having to first dispel myths. I might have skipped this question entirely if I had seen it post-edit for the first time.
In any case, my answer above is still valid. Using a opto-isolator is silly, since you don't need isolation nor a unpredictable voltage shift. The ULN drivers are darlingtons, which have unnecessarily high saturation voltage. You are also trying to use it as a high side driver. Again, it's a lot more trouble to explain why a bad circuit is bad than to show a good circuit. I'll therefore stick to my original answer.