-->
Page 1 of 1

How can I detect a 5V external switch signal with NodeMCU

PostPosted: Mon Mar 14, 2022 2:42 pm
by Jay Welsh
I have a switch circuit through which 5V and approx. 1 to 2 ma DC flows when the switch is closed.

I would like to design a NodeMCU board to detect when the switch is closed and the switch circuit is live (i.e. 5V) without interfering with the signal being relayed to the origin switch device.

Both devices are powered by the mains 230V AC (NodeMCU via USB cable and transformer, switch device has its own 5V transformer and motherboard).

I have included a diagram of what I would like to do.

All the posts I have seen so far seem to suggest reading the voltage on the analogue pin A0, but they all seem to share a common ground/power supply which my devices do not.

Many thanks

Re: How can I detect a 5V external switch signal with NodeMC

PostPosted: Tue Mar 15, 2022 7:14 am
by btidey
First you should connect the two grounds (0V) together. This is quite safe as they are both isolated by transformers.

Next you can detect the signal on the side of the switch which supplies the 5V when the switch is closed. This should be at 0V when the switch is open and 5V when it is closed.

You can't connect it directly to one of the NodeMMCU GPIO pins as these will only tolerate 3.3V. So instead connect it via a resistor divider which will reduce the 5V signal to around 3.3V.

The value of the resistors will depend on how much extra current you are prepared to draw from the 5V circuit.

As an example, a 22K connected to the switch terminal and a 33K connected from that to 0V would draw about 0.1mA extra and give 3.0V when the switch is closed. The junction of the two resistors can then be connected to a GPIO pin set to be an INPUT. This will read a 1 when the switch is closed and a 0 when the switch is pen.