ExplorerHAT Cheatsheet

Simple Outputs

Wiring

images/led.png

Code

import explorerhat
from time import sleep
explorerhat.output.one.on()
sleep(3)
explorerhat.output.one.off()

Additionally the following methods are supported

toggle()Changes the output to its opposite state
blink( on_time, off_time )Turns the output on for “on_time” and then off for “off_time”
pulse( fade_in_time, fade_out_time, on_time, off_time )Same as blink, but lets you fade between on and off
fade( from, to, time )Fade from 0-100 to 0-100 brightness over a number of seconds specified by “time”
stop()Stops any running blink, fade or pulse action

Simple Inputs

Wiring

images/button.png

Code

Reading the value of an input

import explorerhat
print(explorerhat.input.on.read())

Calling a handler functionn

import explorerhat

def push(input):
    state = input.read()
    if state == 1:
        print('You pushed me')
    else:
        print('You released me')

explorerhat.input.one.changed(push)

or alternatively:

import explorerhat

def pushed(input):
    print('You pushed me')

def released(input):
    print('You released me')

explorerhat.input.one.on_high(pushed)
explorerhat.input.one.on_low(released)

PIR

Wiring

PIRExplorerHAT
VCC5V
OUTINPUT #
GNDGND

images/pir.png

Code

Reading the value of the PIR

import explorerhat
print(explorerhat.input.on.read())

Calling a handler function

import explorerhat

def moving(input):
    state = input.read()
    if state == 1:
        print('MOVING')
    else:
        print('NOT MOVING')

explorerhat.input.one.changed(moving)

Analog Inputs

Wiring

images/ldr.png

Code

import explorerhat

print(explorerhat.analog.one.read())

or detect a change, for example in the code below this is looking for a change of only 0.01mV

import explorerhat

def light(channel, event):
    print('Light changed')
    
explorerhat.analog.one.changed(light,0.01)

Motor

Wiring

images/motor.png

Code

import explorerhat
from time import sleep

explorerhat.motor.one.forward(100)
sleep(3)
explorerhat.motor.one.backward(100)
sleep(3)