... four of them work fine and two show very unreliable behaviour.
That means either:
- you have a mistake on two of the boards.
- your design is on the edge of not/working.
When I detect metal object voltage at Zener diode (3.3V) is 0.70 V which is fine but when I connect microcontroller GPIO pin to read them voltage at same point (Zener diode 3.3 V) is 0.254 V and some time when there is no metal detection voltage at (Zener diode 3.3 V) is 1.154 V but it should be 3.3 V.
Numbering the components ZD1, ZD2, R1, R2, etc., makes it much easier to discuss the circuit. However, connecting the MCU pin to the 3.3 V Zener should not load the circuit unless the GPIO is programmed as an output.
- The 24 V Zener shouldn't be required if you are running on a good 24 V supply. If you do get an overvoltage there is nothing limiting the current so it will probably overheat and die. Also note that if is a ±5% device that its breakdown voltage could be < 23 V so it will be on all the time so you may be just lucky if you haven't seen smoke yet. If you really feel you need overvoltage protection then put ZD1 as shown in Figure 1.
simulate this circuit – Schematic created using CircuitLab
Figure 1. D1 provides over-voltage and reverse polarity protection.
- Connecting LED1 in series with the opto-LED saves a couple of components and LED1 gives an indication that current is actually flowing through OPTO1.
- The 3.3 V Zener diode does nothing for you other than cause another possible hot component since its breakdown voltage is the same as the supply. Your pull-up is to the microcontroller's power rail anyway so if you get a high voltage from the pull-up then it's most likely that the MCU is cooked already.
- I've used different ground symbols to indicate that the circuits are isolated. It's not clear from your diagram whether or not this is the case.