H38DA

C
-

  https://projects.drogon.net/raspberry-pi/examples/
  https://projects.drogon.net/raspberry-pi/wiringpi/functions/
  
Python
------

  https://www.youtube.com/watch?v=Bqk6M_XdIC0

> You may be happy to know that you don't need to physically attached
> the pull-up or pulldown resistors, as you can configure the 
> Raspberry PI's broadcom chip to set a pull-up or pulldown resistor 
> internally.

  import RPi.GPIO as GPIO
  
  # to use Raspberry Pi board pin numbers
  GPIO.setmode( GPIO.BOARD)
  
  # GPIO17
  GPIO.setup( 11, GPIO.IN pull_up_down=GPIO.PUD_DOWN
  #                       pull_up_down=GPIO.PUD_UP
  
  # GPIO4
  GPIO.setup( 7, GPIO.OUT) # 
  GPIO.output( 7, 0)

  try:
    while True:
      if (GPIO.input( 11) == 1):
        GPIO.output( 7, 1)
      else
        GPIO.output( 7, 0)
  except KeyboardInterrupt:
    GPIO.cleanup()
 
In this video you will learn how to read input from a GPIO pin using a push button. You should already know how to control an LED using a gpio output pin, be familiar with if statements and be able to use while loops.

Before considering inputs let's review how outputs work. We can set an output pin as either low or high: these are represented by the binary values 0 and 1 in the case of the Raspberry Pi an output pin set low will output zero volts, whereas a pin set high will output put 3.3 volts.

The key thing a highlight here is that we are using logic to produce a specific voltage level. In the case of inputs the exact opposite takes place that is a voltage level is used to produce a logic level. It's hard to get exact electrical specifications for the GPIO pins but after a little experimentation, I found that a voltage of about 0 - 1.2 volts produced a low input and 1.3 - 3.3 volts produced a high input. Thus a voltage between 1.2 volts and one 1.3 volts wasn't read as either low or high.

Let's see how this looks in a circuit. Here are two simple setups recall that pin 12 and pin 7 must be set up as input pins. Also recall that pin 6 is connected to ground and pin 1 is attached to the 3.3 volt rail.

         ---------
        |         |
  02 04 06 08 10 12 14 16 18 20 22 24 26  Top Example
  01 03 05 07 09 11 13 15 20 22 24 26 28

  02 04 06 08 10 12 14 16 18 20 22 24 26  Bottom Example
  01 03 05 07 09 11 13 15 20 22 24 26 28
  |         |
   ---------

In the top example input pin 12 will read zero volts as a low. In the bottom example input pin 7 will read 3.3 volts as a high. Pretty straightforward. 

  02 04 06 08 10 12 14 16 18 20 22 24 26
  01 03 05 07 09 11 13 15 20 22 24 26 28
  |         |
  |   ___   |
   ---o o---

But consider this setup. Here I've placed an open switch that means the pin is not connected to 3.3 volt pin and actually it's not connected to anything. We say the pin is floating and the voltage on it could be really anything between 0 and 3.3 volts. This is a real problem as the pin will sometimes be low and will sometimes be high, and it will change by itself without us doing anything. 

To fix this problem we need to make the input pin have a starting value either starting low or starting high. We can do this using something called pull-up and pull-down resistors. 

  Pulldown resistor      Pullup resistor
                         
    E1 1 0 3V3             E1 1 0 3V3
    K1 1 2                 R1 1 2 10K
    R1 2 0 10K             K1 2 0
    R2 2 GPIO_Input 1K     R2 2 GPIO_Input 1K

On the left is a pulldown resistor and on the right is a pull-up resistor. In both cases we've connected to the: 

  3.3 volt rail, 
  a push switch, 
  a GPIO pin set for input, 
  and ground. 

The resistors will be explained in a moment. Let's first examine the pulldown resistor. Ground is at zero volts and with no current flowing everywhere in this branch will be zero volts. That means that with the switch open the input pin will read zero volts. If the switch is closed current will pass from the 3.3 volt rail to ground. Only a very very tiny current will ever flow into an input pin. So almost all of that 3.3 volts will be used by R1. As almost no current is flowing in R2 the voltage will remain closed - 3.3 volts on both sides of R2 - thus the GPIO pin will read a high input. 

This is perfect. When the switch is open, the input pin reads low and when the switch is closed the input pin reads high. Never do we have a floating value for the pull-up resistor. With the switch open we have no current and thus 3.3 volt at the input pin when the switch is closed 3.3 volts is used by R1. That's the voltage here is zero volts meaning the input pin will read a low. So this is similar to the pulldown resistor only with high and low swapped about the resistors the 10 kilohms resistors are needed to limit the current from 3.3 volts to ground. 

The one kilo ohm resistors on the other hand are not necessary. They serve only to protect the GPIO pins in case it is mistakenly set as an output. For instance, if this input was set as an output at zero volts, a very large current would pass from the 3.3 volt rail to the GPIO pin, likely damaging the pin. 

Note also, that a pulldown resistor gets its name from pulling the GPIO pin initially down to zero volts when the switch is open. And the pull-up resistor gets its name from the voltage being pulled up too high when the switch is open in this case - 3.3 volts.

You may be happy to know that you don't need to physically attach the pull-up or pulldown resistors as you can configure the raspberry PI's broadcom chip to set a pull-up or pulldown resistor internally. Let's see how that's done.

As always we make sure to import the GPIO module and set mode as GPIO.BOARD. To set up the input pin we will use three arguments: 

  -- the first is the pin number. Here I've chosen pin 11
     next we need to set it as an input pin (GPIO.in).
     
  -- We can stop here but if we wish to use an internal pull up 
     or pulldown resistor we can define that in our third argument.
     Here this indicates that we want to use a pulldown resistor.
     For a pull-up resistor the code would differ only slightly.

I'm also going to set up an output pin. This pin will be attached to a LED and we'll go high whenever the input pin goes high. As my program will loop until there is a keyboard interrupt, i will use a try except block. So if the user presses control see the program will reset the GPIO pins and then exit. The if statement inside the loop is fairly simple. If the input is high turn the LED on else turn it off. So this works fine, i can write this in a more efficient way. 

  GPIO.output(7,GPIO.input(11))
  
Here I'm simply saying to output whatever state the input pin is. That if the pin is low output low if it's high output high. Let's see this in practice.

Here pin 1 is connected to a switch which just to be safe is connected to a 1 kilo ohm resistor which is connected to input pin 11.

At the top of the breadboard pin 7 is connected to an LED which is attached to a current limiting resistor which is connected to ground.

Notice that there are no direct connections between the button and the LED. That's because the LED is being controlled by a python code alone.

Now when we press the button the LED turns on.

And there you have it, the basics on how to use an input pin.
