Difference between revisions of "Beginners guide to SPI on Raspberry Pi"

From BitWizard WIKI
Jump to: navigation, search
(Created page with "= Introduction = In this guide, I will show you how to get SPI up and running, on a Raspberry Pi. We will start from scratch, with a blank SD card. = Things we will be using...")
 
(Troubleshooting)
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
= Introduction =
 
= Introduction =
 +
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.
  
In this guide, I will show you how to get SPI up and running, on a Raspberry Pi. We will start from scratch, with a blank SD card.
 
  
= Things we will be using =
+
 
 +
= Required hardware =  
  
 
* Raspberry Pi
 
* Raspberry Pi
* 2GB (or larger) SD card
+
* 4GB (or larger) SD card
 
* Power supply for your Raspberry Pi
 
* Power supply for your Raspberry Pi
* Ethernet cable
+
* Ethernet cable and/or USB keyboard (network connectivity is required)
 
* SD-card reader
 
* SD-card reader
* [http://www.bitwizard.nl/catalog/product_info.php?products_id=69 RPi Serial Breakout board]
 
* [http://www.bitwizard.nl/catalog/product_info.php?products_id=90 SPI cable]
 
* [http://www.bitwizard.nl/catalog/product_info.php?products_id=89 SPI LCD]
 
  
 +
= Bitwizard hardware =
 +
either:
 +
* a set of:
 +
** [http://www.bitwizard.nl/catalog/product_info.php?products_id=69 RPi Serial Breakout board]
 +
** [http://www.bitwizard.nl/catalog/product_info.php?products_id=90 SPI cable]
 +
** [http://www.bitwizard.nl/catalog/product_info.php?products_id=89 SPI LCD]
 +
* [http://www.bitwizard.nl/catalog/product_info.php?cPath=34&products_id=115 Raspberry Pi UI 16x2]
 +
* [http://www.bitwizard.nl/catalog/product_info.php?cPath=34&products_id=123 Raspberry Pi UI 20x4]
 
Not necessary, but useful:
 
Not necessary, but useful:
 
* Monitor with HDMI (or DVI and a HDMI-DVI adapter)
 
* Monitor with HDMI (or DVI and a HDMI-DVI adapter)
Line 20: Line 26:
 
= Software we will be using =
 
= Software we will be using =
  
* [http://downloads.raspberrypi.org/images/debian/6/debian6-19-04-2012/debian6-19-04-2012.zip Debian image for Raspberry Pi]
+
* [http://www.raspberrypi.org/downloads/ The latest Raspbian "wheezy"] [http://downloads.raspberrypi.org/raspbian_latest.torrent Direct link]
* [http://www.bitwizard.nl/software/rpi-spi-binary-kernel-20120608.tgz Modified kernel and modules]
+
* If you are using Windows: download putty.exe from [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html here]
* [http://www.bitwizard.nl/software/bw_lcd.c bw_lcd program]
 
  
Unpack the first two packages, I advise on making a seperate directory for this project, and put all the files there.
+
Unpack the Raspbian image. I advise on making a separate directory for this project, and put all the files there.
  
 
= Preparing the SD card =
 
= 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.<br>
+
== Under Linux ==
 +
 
 +
First, we will flash the SD card with the Debian image. Plug your SD card in your favorite reader, and into your PC. My SD card became /dev/sdc on my laptop, your mileage may vary.<br>
 
Copy the image to your SD-card:
 
Copy the image to your SD-card:
  sudo dd if=debian6-19-04-2012.img of=/dev/sdc
+
  sudo dd if=2014-06-20-wheezy-raspbian.img of=/dev/sdc
 
This may take some time, depending on the speed of your SD card, and card reader.
 
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.<br>
+
==  Under Windows ==
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.
+
 
 +
Download and extract [http://sourceforge.net/projects/win32diskimager/ Win32diskimager]. Connect the SD card to your computer and start Win32Diskimager. Select the drive and image and click 'Write'
 +
 
 +
 
 +
= Connecting the hardware =
 +
 
 +
We recommend you have/buy the rpi_serial board. It breaks out the SPI and I2C busses of the Raspberry Pi. This allows you to use a simple straight through 4- (I2C) or 6-pin (SPI) cable to connect the Raspberry Pi to the LCD board. The cables are available from our shop as well.  
  
Copy the relevant data:
+
Connecting the SPI version of the board to Raspberry Pi without any interface logic is not recommended: The LCD board needs to run at 5V because the HD44780 only works at 5V. The MISO line is then driven by the board at 5V level, which is said to shorten the life of the BCM2735 significantly.  
cp rpi-spi-binary-kernel-20120608/boot/* /media
 
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:
+
The I2C version of the board can be wired directly to the rpi with four wires.  
mv /media/95F5-0D7A/boot_enable_ssh.rc /media/95F5-0D7A/boot.rc
 
  
And your SD card is ready!<br>
+
If you're using SPI, please use the spi0 port for now for the purpose of this tutorial.
Unmount your CD card
+
 
sudo umount /media/
+
For RPi UI users: simply connect the board with the gpio pins.
And remove the SD card from your reader.
 
  
 
= First boot =
 
= First boot =
Line 53: Line 61:
 
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.
 
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.<br>
 
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.<br>
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.
+
Plug your freshly flashed SD card in your Pi, hook up power, and your Pi will boot.
 +
 
 +
 
 +
The first time the RPi boots, it will boot into the Raspberry Pi Config Tool. If you have connected a keyboard, you should enable SSH under advanced settings, expand the SD card and reboot. Upon reboot, you should see a message like
 +
"My network IP address is 192.168.1.116"  
 +
You could connect to the RPi using SSH (see below, recommended) or continue using the keyboard.
 +
 
 +
= Connecting to the Raspberry Pi Using SSH =
 +
To connect the Raspberry Pi, we first need to know its IP-number. This can be found in a number of ways:
 +
* Check your router for new devices in the network.
 +
* Use a network scanner like Zenmap or Fing (on your smartphone)
  
= Playing time =
+
If you figured out the IP, you can connect to the RPi:
 +
* On Windows: start putty
 +
* On Linux: ssh pi@IP where IP is the IP-address
 +
The default username/password is pi/raspberry
  
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.<br>
 
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:
+
= Installing the program =
  gcc -o bw_lcd bw_lcd.c
+
When connected, everything needs to be setup for interfacing with the board. We use our own bw_upgrade script:
 +
Get the script:
 +
wget http://www.bitwizard.nl/software/bw_upgrade && chmod a+x bw_upgrade
 +
Run the script (this may/will take some time):
 +
  sudo ./bw_upgrade
  
 
And now, it's time to play! Let's try to display our first text:
 
And now, it's time to play! Let's try to display our first text:
  sudo ./bw_lcd -T 0,0 'Hello World!'
+
  sudo bw_tool -a 94  -t 'Hello World!'
 +
In case of I2C (check display when unsure):
 +
sudo bw_tool -a 94 -I -D /dev/i2c-1 -t 'Hello World!'
 +
If you did everything right, you now have "Hello world!" displayed on your LCD. Have a look at the [[Bw tool| the wiki page of bw_tool]], or the  [https://github.com/rewolff/bw_rpi_tools/blob/master/bw_tool/bw_tool.c source of bw_tool].
  
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.
+
(For users of the "rev 1" model B, you need i2c-0 instead of i2c-1. )
  
 
= Next steps =
 
= Next steps =
  
The bw_lcd program van also be used to control all of our other SPI boards, using the -a, -r, and -v switches.
+
The bw_tool 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>
+
  sudo bw_tool -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.
 
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
+
  sudo bw_tool -a 82 -w 10:0
 
Now your display should be cleared. <br>
 
Now your display should be cleared. <br>
 +
See also the [[bw_tool| bw_tool  wiki page ]].
  
 
= Congratulations! =
 
= Congratulations! =
  
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 managed to get everything in this how-to working, you should be able to control the entire range of BitWizard SPI boards. Good luck, and please show us your projects!
  
 
= Troubleshooting =
 
= Troubleshooting =
  
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.
+
If you have any problems, please let us know, and we will do our best to help you resolve them. You can [http://www.bitwizard.nl/contact.php contact us], or post your problem in our [http://forum.bitwizard.nl/  forum.] We will respond as soon as we can.

Latest revision as of 17:20, 5 November 2015

Introduction

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.


Required hardware

  • Raspberry Pi
  • 4GB (or larger) SD card
  • Power supply for your Raspberry Pi
  • Ethernet cable and/or USB keyboard (network connectivity is required)
  • SD-card reader

Bitwizard hardware

either:

Not necessary, but useful:

  • Monitor with HDMI (or DVI and a HDMI-DVI adapter)
  • HDMI cable

Software we will be using

Unpack the Raspbian image. I advise on making a separate directory for this project, and put all the files there.

Preparing the SD card

Under Linux

First, we will flash the SD card with the Debian image. Plug your SD card in your favorite 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=2014-06-20-wheezy-raspbian.img of=/dev/sdc

This may take some time, depending on the speed of your SD card, and card reader.

Under Windows

Download and extract Win32diskimager. Connect the SD card to your computer and start Win32Diskimager. Select the drive and image and click 'Write'


Connecting the hardware

We recommend you have/buy the rpi_serial board. It breaks out the SPI and I2C busses of the Raspberry Pi. This allows you to use a simple straight through 4- (I2C) or 6-pin (SPI) cable to connect the Raspberry Pi to the LCD board. The cables are available from our shop as well.

Connecting the SPI version of the board to Raspberry Pi without any interface logic is not recommended: The LCD board needs to run at 5V because the HD44780 only works at 5V. The MISO line is then driven by the board at 5V level, which is said to shorten the life of the BCM2735 significantly.

The I2C version of the board can be wired directly to the rpi with four wires.

If you're using SPI, please use the spi0 port for now for the purpose of this tutorial.

For RPi UI users: simply connect the board with the gpio pins.

First boot

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 your Pi will boot.


The first time the RPi boots, it will boot into the Raspberry Pi Config Tool. If you have connected a keyboard, you should enable SSH under advanced settings, expand the SD card and reboot. Upon reboot, you should see a message like

"My network IP address is 192.168.1.116" 

You could connect to the RPi using SSH (see below, recommended) or continue using the keyboard.

Connecting to the Raspberry Pi Using SSH

To connect the Raspberry Pi, we first need to know its IP-number. This can be found in a number of ways:

  • Check your router for new devices in the network.
  • Use a network scanner like Zenmap or Fing (on your smartphone)

If you figured out the IP, you can connect to the RPi:

  • On Windows: start putty
  • On Linux: ssh pi@IP where IP is the IP-address

The default username/password is pi/raspberry


Installing the program

When connected, everything needs to be setup for interfacing with the board. We use our own bw_upgrade script: Get the script:

wget http://www.bitwizard.nl/software/bw_upgrade && chmod a+x bw_upgrade

Run the script (this may/will take some time):

sudo ./bw_upgrade

And now, it's time to play! Let's try to display our first text:

sudo bw_tool -a 94  -t 'Hello World!'

In case of I2C (check display when unsure):

sudo bw_tool -a 94 -I -D /dev/i2c-1 -t 'Hello World!'

If you did everything right, you now have "Hello world!" displayed on your LCD. Have a look at the the wiki page of bw_tool, or the source of bw_tool.

(For users of the "rev 1" model B, you need i2c-0 instead of i2c-1. )

Next steps

The bw_tool program can also be used to control all of our other SPI boards, using the -a, -r, and -v switches.

sudo bw_tool -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_tool -a 82 -w 10:0

Now your display should be cleared.
See also the bw_tool wiki page .

Congratulations!

If you managed to get everything in this how-to working, you should be able to control the entire range of BitWizard SPI boards. Good luck, and please show us your projects!

Troubleshooting

If you have any problems, please let us know, and we will do our best to help you resolve them. You can contact us, or post your problem in our forum. We will respond as soon as we can.