As @Gerben says, voltage dividers are a good place to start. To elaborate on the topic, you can use a combination of voltage dividers, zener diodes and clamping diodes.
This EE.SE post has some good info about this, related to ADC, but still the same.
First off, a voltage divider circuit is easily found on the internet,
- Google
- Wikipedia
This circuit from Hyperphysics shows the basics:
To add more protection to this so as not to go over the max I/O voltage of the MCU, you can add a zener diode on the Vout.
That would give you something like this(Olins):
This will give you the desired range to give legitimate High and Low on an I/O.
If you wanted to forgo the voltage divider and zener you can use clamping diodes to the microcontroller supply voltage Vcc. This will give a range that will not damage the I/O.
simulate this circuit – Schematic created using CircuitLab
The 10k resistor is there to limit the current through the diodes, which are Schottky diodes.
Another method, which I have used before and is reliable, is to use a diode and two resistors like below:
The logic is that, when the input IN
is > Vcc(5V) the signal on the I/O _IN
will be the voltage of the pull_up resistor R2
, if it is low or floating the signal will be low. R1 also gives a known logic state if the input floats.
Edit: Changed the image to proper logic flow.