Limit switch running on 48V connect to esp32

Hello, I need to connect a limit switch to an esp32. I'm building a robotic arm and the drivers of the stepper motors are supplied with 48V. Online, I have read that operating my limit switches with 3.3V for example count create problems with noise and my esp32 noticing an input even though there is none. I plan to use a PC817C optocoupler to isolate both circuits. I just wanted to check if my calculations for the resistors are correct.

R1 = (3.3V - 1.2V) / 0.02A = 105 Ohm
R2 = (48V - 2V) / 0.02A = 2300 Ohm (this is the right calculated value, ignore the one in the image)

Thank you

Basing a design on something you think you read is not a good start. If using 3.3 volts with your Arduino and limit switch show problems, then address it at that time.
Why do you think 48 volts will not also have problems? Noise is noise at any voltage. And yes, noise may also turn the LED on in an opto-isolator.
By the way, a limit switch should OPEN when pressed, so if the switch and wiring fail and open up you will know it.By only checking for a closed switch and something fails you will never detect it.

R2 = (48-1.2)/10mA = 4680, use 4.7K
With a CTR of 200 the output current should be made less than 2 x 10mA
Five times less would be good so R1 = 5V/4mA = 1250, use 1.5K

1 Like

Thank you, so when I change the resistor values, the circuit would work?

The values are right but your schematic is wrong.

Connect the optocoupler emitter to GND, and the I/O pin to the collector (pin 4).

R1 should be around 10K or higher. Anything less just wastes power.

Here is the correct schematic
image

And then I need to use pinMode(x, INPUT_PULLUP) ?

No just INPUT but if you use PULLUP there is no harm done.
The signal will be inverted. When the switch is closed, the input to the ESP will go LOW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.