Button press detect

In GPIO example section we have shown how to detect button presses by using GPIO read() command. Now we will do the same task by using interrupts.

Requirements:

  • Uper1 board
  • Breadboard
  • Push-button
  • 10k (or similar) resistor
  • Wires

Schematics:

Code:

from time import sleep
from IoTPy.core.gpio import GPIO
from IoTPy.pyuper.uper import UPER1
 
 
def button_callback(event, obj):
    obj['counter'] += 1
    print "Interrupt %i" % obj['counter']
 
    button_state = (event['values'] >> event['id']) & 0x1
    obj['led'].write(button_state)
 
with UPER1() as board, board.GPIO(27) as redPin, board.GPIO(18) as buttonPin:
 
    my_object = {'led': redPin, 'counter': 0}
    buttonPin.attach_irq(GPIO.CHANGE, button_callback, my_object)
 
    redPin.setup(GPIO.OUTPUT)
    redPin.write(1)
 
    while True:
        sleep(0.5)

PIR sensor

This example demonstrates how to use HC-SR501 PIR sensor for motion detection applications, e.g. burglar alarm. The sensor outputs LOW signal when it is idle and HIGH when the motion has been detected.

Requirements:

  • Uper1 board
  • HC-SR501 sensor
  • Wires

Schematics:

Code:

from time import sleep
from IoTPy.core.gpio import GPIO
from IoTPy.pyuper.uper import UPER1
 
 
def pir_callback(event, obj):
    print event
    if event['type'] == GPIO.RISE:
        print "Movement detected"
        led.write(0)  # turn ON the led
    elif event['type'] == GPIO.FALL:
        print "No movement"
        led.write(1)  # turn OFF the led
    else:
        print "Unknown event"
 
with UPER1() as board, board.GPIO(27) as led, board.GPIO(18) as button:
 
    button.setup(GPIO.INPUT, GPIO.PULL_UP)
    button.attach_irq(GPIO.CHANGE, pir_callback, led)
 
    led.setup(GPIO.OUTPUT)
    led.write(1)
 
    while True:
        sleep(0.5)

Rotary encoder

This example shows how to use RotaryEncoder class to detect changes of the rotary switch position.

Requirements:

  • Uper1 board
  • Breadboard
  • Rotary switch (rotary encoder)
  • Two 10k resistors
  • Wires

Schematics:

Code:

from IoTPy.pyuper.uper import UPER1
from IoTPy.things.rotary_encoder import RotaryEncoder
from time import sleep
 
 
def on_rotation(direction, position):
    print "Rotated by %i, current position is %i" % (direction, position)
 
 
with UPER1() as board, \
        board.GPIO(1) as chan0, board.GPIO(2) as chan1, \
        RotaryEncoder(chan0, chan1, on_rotation) as encoder:
 
    while True:
        sleep(0.5)

In this example almost all of the work is done by the RotaryEncoder class - it detects changes on the rotary switch pins and determines the direction of the rotation. For the convenience it also tracks the absolute position. RotaryEncoder class takes three parameters: the first two are just Interrupt channels to the rotary switch pins and the third one is an optional user callback function which shall be called on every detected rotation. If you want, you can skip this third parameter and just track the absolute position via the position variable. You can also reverse the rotation direction by switching the signals either physically on breadboard (green and white wires) or in code (Interrupt GPIO IDs 1 and 2).