Turn Raspberry Pi off using Adafruit LCD & Keypad

So I’ve just got one of these and wanted to turn it off if you hold down one of the buttons for a short period of time.

This is the quick python script I’ve come up with.


import time
import os
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate

# Initialize the LCD plate. Should auto-detect correct I2C bus. If not,
# pass '0' for early 256 MB Model B boards or '1' for all later versions
lcd = Adafruit_CharLCDPlate()

# Clear display and show greeting, pause 1 sec
lcd.message("Booted up!")

pressed_time = None

while True:
	if lcd.buttonPressed(lcd.UP):
		lcd.message("Starting up \nCrazyFlie!")

	elif lcd.buttonPressed(lcd.DOWN):

	elif lcd.buttonPressed(lcd.SELECT):
		if pressed_time is None:
			#just pressed
			pressed_time = time.time()
		elif time.time() - pressed_time >= 3.0:
			lcd.message("Shutting Down")
			os.system("sudo shutdown -h now")

		pressed_time = None


I run this python script on boot. I’ve got a mini bash script which I call in rc.local

sudo python /home/pi/lcd/boot.py

Then in /etc/rc.local just before exit 0 put

/home/pi/lcd/autostart.sh &

Boom, done. Now when you hold down the Select button for more than 3 seconds your pi will turn off nicely

3 thoughts on “Turn Raspberry Pi off using Adafruit LCD & Keypad

Sam Salt

I actually tried to duplicate this for my Pi. I’m very new to coding and semi-n00b to Unix systems… I ended up getting it to where it would actually work, but my system started hanging at SSH on boot-up, i couldn’t SSH and remove the script so I had to do a complete reinstall. Not sure why. I had the script in /etc/init.d. I couldn’t really understand where to put the script and how to actually use it, so I did a lot of googling and came up with something lol. Could you tell me exactly what to do (aside from writing the script, obviously) to make it work and actually boot up? Something else I was wanting it to do, was after boot ,while system is on, i want it to display date/time on first line, and then IP being used on second line, and possibly use the buttons to scroll through certain information, ie: CPU usage, Storage available, time system has been on, etc…


    Christopher Rogers

    Hi Sam

    So what I have done is I have a directory (folder) in the home directory (/home/pi) called lcd (so /home/pi/lcd), but it should really matter where you put it.

    So in /home/pi/lcd (or wherever you want) make a new file called boot.py and put the long python code in there. Save and close that file, then make a new file called autostart.sh, and in that put the second 2 line bit of code above.
    Then edit /etc/rc.local (you will need to be root to do this, so use a command like sudo leafpad /etc/rc.local, replace leafpad with whatever text editor you are using (eg nano) if you want.

    Make sure to put /home/pi/lcd/autostart.sh & above the exit 0 on the last line, and don’t forget the &

    What you probably did by putting it in /etc/init.d/ was to not start it in the background (that is what the & does), and thus have your script running in a while true loop (so it runs forever), blocking anything else from starting.


    Christopher Rogers

    As for your future plans they are all certainly possible!

    You’ll want to edit the boot.py to put the extra information on. You’ll need to learn some more Python though 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *