Input(Button Function)

EduBoard has two momentary switches, wired to GPIO pins 18 and 24 (pins 12 and 18 on the board). Both of them tied to ground. The reason for this is that the Raspberry Pi has internal pull-up and pull-down resistors that can be specified when the pin declarations are made.


GPIO setup Click on the picture to enlarge.
Assumes EduBoard already attached to Your Raspberry Pi, through the GPIO pins.
The following example will demonstrate how to setup and read an input from a button, using BCM numbering (connected to pin number 12, GPIO 18)
Open the editor window using:

Then type or paste in the following text and save the file using CTRL-X and then Y and then ENTER.

You can now run the program using the command:

Output, when button pressed (RED tactile switch, U2):
pi@raspberripi:~$ sudo python
Button Pressed
Button Pressed
Button Pressed
Button Pressed

What is the difference between BOARD and BCM for GPIO pin numbering?

The GPIO.BOARD option uses the pins exactly as they are laid out on the Pi – i.e the numbers printed on the board (e.g. P1)

The GPIO.BCM option option uses the Broadcom SoC numbering, which differs between version 1 and version 2 of the Pi, these are the numbers after “GPIO” in the rectangles around the outside of the below diagrams:

Raspberry-Pi-GPIO-Layout-Revision-1 Raspberry-Pi-GPIO-Layout-Revision-2.2
Revision-1 Revision-2
Unfortunately the BCM numbers changed between versions of the Model B, and you’ll need to work out which one you have guide here. So it may be safer to use the BOARD numbers if you are going to use more than one pi in a project.


Button press / LED on

The following example will demonstrate how to use the above script to lit an LED while holdong the button, with very little change to the code. We will need to open the existing script in editor, using the following command:

We will need to add few more lines to the code, and we will also need to change the GPIO pin numbering from BCM to BOARD pin numbering. This is the third line in the script.

Change it to BOARD numbering as follows : GPIO.setmode(GPIO.BOARD) ## Use board pin numbering

As we have changed the GPIO pin numbering, we will need to change the actual pin number as well on the GPIO.IN pin. This is the fourth line in the script.

Change it to: GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## Enable pull-up resistor on pin 12

Also change the pin numbering on the input state line, right under the while True: line:

If you would like to save the script, to test it, do so. Should produce exactly the same output as the original script.

Make sure to save the script under a different name.

Otherwise just follow the tutorial.

Now we will define the LED output pin Tutorial 4 ,via adding the following line to the code, right under the fourth line:

The last line to add to turn the LED on will be inserted to the  ‘while loop'.

This will go right under the following line:

print('Button Pressed')

Save the script under the following name:

Run the program using the command:

Output, when button pressed (RED tactile switch, U2): The LED will lit, and remain lit until the script terminated.

To change the script to lit the LED while the button pressed, add the following line to the while loop, right under the time.sleep(0.2) line:


“Python uses indentation to determine the grouping of statements. You can use one or more spaces or one or more tab, just that within the same statement always use one or the other. Is recommended to use spaces instead of tabs, but is not required. Read here”

The final script should look like this:

Save the script under the same name:

Run the program. The LED should lit while the button is pressed, if the button released the LED will go OFF.


 Next Tutorial (Tutorial 8 Input (Button Function) Part 2)