A while back, I posted about a Raspberry Pi sound-based sculpture project I collaborated on with my friend Emily Counts. We recently built a follow-up project called Super Mode, which is on display now at Museum of Arts and Design in New York.
Super Mode is physically larger than it’s predecessor, and has a total of 21 buttons. After about a month of being in the gallery, it started having some technical issues due to overheating and SD card corruption. We didn’t have these problems with the previous project because it was installed in a small gallery, and we were able to make sure that the Raspberry Pi got shut down properly each night (using a custom shut down button that was handled in our Python script). Because this new project is installed in a larger gallery, the Raspberry Pi needs to run for longer stretches of time, and it needs to handle being abruptly powered off. It also needs to potentially withstand more heat.
In other words, it needs to run in a true standalone kiosk mode.
I wanted to share what I learned through trial and error as we honed our Raspberry Pi image into something durable enough withstand this tougher environment.
Use Raspbian Lite
Before this project, the only Raspberry Pi OS I had ever used was Raspbian. Raspbian is designed to be an easily-accessible OS, with a GUI (graphical user interface), and a lot of bells and whistles that are only worthwhile when your Pi is connected to a monitor.
But for a Raspberry Pi that’s running all day inside a box in a gallery, Raspbian Lite is a much better OS for the job. Raspbian Lite is a stripped down version of Raspbian. There’s no desktop GUI, so you’ll have to set everything up using the Linux command line. But it’s worth it, and will make a difference to how efficiently your Pi runs.
Download the Raspbian Stretch Lite image and install it on your SD card. Etcher is a free program that makes this process really easy.
Getting things set up
If you’re not used to the Linux command line, even basic tasks can be a little intimidating. I wanted to spell out some things that weren’t obvious to me the first time I up my Raspberry Pi with Raspbian Lite.
Automatically log in
By default, Raspbian Lite will require you to log in using the default Raspberry Pi credentials. When you first start it up, you can log in:
username: pi
password: raspberry
For your Pi to work in kiosk mode, you need your Raspberry Pi to log in automatically after it starts up.
- Type in
sudo raspi-config
to access your Raspberry Pi basic configuration UI. - Using your keyboard’s arrow keys, select Boot Options, then Desktop / CLI, then select Console Autologin
You’ll be asked to reboot. When it does, it should automatically log you in.
Get your Pi on Wi-Fi
If you need to get online (for example, to access your Git repository), you’ll need to set up your Pi’s internet connection. Raspberry Pi model 3 devices have Wi-Fi built in, but you’ll still need to configure it. For a normal home router set up (with a password), it’s pretty simple:
- In the terminal, type the following:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
This should open an existing configuration file in the text editor Nano. It should already have some text in it. - Arrow to the bottom of the file, and add the following, replacing
router-name
with your router’s name, androuter-password
with the password (keep the double quotes):
network={
ssid="router-name"
psk="router-password"
}
- Press Control-X to exit Nano, press Y to save, then press Enter.
- Now type
sudo reboot
When your Pi reboots, you should be connected to your router. More details and options are here.
Download and install stuff
Now that you’re on Wi-Fi, you can install programs and libraries you need. For me, I only needed two packages that aren’t already in Raspbian Lite: Git (where my code repo lives), and Pygame (my Python script uses Pygame for sound playback).
First, type the following to update apt-get:
sudo apt-get update
Now you can install packages.
sudo apt-get install python-pygame
sudo apt-get install git
Now that I have Git, I can clone my repo. I’ll make sure I’m in the pi directory, add a folder called repos, then clone it there.
cd ~
mkdir repos
cd repos
git clone https://www.github.com/stenobot/SoundMatrixPi
Adjust audio settings
In kiosk mode, you need everything to be configured correctly, including audio settings. This is especially important for my project, as I need to make sure audio is going out of the headphone jack, and not the HDMI port. This can be set up back in raspi-config:
- Type in
sudo raspi-config
to access your Raspberry Pi basic configuration UI. - Using your keyboard’s arrow keys, select Advanced, then Audio, then select Force 3.5mm
This will force audio to always go out of the headphone jack.
Raspbian Lite system volume tends to be pretty low by default, but you can turn it up. The easiest way is to type the following:
amixer set PCM,0 90%
The volume in Raspbian Lite seems to default somewhere around 50% or 60%. Turning it up to 90% or 95% gets the volume to a more reasonable level.
Launch script on startup
If you’re following this tutorial, you will almost certainly need your Pi to automatically run a script when it boots. In my previous post, I recommended using Linux Crontab. This was a mistake, as Crontab will not work when a Raspberry Pi is forced into read-only mode (we’ll cover this in the next step). There are a lot of ways besides Crontab. The one I prefer is to run a shell script after log in is complete. This shell script’s only job is to run my main python script. You can add a line in the .bashrc file to run the shell script.
Make sure your in the pi directory, then create a launcher shell script. I’ll call mine “launcher.sh”:
cd ~
sudo nano launcher.sh
In Nano, have the shell script navigate home, then to the folder where your script lives, the run the script, then navigate back home:
cd /
cd home/pi/repos/SoundMatrixPi
sudo python core.py
cd /
Press Control-X, Y, then Enter to exit Nano and save your new file.
Next, make your script executable:
sudo chmod 755 launcher.sh
Then test the shell script to make sure it works. It should run your python script (mine will run core.py):
sh launcher.sh
Press Control-C to stop the running script.
Next we set .bashrc to launch our shell script for us. Go home, then open .bashrc:
cd~
sudo nano .bashrc
In Nano, arrow down to the very bottom of the .bashrc file, and add this:
./launcher.sh
Press Control-X, Y, then Enter to exit Nano and save the .bashrc file.
Now run sudo reboot
. If everything worked, your shell script should run automatically, which will in turn run your python script!
Read-only mode
Now for the coolest part.
When a Raspberry Pi gets unplugged, it’s possible for the SD card data to get corrupted. This isn’t likely, but it can happen, particularly if the Pi is running for long stretches and loses power while it’s in the middle of doing something. And if this happens while your Raspberry Pi is in a gallery on the other side of the country… well, that’s bad.
So how can you prevent this? By not allowing it to write to the disk in the first place! This solution is perfect for my project. We’re not saving any data to the disk. We just need our Python script to run and play sounds when buttons are pressed.
Important Note: This is irreversible! it won’t hurt your Raspberry Pi hardware, but it will “freeze” your SD card in time. You won’t be able to make changes to it anymore (without re-imaging the card, that is). So, make sure you do this step last! And back up your SD card before you do it!
Adafruit has made this process very easy. Just download their read-only shell script, and run it. I won’t copy the code here, since I didn’t write it, and the Adafruit page has a lot of other useful info that’s worth reading before you commit to this.
Adafruit’s read-only Raspberry Pi script
After the script runs, your Pi will be frozen in time. I highly recommend this step if your Raspberry Pi needs to run unattended for a long time.
By the way: Shout out to the crazy person at Adafruit who wrote this shell script. Very cool stuff.
Fans and heat sinks
There are a bunch of fans and heat sinks that you can buy to cool your Raspberry Pi down. They are generally pretty cheap, and can help to cool down the Pi a bit. But if your Pi is regularly overheating, simply adding fans and heat sinks probably won’t solve the issue. It’s likely that the Pi is in a place where it’s just too hot, or there’s not enough ventilation. But they won’t hurt, either, so I recommend at least adding some heat sinks.
Thanks for reading! Hope this info is helpful to someone out there!