1

I have this rocker switch.

Enter image description here

I have it connected on the GND pin to GPIO pin 11 (BCM 17) and a 1 kohm resistor to GND on the Raspberry Pi, the 3.3 V pin from the Raspberry Pi to positive:

Switch '+' -> 3.3 V
Switch '-' -> GPIO ALSO to resistor and then GND of the Raspberry Pi.

I have this code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)

while True:
    if(GPIO.input(11) == True):
            print("switch ON")
    else:
            print("switch OFF")

But it only ever says that it's off. Why is this?

  • I'm confused as to your connections. Is it Pi ground to 1K resistor to switch contact #1. Switch contact #2 to gpio17? I'm not sure what the 3.3V pin is doing. – joan May 1 '14 at 19:10
  • Pi GPIO 17 is directly on the gnd pin of the switch. On the switch's gnd is also a resistor and then to the Pi's gnd. 3v3 goes to the switch's positive. – developius May 2 '14 at 8:54
1

I don't know why your switch doesn't appear to work. The usual reason is it's not connected to the gpio you are using in software.

I think setting up BOARD numbering will use the P1 pin numbers. So when you refer to 11 you are referring to P1-11 (which is attached to Broadcom gpio 17).

http://elinux.org/RPi_Low-level_peripherals

Personally I'd wire the switch as follows

Pi ground - resistor - switch - gpio

and set the internal pull-up on the gpio to 3.3V.

The resistor acts as a safety device in case the gpio is accidentally set as a high output. Without a resistor there would be a short to ground and the gpio might be damaged.

Any resistor in the range 300-10,000 ohm should be fine.

With the above set-up the gpio will read 1 if the switch is open and 0 if closed.

1

In the schematic (borrowed) you can see GND is connected to the GPIO. This provides a LOW state which means the switch is OPEN. When you close the switch the GPIO will read high. The resistor is 10 kohm, but 1 kohm is also OK. That value only matters if you are trying to save battery power.

Make sure to use 3.3 volt and not 5 volt.

Enter image description here

  • If the switch is closed and the gpio is (accidentally) set to be a low output would that cause damage, i.e. pop one or more gpios? Why not put the switch between resistor and gpio? With an internal pull-up to 3.3V. The gpio will then normally read 1, but read 0 when the switch is closed. – joan May 1 '14 at 21:12
  • Software is human error. To prevent that you can place a diode on the 3.3v. But reversing the circuit is also acceptable. – Piotr Kula May 1 '14 at 22:07
  • I'm pretty sure this is exactly what I have. So 3v3 goes to the + on the switch and the switch's - goes to gpio and (via resistor) to gnd? – developius May 2 '14 at 8:55
0

Thanks for all the help!

In the end, I tried this which works. enter image description here

 Black is gnd of Pi
 Green is GPIO input pin
 Red is 3v3 pin of Pi

Is this safe?

  • That Black wire is doing, absolutely nothing :) I bet if you remove it, it will still work. Unless by luck, the Black Wire PIN is common with the Green wire PIN, in which case its like the circuit I posted. Just using 3.3V to GPIO in will work fine, but it not really recommended. – Piotr Kula May 2 '14 at 14:52
  • 1
    I took out the gnd and it doesn't work after that... – developius May 18 '14 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.