APC Australia - September 2019

(nextflipdebug2) #1

howto»raspberry pi masterclass


T


hisissuewe’lllearnhowtouse
theExplorerHATProtowork
withanalogueelectronicsby
creatingtwoprojects.First we
createasimplelight-detecting
nightlight,thenwecreatean
electroniccandlethatwecanblow
out.Butfirstweneedtosetupour
equipment.
Withthepowerturnedofftoyour
RaspberryPi,placetheExplorerHAT
Prosothatitconnectstoall 40 GPIO
pins,andsothatitfitsneatlyabovethe
Pi.Nowconnectyourkeyboard,mouse,
HDMIandsoontoyourPiandbootup.
Forthenextstepyouwillneedto
connecttotheinternetusingWi-Fior
Ethernet.Onceconnected,opena
terminal,theiconforwhichisfound in
thetopleftofthescreen.Inthe
terminaltypethefollowingto
automaticallyinstallandconfigure
theExplorerHATProsoftware:
$ curl https://get.pimoroni.
com/explorerhat | bash
Duringtheinstallyouwillbeasked
toconfigureI2Candtoinstallexample
codewhichyoucangetfromhttp://bit.
ly/lxf252pihat.Answeryestothese
questions.Onceinstalled, close the
terminalwindow.

PROJECT1:NIGHTLIGHT
Tointroduceanalogueelectronicswe
shallusethemostsimpledevice,alight
dependentresistor(LDR).This
componentchangesresistancebased
onlightlevels.Forthisprojectweshall
placetheLDRonthebreadboardand
connectonelegto3.3V,andtheother
toGNDviathe22Kohmresistor.Then
weconnectAnalog 1 fromtheboardto
thesamelineasourresistorandLDR.
Wedothisasitcreatesavoltage
dividerthatweusetomeasurethe
lightlevelasavoltage.Seethediagram
inthedownloadforanaccurate
reference.
Wenowmoveontothecode.Using
yourfavouriteeditor(IDLE,Mu,
Thonny)createanewfileandthensave
thatnewfileasnightlight.py.
Remembertosaveoften!Westartthe
codewithtwoimportedlibraries,
time and explorerhat ,whichwe
renameto eh forbetterconvenience.
import time
import explorerhat as eh

Using analogue


HAT interfaces


Les Pounder showsustwomore projects using the deceptively simple


Explorer HAT Pro – and a little Python code.


Thenwecreatealoop to constantly
runourcode.
while True:
Let’stakealightlevelreadingand
storeitinsideavariablecalled light


  • thiswillshowthevoltagethatour
    circuitisproducingbasedonthe
    resistanceleveloftheLDR.Thisis
    printedtothePythonshellfordebug.
    light = eh.analog.one.
    read()
    print(light)
    Wethenuseaconditionaltestthat
    checkstoseeifthevoltagedetectedis
    lessthan2V.Ifthatisthecasethenit
    mustbegettingdark,sothefourLEDs
    ontheExplorerHATProwillturn on.
    if light< 2.0:
    eh.light.on()
    Butifitdaylightthenthevoltage
    willbeover2Vandsothe else
    conditionactivatesand turns off the
    ExplorerHATLEDs.
    else:
    eh.light.off()
    Savethecodeandwhenready,runit.
    CovertheLDRwithyourhandandthe
    LEDsshouldlightup;moveitandthe
    LEDsturnoff.Ifyouneedtotweakthe
    values, take a look at the Python shell


toseewhatvoltagechangesthereare
whenthesensordetectsdarkness.

PROJECT2:ADIGITALCANDLE
ThisprojectisinspiredbytheNintendo
DS,specificallyasectioninTheLegend
ofZelda:PhantomHourglasswhereyou
areinstructedto‘blowout’fire/torches
inthegame.Todothisweblowonto
theconsoleandthegamereacts.In
realityweareblowingontoa
microphoneandananaloguesignalis
beingmeasuredtoactasatrigger.To
replicatethisweshallcontroltheLEDs
presentontheExplorerHATProusing
amicrophoneboard.Themicrophoneis
connectedtoourExplorerHATProas:
GND>GND,OUT>Analog 1 andVCC>
5V.
Wenowmoveontothecode.Createa
newfileasbeforeandthensavethat
newfileascandle.py.Thefirstthree
linesofourPythoncodeimport
prewrittenlibrariesofcode.Thefirstis
called time andweusethatto
controlthespeedofourproject.Next
weimportthe explorerhat library,
butrenameitto eh foreaseofuse.
Lastlyweimportthe random library
which will be used for our candle

Thissimplecircuitof fersagreatintroduction
toanalogueelectronics.Itrequiresonlyafew
cheap components and is accessible to all.
Free download pdf