6

i have a flip switch and a 2700 ohm resistor and i want to read the state with python. The code-part isn't a problem to me but is it possible to use this components to wire it up to the GPIOs to read the state? And how must the wiring look?

I read something about some Pins have an integrated pull-up resistor or something. Does it mean that i don't need a resistor for using a button/switch?

Thank you! :)

Greetings, Mark

2 Answers 2

6

Here is the basic setup you need. You can use the GPIOs with the builtin resistors if you wish


 3V3---o
       |
       |
       >
       > Resistor
       >
       |
       |
GPIO---o
       |
       |
        \ Switch
         \
       |
       |
 GND---o   

You can also use the weak pullup/pulldown resistors inside the SoC eg.

  // enable pull-up on GPIO24&25
   GPIO_PULL = 2;
   short_wait();
   // clock on GPIO 24 & 25 (bit 24 & 25 set)
   GPIO_PULLCLK0 = 0x03000000;
   short_wait();
   GPIO_PULL = 0;
   GPIO_PULLCLK0 = 0;

If you are using the internal pullup you just need this part

GPIO---o
       |
       |
        \ Switch
         \
       |
       |
 GND---o   
13
  • @ppumkin, If the resistor is not there, the input will float and tend to switch randomly as any electric fields move around it. The value of the resistor is not critical. Sep 18, 2012 at 16:25
  • Okay thanks. But the 2700ohm resistor is okay for it? Sep 18, 2012 at 16:27
  • Huh? If its driven by 3.3volts its not floating.. Using a resistor only reduces the voltage.. so how does that eliminate floating voltages? I understand a pull up or pull down schematic does that.. but i think that inline resister is redundant...
    – Piotr Kula
    Sep 18, 2012 at 16:27
  • @Mark, 2700ohm should be fine. Sep 18, 2012 at 16:28
  • @ppumkin, it is a pullup resistor. Without it the switch would short 3V3 to GND. It's job isn't to reduce voltage. The voltage at the GPIO will be 3V3 if the switch is open and 0V if the switch is closed Sep 18, 2012 at 16:29
1

Normally, GPIO pin is not connected to anything so it's value is not possible to reliable detect it's state. It is said, that this state is floating as in this state, value of GPIO pin can change depending on some unpredictable external events.

Now, you can connect switch (or bush button) with one side to GPIO pin and one to the ground (GND). This way, if you push the button, GND will be connected to GPIO and it's state will be low. If you release the button, GPIO wont be connected to anything again (so it will have floating state).

So you need to somehow make your GPIO be connected to high voltage when your push button is released. This is called pull-up as when you do this, this will pull your voltage up.

But how to do this? You have to connect your GPIO to 3.3V. This way, when if your button is released, GPIO will have state high (3.3V). If you push your button, it will be connected to both 3.3V and GND. Current will flow from 3.3V to GND leaving GPIO at 0V (state low).

There is one problem, however. You have almost no resistance in your circuit so the current that will flow will be very thing (so called short circuit). That's bad. And this is why you need resistor, a pull-up resistor. It will limit current flowing from 3.3V to GND or GPIO pin.

How big resistor should be? Big enough to limit current to the value that won't damage GPIO pin, won't blow your input fusses on RPi or make input voltage drop to the value that will make your RPi reboot. In case your RPi would be powered from batteries, you would also like not to take too much current so the battery will last longer. It can't be to high, however, since RPi may have problems with detecting high voltage in the input. So to be on safe side, let's say that its resistance should be measured in kilo ohms :)

Now you should imagine what is internal pull-up resistor. It's a small resistor inside of a chip that connects GPIO pin to high voltage that you can use instead of connecting external one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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