Beginners guide to SPI on Raspberry Pi
In this guide, I will show you how to get SPI up and running on a Raspberry Pi, and how to control a SPI_LCD module. We will start from scratch, with a blank SD card.
Things we will be using
- Raspberry Pi
- 2GB (or larger) SD card
- Power supply for your Raspberry Pi
- Ethernet cable
- SD-card reader
- RPi Serial Breakout board
- SPI cable
- SPI LCD
Not necessary, but useful:
- Monitor with HDMI (or DVI and a HDMI-DVI adapter)
- HDMI cable
Software we will be using
Unpack the first two packages, I advise on making a seperate directory for this project, and put all the files there.
Preparing the SD card
First, we will flash the SD card with the Debian image. Plug your SD card in your favourite reader, and into your PC. My SD card became /dev/sdc on my laptop, your mileage may vary.
Copy the image to your SD-card:
sudo dd if=debian6-19-04-2012.img of=/dev/sdc
This may take some time, depending on the speed of your SD card, and card reader.
Next, we need to move the modified kernel image and modules to the freshly flashed SD card. For this, you need to mount the partitions on your SD card. Im lazy, and just unplug my card reader, en plug it back in. Ubuntu then automagically mounts the two relevant partitions.
You will see a small (75MB) partition (mounted as /media/95F5-0D7A on my system), which is the /boot partition/directory. You will also see a larger (approx 1.6GB) partition, containing all other data (mounted as /media/18c27e44-ad29-4264-9506-c93bb7083f47 on my system). You may need to change the target directory in the following commands.
Copy the relevant data:
cp rpi-spi-binary-kernel-20120608/boot/* /media/95F5-0D7A/ sudo rsync -aPv rpi-spi-binary-kernel-20120608/lib/ /media/18c27e44-ad29-4264-9506-c93bb7083f47/lib/ cp bw_lcd.c /media/18c27e44-ad29-4264-9506-c93bb7083f47/home/pi/
Enable ssh on your pi:
mv /media/95F5-0D7A/boot_enable_ssh.rc /media/95F5-0D7A/boot.rc
And your SD card is ready!
Unmount your CD card
sudo umount /media/
And remove the SD card from your reader.
You might want to hook up your pi to a monitor, but I usually find that to much of a hassle. Hooking up your Pi to your network, however, is necessary.
Mount the RPi-serial board on your Pi, connect the SPI cable to the SPI0 port (mind the polarity!), and connect the other end of the SPI cable to your SPI_LCD module.
Plug your freshly flashed SD card in your Pi, hook up power, and wait... If you haven't connected a monitor, you can remove power to your Pi after 2 minutes, and reconnect it. If you do have a monitor attached, you can power cycle your pi if "Stopping portmap daemon..." takes more than 5 seconds.
SSH into your Pi. If you have a monitor attached, your Pi will tell you it's IP address: "My network IP address is 192.168.1.116" (or an other IP of course). If you don't have a monitor attached, you need to check your routers logs to find out which IP your Pi has.
SSH into your pi:
ssh 192.168.1.116 -l pi
The default password is "raspberry"
Now we need to compile the bw_lcd.c program:
gcc -o bw_lcd bw_lcd.c
And now, it's time to play! Let's try to display our first text:
sudo ./bw_lcd -T 0,0 'Hello World!'
If you did everything right, you now have "Hello world!" displayed on your LCD. Have a look at the source of bw_lcd, or its [Raspberry_Pi_LCD_program manual] for other options.
The bw_lcd program can also be used to control all of our other SPI boards, using the -a, -r, and -v switches.
sudo bw_lcd -a <address of the board> -r <register> -v <value>
Let's try this with a SPI_LCD, by clearing the screen. The display is at address 82, the register is 0x10, and the value doesn't matter. Caveat: The address should be given in hex, but the register and value in decimal.
sudo bw_lcd -a 82 -r 16 -v 0
Now your display should be cleared.
If you managed to get everything in this howto working, you should be able to control the entire range of BitWizard SPI boards. Good luck, and please show us your projects!
If you have any problems, please let us know, and we will do our best to help you resolve them. You can email us, or post your problem below. We will respond as soon as we can, and post the problems and solutions on this page.