User Guide
Table Of Contents
174
Figure 10. Connecting a 23K640 SPI memory chip to the Pi
To use SPI in Python:
1. Click the Raspbian Menu button, point to
Programming, and then click Python 3.
2. In the Python Shell, on the Menu, click File and
then click New Window.
3. Type the following at the start of the script:
import spidev
import time
To create an instance of the SpiDev class and open a connection,
add the statements:
spi = spidev.SpiDev()
spi.open(0, 0)
The first parameter to open() is the device number. Only one SPI
master device is available on the GPIO header and so this is
always zero. The second parameter is the slave select pin that
you want to use. 0 tells the module to use pin 24, and 1 tells it to
use pin 26.
To write a byte to the SPI device, you can use the xfer() method.
For example:
spi.xfer([2,0,0,8])