I've an old board with an output of 30-31V or 0. I need to read these high and low values with an ESP8266 that works at 3V3.
Do you think I can use a PC817 opto-isolator or something similar?
Can you point me to some schematics?
To use an opto-isolator you can use this circuit:
simulate this circuit – Schematic created using CircuitLab
The current through R1 is approximately (Vin-1.2V)/R1. Something like 5mA is reasonable for that particular optoisolator and for long life, so for 30V you want something of the order of 5.6K or 4.7K. Power dissipation is less than 200mW so a 1/4-W resistor is fine, but don't use a tiny SMT part without checking.
R2 is a bit trickier, it depends on safety margin, allowance for aging and the initial CTR (Current Transfer Ratio) for the particular optoisolator bin. If we look at this datasheet, there are four CTR ranks available, and the lowest performance one has a CTR of 50%. So if we assume that is the one, we might allow something like >5:1 for the margin and aging and temperature (higher temperatures are worse) and full saturation, so the current through R2 should be no more than 0.1*5mA = 0.5mA, so with a 3.3V Vdd, that implies 6.6K or more. So 10K should be fine.
You could choose to use 1mA rather than 5mA and increase both resistors by about 5:1, but that will reduce the speed by a similar factor and put it in the milliseconds range most likely. The CTR is actually guaranteed at 5mA only, but it should follow the relationship in figure 9, from whatever value it is at 5mA. Note that CTR deteriorates more in relative terms at high temperature at 1mA than at 5mA.
D2 is not necessarily required, but it protects the LED from damage if the voltage can be more negative than few volts (for example if it is connected incorrectly).
You can use opto coupler after caring for the voltage rating. But, i have simple solution.
\$Vin\$ can be \$30 V~31 V\$ or \$0V\$.
\$V_{out}\$ is simply will be \$~3.3 V\$ or \$0 V\$.
Example values: If \$R1 = 10 kOhms\$ and \$R2 = 1.2 kOhms\$, then the Output high will be about \$3.1 V\$ or \$3.3 V\$. When \$V_{in}\$ is \$0 V\$, the \$V_{out}\$ will be \$0 V\$.
For safety of the ESP8266 pin,
1. You can also connect a zener diode of about 3.6V reverse breakdown voltage
2. a capacitor parallel to resistor \$R_2\$ will help in filtering any transient pulses.
3. A series resistor to the input pin will limit the current (just in case).
A Zener diode is a silicon semiconductor device that permits current to flow in either a forward or reverse direction. The diode consists of a special, heavily doped p-n junction, designed to conduct in the reverse direction when a certain specified voltage is reached. The Zener diode has a well-defined reverse-breakdown voltage, at which it starts conducting current, and continues operating continuously in the reverse-bias mode without getting damaged. Additionally, the voltage drop across the diode remains constant over a wide range of voltages, a feature that makes Zener diodes suitable for use in voltage regulation.
You could pick up a 3.3v zener diode keeping in mind the appropriate current limiting resistor value. you could view more information here.