# Using optocoupler with MOSFET for dimming a LED

I have a blue LED which I want to dim using an Arduino, an optocoupler and an N-MOSFET.

For achieving this I assembled the circuit on a breadboard as shown in the pictures, using a YwRobot breadboard power supply to provide power to the optocoupler's transistor, and the MOSFET. I used the Fading example sketch from the Arduino IDE.

When I try to dim the LED in this basic configuration: $(PWM_{out} → Resistor → LED → Ground)$
The LED slowly lights up, reaches its maximum, then slowly dims, then the cycle repeats as it should in this case.

But in the circuit it behaves weirdly.
It doesn't dim, it's on full brightness, but when the cycle reaches again zero voltage, it's turned off. No sign of any change in the brightness.
I tried modifying the delay of the cycle and I used a multimeter to measure the LED's voltage. As soon as the cycle moved from turned off state $(0V)$ to a slightly higher voltage (I used 1 unit increments in analogWrite and 4 second delay) the LED was on full brightness $(2,8V)$.

I don't know what's going on. Maybe I don't need one of the resistors on the optocoupler's BJT side? I tried taking the $R_3$ resistor out but then the LED was always on, didn't even bother to turn off so I put it back.

Questions:

• What should I change in the circuit to make the LED dim like in the basic configuration?
• Do I need current limiting resistors for the BJT both on the emitter and the collector side $(R_2, R_4)$ in this case? I know that in the case of FETS, the $I_D=I_S$, so one resistor $(R_6)$ is enough.

Here are the schematic and the Fritzing project:
Datasheets:
4N35M optocoupler datasheet
IRLZ34N n-mosfet datasheet

• This is a wild guess, but r4 seems unnecessary, and r3 seems way to small for a proper bleed resistor. Depending on the fets capacitance, it's simply not shutting off fast enough? – Passerby Jan 31 '16 at 4:34
• @passerby - don't you mean R3 is way to big? – brhans Jan 31 '16 at 4:41
• Can you try measurimg the voltage @ OK1, pin4 please? My theory is that you're overdriving either the optocoupler or the MOSFET, which is causing all used signal levels to simply "max out" the led. (That, or try running it without OK1, just using the MOSFET in voltage-follower configuration). – Robherc KV5ROB Jan 31 '16 at 4:48
• @brhans, large resistance, to small current, yes, my mistake. – Passerby Jan 31 '16 at 5:03
• @domenix 4,575V is definitely looking like the optocoupler is being overdriven. Either you're going to want to reduce the power being fed to the optocoupler (bleed resistor between PWM and ground in your diagram), or fimd another way to isolate/protect your 'duino without losing linearity (if OK1 won't function reliably at any reasonable input level). FYI: Being an incredibly high-impedance device, a power MOSFET (in my opinion) should provide a pretty huge degree of protection for your arduino on its own (check the Drain-Gate and Gate-Source leakage/isolation in the datasheet). – Robherc KV5ROB Jan 31 '16 at 5:11