Jump to content
 
  • entries
    7
  • comments
    6
  • views
    11,211

7. Basic inputs


DavidB-AU

1,459 views

So far we have looked at outputs to control LEDs. Now let's look at inputs. There are two ways to detect an input.

 

blogentry-6959-0-89607300-1360745317.png

 

In Method 1:

  • GPIO is HIGH when button is down.
  • GPIO is LOW when button is up.

In Method 2:

  • GPIO is LOW when button is down.
  • GPIO is HIGH when button is up.

Either method can be used depending on the application.

 

Here's how it looks on the breadboard. Method 1 is on the left, attached to GPIO24 (pin18). Method 2 is on the right, attached to GPIO25 (pin 22).

blogentry-6959-0-69119600-1360745750.jpg

 

I have shown pushbuttons here but it can be any sort of on/off tigger. Other ways to trigger an input could include:

  • magnet passing over a reed switch
  • a photodiode detecting a UV light (or detecting when the light is blocked)
  • a relay triggered by a higher voltage application
  • a DCC inductive current

Now to control it. This time I am using a different Python extension module called curses which is a library of commands for character printing and keyboard handling on text-based terminals. This is particularly useful if you are using a small LCD screen (one of the RPi accessories) instead of full monitor. I won't go into describing how curses works. Full documentation is available here and a "how to" here. There is also a useful tutorial here (PDF).

 

Here's the code for a program called buttontest1.py.

import cursesimport RPi.GPIO as GPIO# Set up header pins 18 (GPIO24) and 22 (GPIO25) as inputGPIO.setup(18, GPIO.IN)GPIO.setup(22, GPIO.IN)stdscr=curses.initscr()                 # Initialise the screenstdscr.nodelay(1)                       # Act on key presses immediatelycurses.noecho()                         # Don't echo a key press on the screenwhile 1:  stdscr.addstr(0, 0, "Button 1 is")    # Writes text at position y = 0, x = 0  stdscr.addstr(0, 20, "Button 2 is")   # Writes text at position y = 0, x = 20  if GPIO.input(18):                    # Tests if button 1 on GPIO pin 18 is True (HIGH)    stdscr.addstr(0, 12, "DOWN")        # If True, that means the button is down. Print it on the screen in the right position.  else:    stdscr.addstr(0, 12, "UP  ")        # If False, that means the button is up. Print it on the screen in the right position.  if GPIO.input(22):                    # Tests if button 2 on GPIO pin 22 is True (HIGH)    stdscr.addstr(0, 32, "UP  ")        # If True, that means the button is up. Print it on the screen in the right position.  else:    stdscr.addstr(0, 32, "DOWN")        # If False, that means the button is down. Print it on the screen in the right position.  stdscr.addstr(1, 0, "")               # Moves the cursor to the next line out of the way  stdscr.refresh()                      # Refresh the screen  if stdscr.getch() == ord('q'):        # Checks if the 'q' key is pressed    break                               # If so, quit

Run the program as root.

pi@raspberrypi ~$ sudo python buttontest1.py

When you push down either or both of the buttons, this will be shown to you on the screen.

 

This is just a very simple demonstration of inputs. The input triggers could be anything and when you detect an input you can do anything. For example you could have a basic representation of a layout drawn using curses and use multiple inputs to display which tracks are occupied. Or you can use the inputs to trigger an output on another GPIO pin, but more on that later!

 

blogentry-6959-0-12417200-1360746822.png

0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...