help with an LED dim / switch circuit, please

I’m having a bit of a brain blockage and could use a fresh pair of eyes, please. :wink:

I’ve built a circuit that isn’t quite behaving as expected. I have attached 2 versions of the schematic, one normal and one with the separate parts of the circuit highlighted with different colours to make explaining easier.

The circuit is part of my home automation project and it’s meant to switch a 12V LED strip on and off and also dim it. The catch is that the switching must also work in case the Arduino fails. I’m using a bi-stable relay and a wall light switch (like a 2 switch light circuit). The dimming is done after the switching with a MOSFET. The gate of the MOSFET is pulled high using the LED supply voltage so that the MOSFET is closed even if there is no signal from the Arduino.

In the highlighted schematics the
yellow parts are the relay control,
green is the dimming parts and
blue is the LED power parts. This all works fine.

The problem is the red parts. This is the feedback back to the Arduino that tells me whether the LEDs are on or off. Since there is a manual switch involved, I don’t know whether that is on or off. So I need to detect the state.

The problem is that when I build it like in the circuit the LEDs never go off (but they do dim ever so slightly). I just can’t get my head around why that is happening. When I cut the wire where the purple X is, then the circuit works fine. But of course then I can’t read the state any more.

Any ideas what I’m doing wrong?

Arduino_Dimmable_LED_Switch - Schematic.pdf (37.6 KB)

I’d try this. I did modifucation on the red circuit, in order to pull down the pin when the circuit is closed, and the state remains pulllled high when the switch is opened. I removed a transistor and a pull up 47k.

(if there are other wires missing due to bad copypaste :sweat_smile: please ignore, the modification are the 2 purple X and the new wire).

Warning: This is assuming you are switching the ground line, that’s why I connected the pin straight to the switch, there shouldn’t be any tolerance problem. However I’m not responsible for damage, you must check the schematic before modifications.

This is actually how I've done it first. This works BUT when the LEDs are off, I get about 7V on the "state" line that goes to A3 on the Arduino. I was not able to explain that either. Then I added that transistor for that reason (and for the added bonus of inverting the State signal).

Could it have something to do with how the MOSFET is used? When the 2 switches result in open, I'm basically pulling the source of the MOSFET to 12V through the 47k resistor (red).

I still have a few 4N25 opto couplers lying around, so I could just use one of these instead. Though it does bug me that I can't explain that unexpected behaviour.

I’ve done it like this now. Looks faaar to complicated for what it does but works as expected this way.

Arduino_Dimmable_LED_Switch - Schematic.pdf (39.6 KB)

I see a redundace on the BC547 on the state pin. Why do you use a transistor at the optocoupler output? I think you can connect the optocoupler straight to the Arduino state pin (pulled high to 5V of course).

You mean Q5? This inverts the signal so the Arduino pin is high when the light is on. I could have done this in the sketch but I’m going to connect some other relays with state output (similar to these but for mains and without dimming) and they report high when switched on. So the code would have been a bit more messy if I only need to invert some.

Oh ok.

Do you really know that the LEDs are in fact lighting or do you just know you’ve turned the FET on?
I’ve done similar circuits where I have a low resistor (0.1 ohms) between the drain of the FET and ground. Then, take that into an Analog pin on the Arduino. If there is current flowing in the circuit, there will be voltage across that resistor. In my case, I wanted to know motor overload current, thus 400mV meant I had 4A motor current. Obviously you can tailor the resistor to read the desired current range.

Do you mean when I'm testing it? Yes, I test with actual LED strips connected. Or do you mean the state signal back to the Arduino? That is, in fact, only telling me whether the MOSFET is on. If it was just an on/off circuit I would have done it the way you described or just take the GND for the optocoupler LED at the drain, not the source. But I wanted to get the signal before dimming to avoid getting false "off" readings caused by PWM dimming.

actually, the state it's not telling me if the FET is on. Only the Arduino can control the FET, so it knows if it does. It tells me if the switch - relay combination results in on or off.

I wanted to get the signal before dimming to avoid getting false “off” readings caused by PWM dimming.

If you use a lowpass ahead of your A/D you would average the PWM readings thus approximating what the eyeball sees. Even if the LEDs are DIM, there is still power going to them and it should be detectable.

Just wanted to toss another idea at you.