Adding a Shutdown Button to the Raspberry Pi B+ · All Places > Raspberry Pi > Raspberry Pi...

Click here to load reader

  • date post

    15-Oct-2018
  • Category

    Documents

  • view

    235
  • download

    3

Embed Size (px)

Transcript of Adding a Shutdown Button to the Raspberry Pi B+ · All Places > Raspberry Pi > Raspberry Pi...

  • AllPlaces>RaspberryPi>RaspberryPiProjects>Documents

    Introduction

    Whatdoyouneed?Step1.SetuptheRPi

    Step2.Connectingthebutton

    Step3.WritingaPythonScript

    Step4.Addingittostartup

    Step5.Moretodo

    IntroductionForabeginnertotheworldofraspberrypi,thereareanumberofprojectsthatcanbecomethestartofsomethingbig.Inthisarticle,Idiscusssuchasimpleprojectwhichisaddingabuttonthatcanbeusedtoshutdowntheraspberrypiusingabitofsoftwaretinkering.Iwroteasimilararticlein2013atmyblogembeddedcode.wordpress.comanditsgotitsshareofattentionsincealotofpeoplestartingoutwithasingleboardcomputer,keptlookingforapowerbutton.Additionally,thosewhowantedaheadlesssystem,neededawaytoshutdownthecomputerwithoutthemessofconnectingtoitoverthenetworkorattachingamonitorandkeyboardtoit.Inthisarticle,Irevisitthetutorialonhowtoaddashutdownbuttonwhiletryingtoexplaintheworkingsandperhapsbeginnerswillfinditanamusingtoaddfindmorethingstodowiththislittlerecipe.

    Whatdoyouneed?Hereisabasicbillofmaterialsrequiredforthisexercise.RaspberryPiB+(ProductLink)ApushbuttonConnectingwiresBreadboardOR...Nipper&SolderingIronandrelatedstuff(notnecessary)TheRaspberryPiB+canbereplacedbyanotherPimodelbutforthisexercise,IwillbeexplainingthingswiththeRPiB+asareference.Thepushbuttonalsodependsonavailabilityandpreference.DoyoupreferaBIGREDBUTTON?Noproblemusethat.TheConnectingwiresarerequiredsinceweneedtoconnectthebuttontotheRaspberryPiGPIOs.Iwillstartwithusingthebreadboardandupgradetoasolderedbutton.Incaseyouhavesomethinglikeabuttonbreakoutwhichdoesnotneedsoldering,thatsfinetoo.Wewillmakethesedecisionsattheend.Rightnowletsgetprototyping.

    Step1.SetuptheRPiWeneedaraspberryPifullysetupwiththelatestversionofRaspbianOS.Sincetheyouprobablyhavethatdone,Iwontgointothedetailsbutforthosewhohavenotdoneityet,Ihaveafewlinksyoumightliketovisit.https://embeddedcode.wordpress.com/2013/07/10/thegeekgettingstartedguidetotheraspberrypi/

    https://www.raspberrypi.org/help/noobssetup/

    AddingaShutdownButtontotheRaspberryPiB+Createdbyipv1onAug4,20153:05AM.Lastmodifiedbyipv1onAug18,20159:52AM.

    Version1

    Welcome,Guest Login Register Activity Translate

    SearchSearchwithincontent,membersorgroups

    Topics Resources Members DesignCenter Store

    Content

    https://www.element14.com/community/placeshttps://www.element14.com/community/community/raspberry-pihttps://www.element14.com/community/community/raspberry-pi/raspberrypi_projectshttps://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/content?filterID=contentstatus[published]~objecttype~objecttype[document]https://www.element14.com/community/external-link.jspa?url=https%3A%2F%2Fembeddedcode.wordpress.com%2F2013%2F07%2F10%2Fthe-geek-getting-started-guide-to-the-raspberry-pi%2Fhttps://www.element14.com/community/external-link.jspa?url=https%3A%2F%2Fwww.raspberrypi.org%2Fhelp%2Fnoobs-setup%2Fhttps://www.element14.com/community/people/ipv1https://www.element14.com/community/people/ipv1https://www.element14.com/community/welcomehttps://www.element14.com/community/create-account.jspahttps://www.element14.com/community/newshttps://www.element14.com/community/guest-preferences!input.jspajavascript:void(0)https://www.element14.com/community/placeshttps://www.element14.com/community/places?filterID=all~tag[resources]https://www.element14.com/community/community/membershttps://www.element14.com/community/community/designcenterhttps://www.element14.com/community/content
  • https://www.raspberrypi.org/help/quickstartguide/Connectupamonitor,keyboard,mouse,ethernetcable(notnecessary)andthepoweradaptor.BootuptheRPiandlogin.

    Step2.ConnectingthebuttonNextweneedtoaddthebutton.Iaddedapushbuttontothebreadboardandaddedtheconnectingwires.Thispartwillvarywiththebuttonavailability.NowwheredoweconnectittotheRPi?Wellletstakealookatthediagrambelow.

    FromthediagramitsclearthatsomeoftheGPIOshavedualrolesaswellsuchasUART,I2C,SPIetc.FromtheseIchoseGPIO18becauseitsneartheGNDorgroundpinbutyoucanchooseonethatfitsyourneed.IsuggeststayingawayfromthedualpurposepinssinceyouneverknowwhenyoumightwanttoaddanSPI,I2Corserialperipheralinyounextproject.Theimagebelowshowsmysetupandyoushouldmakesureyouconnectedthepinsright.

    FeedbackSurvey

    nothanks[x]

    Whenyouaredonewithyourvisittooursite,pleasefilloutourfeedbacksurvey.

    StarttheSurveyNow!poweredindependentlybyQualtrics

    https://www.element14.com/community/external-link.jspa?url=https%3A%2F%2Fwww.raspberrypi.org%2Fhelp%2Fquick-start-guide%2Fhttps://www.element14.com/community/servlet/JiveServlet/showImage/102-78055-1-227625/1.jpgjavascript:void(0);https://element14.eu.qualtrics.com/SE/?SID=SV_ex5DNLnpewqc7k1
  • Step3.WritingaPythonScriptThereareotherwaystodoashutdownscriptbutovertime,Ihavecometotheunderstandingthatsincepythonhasgainedsomuchpopularity,itsbettergothisrouteandifyouwanttoextendfunctionalitytosaystartingorstoppingaservice,wecandothataswell.Inordertokeepthingsorganised,wecreateafoldercalledScriptsandthenanemptyfilecalledshutdown_pi.py

    +expandsource viewplain

    Nextweneedtoeditthefileandaddthecontents.Executethecommand

    +expandsource viewplain

    andthentypethefollowingintoit.

    +expandsource viewplain

    SavethefilebypressingCTRL+Xandtheny,EnterNotethatinPython,youdonotneedparentheses({})tocreateasubsection.Thetabbedorindentedformattellstheinterpreterthatitsasubblocksopleasemakesurethatthespacingmatchestheabovecode.NowsinceourpythonscriptistryingtoaccesssomeGPIOsandtryingtoshutdownthesystemthisscriptsneedstoberunasroot.Totestitsworking,enterthefollowingcommand

    +expandsource viewplain

    01. mkdirScripts02. cdScripts03. touchshutdown_pi.py

    01. nanoshutdown_pi.py

    01. #!/bin/python02. #SimplescriptforshuttingdowntheraspberryPiatthepressofabutton.03. #byInderpreetSingh04. 05. importRPi.GPIOasGPIO06. importtime07. importos08. 09. #UsetheBroadcomSOCPinnumbers10. #SetupthePinwithInternalpullupsenabledandPINinreadingmode.11. GPIO.setmode(GPIO.BCM)12. GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)13.

    01. sudopythonshutdown_pi.py

    https://lh3.googleusercontent.com/7Pt-YEAgHs53P_ClRovFCyZTdPm5qdBG2hpdMm-d3WE=w1343-h755-no
  • Thesudoensuresthatpythonisgivenalltheprivilegesforthisrun.Ifatthispointyoupressthebutton,theRPishouldshutdown.Ifitdoesntthenyouneedtocheckthescriptaswellastheconnections.NoPullupresistancesareneededsinceweenabletheinternalpullupsonthePiitself.

    Step4.AddingittostartupWeneedourpythonscripttorunautomaticallyeverytimetheRPistarts.ForthisweneedtodoalittlemanualeditingoftheRPIgeneratedfiles.Runthefollowingcommand

    +expandsource viewplain

    ThisfileiswhatgetsexecutedeverytimeyourRPibootsup.Weneedtoaddourpythoncommandbeforethelastlinewhichclosestheifloop.Therefore,addthefollowinglinebeforethe#fiattheendofthefile.

    +expandsource viewplain

    PleasenotethateverythingiscasesensitivesoScriptsisnotthesameasscripts.The&attheendofthecommandtellsittoruntheprocessinthebackground.Ifyouomitit,thenyourloginpromptprobablywillnotappear.

    NowreboottheRPi.Thatsit!Youaredoneandonceyougeteverythingworking,youcanstartmakingmodificationstothescriptforaddingmorebuttonstostartaprocesssuchasaGUIetcorkilltheprocess,sendacommandoverthenetworketc.ThepossibilitiesareendlessandIwouldlovefeedbackonhowyoumadeyourownshutdownbutton.

    Step5.MoretodoSincewehaveaworkingprototypewemaywanttoimprovetheergonomics.IsimplesolderedtwowirestothepressbuttonandusealittlehotgluetostickthebuttontothesideoftheRPiCase.Youmaychosetouseapanelmountbuttonbuttheconnectionsremainthesame.

    Ifyouhappentohavea3Dprinter,thenyoumayprintyourowncasesuchastheoneat( [ProjectVIRUS][Week6]Abiton3Dprinting)Havefun!IP

    12914Views Categories: Tags:raspberry_pi,rpibeginner,ip_iot

    01. sudonano/etc/rc.local

    01. sudopython/home/pi/Scripts/shutdown_pi.py&

    https://lh3.googleusercontent.com/xqSSY96MxYLQl4DhE1bArwvpRMqzCkumMVz-ZGvVZCM=w1343-h755-nohttps://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/06/08/project-virusweek-6-a-bit-on-3d-printinghttps://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/tags#/?tags=raspberry_pihttps://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/tags#/?tags=rpibeginnerhttps://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/tags#/?tags=ip_iot