GPIO Simple Input Example

 

Intro

 

Each of the 8 GPIO pins can be used as an input or an output.   When a pin is setup in input mode it can detect a high or low voltage.

 

 

The Board

To get started set up your board like this:

LED to pin 22 (GPIO 13)

Button to pin 17 (GPIO 11)

 

File 2015-10-21, 1 12 57 PM

The Code

 

#This program demonstrates how to use an input and out pin on the GPIO

 

import RPi.GPIO as GPIO

 

 

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.IN)

GPIO.setup(13,GPIO.OUT)

 

 

while(True):

    input = GPIO.input(11)

    if(input):     

        GPIO.output(13,GPIO.HIGH)

        print('hi')

    else:

        GPIO.output(13,GPIO.LOW)

        print(‘bye’)

 

   

 

 

 

 

Note: input is a variable that stores the state of pin 11 as either True(high) or False (low).  That is why you can put it by itself in an if statement.

 

 

 

 

Important!!!  Pin 11 is actually GPIO pin 17

There is a card in your kit that shows you the correspondence.  Use the green numbers in your code, the grey numbers are on the board.