I am trying to read a mechanical switch input that is connected to a 12V LiFePO4 battery. The signal will be read by an ESP32 Feather board (3.3V) from Adafruit.
I've read there are a few ways to do it (octocoupler, voltage divider). Is there a "best" way to accomplish this task given the input is coming from a 12V LiFePO4 battery?
I've read 12V inputs from a car battery, but it was suggested to use a optocoupler circuit due to voltage spikes... so I just wanted to confirm if there's anything else to consider for the LiFePO4? Thank you.
That's the safest way. For "ki9nd, nice" 12 volt source a voltage divider works well. However, motors, solenoid etc might cause ripple, spikes, in the signal and then the opto is preferable.
By "switch input" I assume you reading the voltage on the "other" side of a switch connected to the +12V.
A voltage divider is the simplest approach. This link should help. A divider using 10K and 3.3K resistors should give you a 3.2V reading when the input voltage is 12.8V.
You may want to look closer at what the battery's connected to. If you intend to charge it in situ, the divided voltage may exceed the 3.3V input. (you can use the link to decide which resistor values to use in that case. Be sure to use standard values, otherwise you'll never find what you're looking for.)
To be safe, you may want to knock the voltage down lower in case of surges and ripple and read the level using an analog input pin.