LED blink
This example demonstrates how to use UPER GPIO output to blink the red LED. It uses the on-board RGB led and requires no additional components.
Requirements:
- Uper1 board
Code:
from time import sleep from IoTPy.core.gpio import GPIO from IoTPy.pyuper.uper import UPER1 LED_PIN_ID = 27 with UPER1() as board, board.GPIO(LED_PIN_ID) as redPin: redPin.setup(GPIO.OUTPUT) # set GPIO pin to be output while True: redPin.write(0) # Turn led ON (LED on board is common anode - therefore inverted) sleep(0.5) redPin.write(1) # Turn led OFF sleep(0.5)
LED multicolor blink
In this case we use all LEDs and create a random color blinker. Just like before - out of the box UPER board is enough.
Requirements:
- Uper1 board
Code:
import random from time import sleep from IoTPy.core.gpio import GPIO from IoTPy.pyuper.uper import UPER1 with UPER1() as board, \ board.GPIO(27) as redPin, board.GPIO(28) as greenPin, board.GPIO(34) as bluePin: pins = [redPin, greenPin, bluePin] for pin in pins: pin.setup(GPIO.OUTPUT) # set GPIO pins to be output while True: pin = random.choice(pins) # choose random rgb pin pin.write(random.choice([0, 1])) # randomly turn that pin on or off sleep(0.2)
Button press detect
This example shows how to use GPIO read function to determine push-button state.
Requirements:
- Uper1 board
- Breadboard
- Push-button
- 10k (or similar) resistor
- Wires
Schematic:
Code:
from IoTPy.core.gpio import GPIO from IoTPy.pyuper.uper import UPER1 with UPER1() as board, board.GPIO(27) as redPin, board.GPIO(18) as buttonPin: buttonPin.setup(GPIO.INPUT, GPIO.PULL_UP) redPin.setup(GPIO.OUTPUT) oldState = buttonPin.read() redPin.write(oldState) nButtonPress = 0 nButtonRelease = 0 # This might look complicated, but all of the code in the while loop # can be replaced with redPin.write(buttonPin.read()) # Variables and counters are here just for the print messages. while True: newState = buttonPin.read() if oldState != newState: oldState = newState if newState == 0: nButtonPress += 1 print "Button pressed %i" % nButtonPress redPin.write(0) # Turn led ON else: nButtonRelease += 1 print "Button released %i" % nButtonRelease redPin.write(1) # Turn led OFF
SHT10 sensor
SHT1x family sensors use a modified (non-standard) I2C protocol, therefore we have implemented a bit-banged SHT1x sensor class.
Requirements:
- Uper1 board
- SHT10 breakout board
- Breadboard
- Wires
Schematic:
Code:
from time import sleep from IoTPy.pyuper.uper import UPER1 from IoTPy.things.sht1x import SHT1X with UPER1 as board, board.GPIO(1) as data, board.GPIO(2) as sck, SHT1X(data, sck) as sensor: while True: print "Temperature: %.1f Humidity: %.1f" % (sensor.temperature(), sensor.humidity()) sleep(1)