Optocouplers and light circuit (check if lights on)


I'm currently trying to make an "on-board computer" for my classic car. Basically it would be to measure basic data like speed, engine RPM, voltage... I'll be using a standalone ATMEGA328P.

I would like also to add some kind of circuit to check if the headlights are turned on and if the overheat switch is closed. I was thinking about two circuits :


Legend: BAT+: 10-14V car battery positive voltage / VCC: 5V regulated for the ATMEGA328P (7805) GND: common between the ATMEGA and the car ground 12V_LIGHT_BULB: 12V light bulbs with unknown resistance D0/D1: digital inputs of the ATMEGA

The top one is for the overheat switch. The light bulb is connected to the car battery positive in the dash and the switch connects to ground when reaching about 100°C. The bottom one is for the headlights. The switch is connected to the car battery on one side and the bulbs directly to the ground. If I have chosen this placement for the optocouplers, that's because it's the most convenient way to connect them in the car and because I don't want to cut wires.

My questions are : - Would both circuits work ? It would be perfect but it is where I have a big doubt - If not, is there any alternative not involving cutting the car wires ? - If they do work, can they be improved ? (optional question, but you might have better ideas than me)

Thanks in advance for your help!

Screenshot 2021-02-12 202506.png|617x705

Sorry, but neither circuit will tell you if the lights are on. They might tell you if there is 12 volts on the circuit to the lights. You need to measure the CURRENT to the lights to tell is they are ON or OFF, or BURNED OUT. Paul

Thanks Paul for your answer :)

Oh sorry, I didn't explain very well what I meant by checking if the lights are turned on: I just need to know if the switches are closed.

So if I understand your reply, it will work in that case?

Both circuits look OK to me, but you don't need the 1N4004 diodes.

...I didn't look-up the opto-coupler datasheet to verify the value of the 1.8K resistors but it looks about right.

Ok thanks!

I couldn't figure out if what I designed would be working. I thought it would but I had big doubts :sweat_smile:

I've added the 1N4004 diodes because the reverse polarity of the optocouplers is low (3V) and don't want to burn them if the polarity of the board would be reversed.

For the resistor values, the ILQ30 and ILD55 I'm planning to use can take up to 60mA. I will lower a bit the value, I can go down to about 330 Ohms.

You probably don't need the 1N4004 diodes as it is very unlikely you will get a negative voltage surge on the lines and surely you will not wire it wrong.

If the optocouplers are mounted close to the ATmega, you do not need the 10k pullups - you just use INPUT_PULLUP which is effectively 47k. The 1k8 resistor could be more like 4k7 (even with the 10k pullup) as it is dropping 10 V. No current optocoupler has a CTR less than 50% even at 2 mA (the 4N2x series is clearly simply obsolete)!

The ILQ30/ ILD55 is completely inappropriate for this application.

Use a PC817 or multiple thereof.

If I have understood correctly your advices, my schematic looks more simpler now : |500x440

I keep the 1N4004, the battery leads are too easy to reverse... :smiling_imp:

For the resistors let's go with 4.7k then. And yeah, the PC817 datasheet looks much better ;) I'm new to the optocoupler world, I took pretty much the first one I saw.

Screenshot 2021-02-12 234322.png|611x538

arnakazim: Hello, I'll be using a standalone ATMEGA328P.

Why would you do this? Use a Nano or similar small from factor 328 and make it so it plugs into your PCB design. It will be so much easier to design and debug.

Here is a prototype I made a long time ago, similar concept, makes reprogramming sooo much easier.

|500x375 Tom... :)


And I agree. Using a standalone ATmega is simply making it difficult for yourself. A Nano if you anticipate reprogramming it, or a Pro Mini if you do not, is far more practical. Just do not use the on-board regulator. :astonished:

Yeah it would definitely be easier to base my design on a standalone board! But I've made plenty of projects using Arduinos (mostly Duemilanove/Uno and Nano) and thought it would be a cool experience to make an Arduino from scratch ;) I am using Nanos for the prototyping part, but for the final product I'll make my own.

Also I might have the use of this board in more than one car. Once my prototyping is finished, I plan to print a few PCBs. I'll be able to have a compact board with no hanging wires (except the ones that connect to the car) and looking a bit cleaner.

I haven't decided yet if I will program the board via an external FTDI module or via ISP. The former will allow me to debug via serial if need be, the later is more compact. But in either case, it is just a 6 pin header (1x6 or 2x3) I have to add in my design.

"1x6 or 2x3" or both. Can be smaller pitch, make an adapter from that smaller pitch to 0.1" pitch for FTDI, ICSP, for space savings.

What's the point of using opto-isolators if you connect the input GND to the OUTPUT GND ? That eliminates the isolation.

CrossRoads: "1x6 or 2x3" or both. Can be smaller pitch, make an adapter from that smaller pitch to 0.1" pitch for FTDI, ICSP, for space savings.

I didn't think about reducing the pitch, but thanks for the idea! I think I'll incorporate it in my design :)

raschemmel: What's the point of using opto-isolators if you connect the input GND to the OUTPUT GND ? That eliminates the isolation.

I'm not using the optocouplers to isolate circuits in this case. I use them more like relays. I could use voltage dividers, but with optocouplers I'm sure the output tension will be 5V no matter how fluctuating the tension is on the input side. Just need to be sure I get a sufficient current on the input side with in the voltage range.

Why not just use a transistor then ? For the record, a relay offers isolation that a transistor does not because the coil circuit is isolated from the contacts.

Yeah I could. What would be the advantage to use transistors over optocouplers?

3-pin transistor + resistor, vs 4 pin optocoupler + resistor.

Or maybe just a cd74HC4049, with 0.1uF decoupling cap on the 5V VCC pin to Gnd. 12V in, 5V low output to pull down an input with internal pullup. Fewer total parts. DIP & SMD available


@CrossRoads, I like your ideas! Using a CD74HC4049 is a really elegant way to replaces the optocouplers, it will save me using a PC847 along side a PC827. One IC, only 16 pins to interface instead of 16+8!

I'm not quite sure I understand everything about the CD74HC4049 though. So according to the datasheet I can use it up to 16V. Which is fine, normally an alternator doesn't go over 14V. But what I don't get is do I still need to use resistors on the input ? I am not sure about the meaning of the current values in the datasheet...

Also for the capacitor, I am already planning to use a 0.1µF capacitor between 5V and GND. I don't need another one, do I? (sorry for my beginner questions...)

Ok so from what I understand from the sparse schematics I've found there is no need for resistors before the 4049.

BUT I am really not sure. If nobody corrects me, I guess experimenting with the CD74HC4049 will give me the answer...