The illuminated pushbutton switch may be a DPST switch - as shown in Schematic A below. IfBut even if it's SPST, it doesn't change the fact that the LED must have a current-limiting resistor. ItThe form of the switch (SPST v DPST) doesn't change the interface requirements either. A part number or schematic of the switch would enable us to sort that out, but again, I don't feel it makes a huge difference unless it's a very specialized switch.
The schematic below (Schematic A) shows two options for the interface - Option I & Option II:
1. Option I: Zener Diode
A zener diode limits the voltage at the GPIO pin to 3.3V. It will maintain this 3.3V limit over a wide range of power supply voltages. This reduces yourthe risk of damaging your RPi - and the circuitry behind the GPIO pins is very fragile.
2. Option II: Voltage Divider
A voltage divider will also work - although over a smaller range of input voltages than Option I. You have indicated this is your preferred solution due to parts availability - and that's fine as long as you're, but you should be clear on the tradeoffs you're making.
Schematic A:
From Ohm's Law
simulate this circuit – Schematic created using CircuitLab
Voltage Divider Calculations for Option II:
The RPi's GPIO pins operate from a 3.3V supply; (and the Wikipedia page)damage is almost guaranteed (see warning) if input voltages exceed that level. Consequently, the resistor values ofneeded to set the two resistors mayvoltage to be easily calculated as followsinput to the RPi must be determined with some accuracy. This is a critical step; an error in this calculation may destroy your RPi. Fortunately for our RPi hardware, Ohm's Law, and the Wikipedia page on voltage dividers, provide guidance for calculating these resistor values:
Vout = Vin * R3 / (R3 + R4)
Vout = Vin * R3 / (R3 + R4)
Finally on the subject of resistor selection for a voltage divider interface, this is an opportune time to address another topic:
Voltage Thresholds for Logic HI (1) and Logic LO (0)
simulate this circuit – Schematic created using CircuitLab RPi GPIO are all digital, meaning there are only two valid states: High (1) and Low (0). But voltage, resistance and current are clearly creatures from the analog domain. This means that we should consider the voltage thresholds imposed on us by this binary system as part of our voltage divider design.
These voltage thresholds are currently defined in the "Official" documentation. They are summarized as follows:
Binary Logic Level |
Worst-Case Voltage |
Low / LO / 0 |
Vin ā¤ 0.6 Volts |
High / HI / 1 |
Vin ā„ 1.6 Volts |
"Worst case" here means that for any (defined) value of VDD IO
, the thresholds in the table above are valid. For the case at hand, the designer may use a value for R4 as low as ~1.8Kš®. Since playing at the margins is usually risky business, values of 10Kš® for R3 and 2.7Kš® ā¤ R4 ā¤ 3.3Kš® seems reasonable.
3. Option III: Potentiometer
AnA functional equivalent alternative to the voltage divider is to use a potentiometer with the wiper connected to the GPIO pin - see the second schematicSchematic B below.:
Schematic B:
simulate this circuit
4. Option IV: External voltage clamp
UseClamping diodes offer valuable risk reduction if a resistive divider, or a potentiometer with clamping diodesis used. You've indicated a preference for resistors only, but for the sake of completeness aI'll include this option. A pair of clamping diodes connected across the GPIO input will provide some additional protection. Note that the GPIO circuitry on the RPi already includes a pair of clamping diodes, but as they are elements of the integrated circuit (parasitic FETs most likely), they have a very limited current-carrying capacity. External
Schottky diodes are much favored over p-n junction diodes for voltage clamps can carry more current & will likely reduce the risk of damage. An external Schottky diode offers superior performance (and protection in this application) for three reasons:
Schottky diodes can carry more current & thereby reduce the risk of damage to the RPi's GPIO circuitry
Schottky diodes have a significantly smaller forward voltage drop than a p-n diode
Schottky diodes have very low effective capacitance, and as a result are extremely fast - speed matters in device protection.
Schematic C: