Convert square pulse with 24-volt amplitude to square 5-volt amplitude
I need to take a 24-volt amplitude square pulse with offset sometimes from 0 levels and I need to convert it to a 5-volt without any offset to able to enter it into Microcontroller.

One might start with a simple voltage divider.
For example:



simulate this circuit &ndash; Schematic created using CircuitLab
Assuming there are no loading issues and precision resistors,

if the input is between 24V and 27V, the output will be between 4.444V and 5V
if the input is between 0V and 3V, the output will be between 0V and 0.556V

This should be adequate for driving the logic levels of a gpio pin of a 5V microcontroller.
If you need something more precise, I would add a second stage consisting of a logic level cmos buffer such as the SN74LVC1G125.



simulate this circuit
The output of this circuit will be

0V if Vin &lt; 7.29V
5V if Vin &gt; 20.79V