The Cracked Bassoon

Sam Mathias

I'm a psychoacoustician. I mainly post code for making interesting sounds.

22. July 2016 842 words ~6 minutes read 1 Comment

Running psychological experiments on a Raspberry Pi

The Raspberry Pi is a credit-card sized, single-board computer developed for hobbyists and educational purposes. Despite its low price (around $35 in the USA), the most recent version of the Pi — the Raspberry Pi 3 Model B — is surprisingly capable. It has a quad-core, 64-bit processor, 4 USB ports, HDMI output, and onboard Bluetooth and wireless. Given that the capabilities of the Pi are about on a par with those of the machines I was using when I started doing research as an undergraduate, I've decided to test the feasibility of the Pi for psychological testing. They definitely have potential: Pis are small and therefore easily portable, very quiet on account of having no moving parts, and really really cheap. In this post, I'm going to describe some basic steps to get experiments up and running on a Pi. In future posts, I hope to do some more rigorous benchmarking, like measuring reaction times, testing sound quality, and so on.

Hardware

The Raspberry Pi 3 Model B was released in February 2016 and is currently fastest model available. I own three Pis — a Raspberry Pi 1 Model B+, a Raspberry Pi 2 Model B, and a Raspberry Pi 3 Model B — and the Pi 3 is noticeably more responsive than its predecessors. There are other models available, but the Raspberry Pi Foundation doesn't recommend these unless you are specifically developing some kind of embedded system. Here is a picture of my set up, with a bobblehead for scale:

I've connected up a cheap-and-nasty USB keyboard and mouse, an equally cheap-and-nasty HDMI monitor, and a cheap (but actually good!) pair of Sennheiser HD 280 Pro headphones. I'm using one of the recommended power supplies and a SanDisk Ultra 64GB microSDXC UHS-I Card. The choice of SD card was actually a bit of a gamble since I wasn't sure it would be compatible with the Pi (I picked it up on an Amazon Prime Day sale), and it did require a little extra effort to get it to work.

Installing raspbian

Before booting the Pi for the first time, an operating system needs to be installed on the SD card. The Foundation recommends installing SDFormatter on a Windows PC or Mac with a card reader, formatting the card from that machine, and then transferring NOOBS onto the card. This didn't quite work for me. For cards bigger than 64 GB, SDFormatter will automatically use the exFAT file format, which the Pi doesn't support out of the box. The solution was to format the card as FAT32 using Mac's built-in Disk Utility.

After copying over NOOBS, its time to plug everything into the Pi (including the SD card) and boot for the first time. Following the on-screen instructions installs raspbian in about 10 minutes.

Raspbian is the default choice of OS for the Pi and is designed to be easy to use. Consequently, most things will work straight after installation, but I had to fix a couple of minor issues. To update, open a terminal and type:

sudo apt-get update
sudo apt-get upgrade
sudo reboot

A little annoyance is that the ping command doesn't initially work without super-user privileges. To fix this:

sudo chmod u+s `which ping`

If your visual display doesn't completely fill your screen and there are black bars surrounding it, you need to enable ‘overscan’ in the file /boot/config.txt. Here's the contents of mine:

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
disable_overscan=0
# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=-16
#overscan_right=-16
#overscan_top=-16
#overscan_bottom=-16

Note that negative numbers increase overscan (make the screen bigger).

Python packages

I use Python for most coding jobs, including running experiments. Raspbian includes Python 2.7, NumPy and Pygame, but not several other essential packages. To install them:

sudo apt-get install python-scipy python-pandas python-qt4 python-paramiko
sudo pip install brian web.py

Charlie

Charlie is a collection of Python scripts for running neuropsychological tests (written by me!). It is currently being used for a project in our lab. You can use git to clone the repository, and run a simple test as follows:

git clone https://www.github.com/sammosummo/Charlie.git
cd Charlie
python run.py -t orientation

Some initial impressions

The Pi 3 is more than capable of running simple psychological tests. I didn't try out all of the Charlie tests, but the ones I did try worked perfectly. The fact that raspbian is a lightweight OS is great; there are no background processes slowing things down (this happens all the time on Windows). All in, it took less than an hour to get everything working.

One big initial disappointment is the sound quality out of the audio jack. It's very poor. I'm not sure whether the poor quality is related to Pygame, the jack itself, or the sound capabilities of the board. It's something to investigate next.


References

Banner image is The Card Players by Jean-Louis Ernest Meissonier.

1 Comment

  1. Léo

    just saw your blog, the post is a year old and maybe you figured it out, but the audio output on Raspberry is just a crappy PWM with a resolution of 8 bits, it's noisy as hell and the best thing to do is buy some cheap USB "soundcards" off aliexpress for example for a couple of dollars to get something more or less decent 😉

Leave a Reply

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