I used all the Arduino UNO R4 WiFi digital pins and needed 2 additional digital pins. So I programmed 2 analog pins, one in 5V sensing and one in ground sensing.
I used @jim-p diagram to isolate the signals from the Arduino pins.
pinmode(A1, INPUT); for 5V sensing
pinmode(A2, INPUT_PULLUP); for ground sensing
I don’t have a separate ground. I use the 12V barrel to supply power and return (ground). This ground comes from a fuse box which is supplied by a regulated 12V power supply. The Arduino ground pin is connected to the optocoupler grounds and signal ground.
If you think the ground reference is probably the culprit, I can try to isolate the analog inputs circuit ground.
That is wrong and is the problem.
Neither circuit needs a pullup. The circuits have a resistor connected to ground and a pullup will cause a slight voltage at Dn when the opto is off, could be as high as 1V, so with some noise, can cause false readings.
Remove the 5k1 in both circuits and connect the opto transistor between pin and ground.
Set the pinMode to INPUT_PULLUP.
Deal with HIGH or LOW logic in software, not in hardware.
The 1k LED resistor could be replaced with 5k1, for 2mA LED current.
Which is still plenty to switch an Arduino pin with internal pull up.
It wasn't some AI suggestion. It was simply code evolution. I started with the ground sensing signals and had no optocoupler or external resistor. So I used INPUT_PULLUP. When I wanted to introduce the 12V sensing signals, I created the previous post, got jim-p circuit suggestion and started using optocouplers and external resistors without realizing that I needed to replace the INPUT_PULLUP by INPUT. Now the code is corrected.
Thanks for the clarification, but that was exactly what I understood.
I fixed the A2 pinMode declaration. For A1, I checked and reconfirm the ground connections. From the fuse box negative bus bar to the ground in U1, there is a 0.007V difference. When the 12V signal is present, the A1 pin is at 4.97V. When it's open circuit, the A1 pin is at 0.007V.
So the circuit is OK and should provide the proper logic.
I then rechecked the code again and this time I found a small error that skips the digitalRead(A1) in some conditions. I fixed the small error and now the A1 pin status readout is working as expected. Sorry. My bad.
For those interested, here is a look at the Home Assistant / RAM VSIM dashboard that I use in my van to monitor different sensors. Some writings are in french because I'm french canadian ;-).
Thanks again everyone for your help. It's much appreciated.
I'm using the U1 circuit to detect 12V or open circuit. The 12V source provides 13.3V.
When the source is active, pin 1 of the optocoupler reads between .5V and 1.1V, so not enough to activate the LED. Does it suggest I have a defective optocoupler?
edit: 1.1V seems to be ok to activate the LED.
edit 2: If I deactivate the 12V source and reactivate it, the voltage at pin 1 changed from 1.1V to 0.7V, thus not detecting the activation.
edit 3: I swapped the optocoupler with a new one. The voltage at pin 1 is now 1.2V, consistent from one activation to the next. So I guess the problem was a defective optocoupler.
question: The circuit I have keeps the 12V source active pretty much all the time. Does the optocoupler able to withstand a constant (24h/7d) 13mA?
However, a negative transient could have damaged the opto.
I added 1N4148 diodes to protect the optos from negative voltages (currents going the wrong way).