Erratic readings when using UNO R4 WiFi analog pins as digital inputs

Hello to all,

This is a follow up from this thread:

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.

It works partly. Every once in a while, I notice that the Arduino analog pins doesn’t sense a digital 1 when the signal is at 12V.

Are the digital high and low thresholds different on the digital and analog pins?

Should I adjust the R2 and R4 resistor values?

Thanks

Have you set the pins as inputs
pinMode(A0, INPUT);
and do a digital read?
digitalRead(A0);

Is your chassis ground good?

Hello Jim, thanks for your reply.

I’m using

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.

Your MCU side sensing circuit is same in both cases, with pulldown, so why would you apply pullup for A2? Some AI suggestion?

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.

Thank you all for the replies. Sorry for the delay, I’m in a cellular dead zone.

I’ll make the changes tomorrow and see if it improves.

@jim-p , you say that the problem is the A2, INPUT_PULLUP declaration. Replacing it with A2, INPUT should fix both A1 and A2 reading issues?

Make a decision.

Use no resistor and INPUT_PULLUP (preferred)

OR

Use an external resistor and INPUT

Can't do both.

It will fix A2. If you have problems with both, then make sure the ground connections are good.

Just to be clear, the U1 circuit is used for detecting a 12V signal. The U2 circuit is used for detecting a connection to the 12V ground.

This is how the circuits should be used:

Thanks for all your inputs,

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.

So if all is good, then have fun!

Hello @jim-p ,

My circuit is acting weird again.

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?

Should not be a problem unless you have it mounted near a hot engine.

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).

Thanks @jim-p,

Diode installed. I guess the problem is fixed. So far so good. I'll see on the long run.