Logic level converter for NodeMCU (ESP8266): Input 24V/16V (Hi/Lo), 500 Baud
The simplest thing to do from a hardware perspective would be to use a pair of resistors to scale the voltage to a range acceptable to an analog input on the NodeMCU and do the rest in software.

If you want to do more in hardware, add a voltage comparator that compares the scaled voltage to an appropriate threshold and then feed the output of the comparator to a digital input on the NodeMCU. For example, if you scale the 24V input down to 3.3V, the lower level will be at 2.2V. A threshold voltage of 2.75V would be appropriate.



The following circuit made only with discrete transistors might do an adequate job. The input divider scales the 16V/24V input to 2.2V/3.3V. The B-E junction of the PNP creates a threshold at about 3.3V - 0.65V = 2.65V. When the input is at 16V (actually, anything less than about 19V), both transistors are switched on, and the digital output is pulled to ground.







simulate this circuit &ndash; Schematic created using CircuitLab

When the input is 16V, this is equivalent to connecting the base of Q1 to 2.2V through a 3700&Omega; resistor (Th&eacute;venin equivalent source). This puts about 0.45 volts across R5, allowing about 100 &micro;A of current to flow out Q1's collector. Most of this current flows through Q2's base, driving it into saturation and sinking about 3 mA through R4.

R3 prevents leakage current through Q1 from turning on Q2. It sinks about 30 &micro;A @ 0.65 V, so anything less than that keeps Q2 cut off, while anything more than that switches it on.