On-board SPI Devices

The Gertboard has 2 SPI device on-board and there are connected to the jumper block so that they can be directly connected to the Raspberry Pi’s SPI bus. The Pi has one SPI bus with two enable pins – this enables the Pi to talk to 2 separate SPI devices.

The devices on the Gertboard are the MCP3002 dual-channel analog to digital converter (A to D) and the MCP4802 dual-channel digital to analog converter (D to A). The D to A converter allows us to output an analog voltage from the Pi and the A to D allows the Pi to read an analog input. Each of these devices has 2 separate channels which are individually addressable.

The analog input can range from 0 to 3.3v and the analog output can range from 0 to 2.047 volts.

To calculate the value to write to make any output voltage on the D to A converter, the formula is:

Value = Desired Vout * 255 / 2.047

So, for example, to output 1.2 volts, we chose 1.2 * 255 / 2.047 = a value of 150.

To work out the input voltage, given the number returned from the A to D converter, the formula is:

Voltage = Read Value / 1023 * 3.3 volts

So if we read a value of 700, then the input voltage is 700 / 1023 * 3.3 = 2.26 volts.

To use these devices, we need to do 3 things:

  1. Remove any jumper wires from the 5 SPI pins – these are marked GP7, GP8, GP9, GP10 and GP11 on the J2 connector.
  2. Install jumper links over to the corresponding pins CSnB, CSnA, MISO, MOSI, SCLK.
  3. Load the kernel SPI module by using the gpio command: gpio load spi

At this point your Raspberry Pi and Gertboard are ready to use the SPI devices, but we’ll run a few quick tests at this point.

Connect a jumper wire from AD0 to DA0 on the long jumper row to the left of the Gertboard and run these commands:

gpio gbw 0 128
gpio gbr 0

That sets the output on D to A channel 0 to its mid-point (it’s an 8-bit output so the range is from 0 to 255), then reads A to D channel 0. You should expect to see a number near to 322.

Just to check: The output is an 8-bit output with a range of 0-255. 128 is the mid-point. The analog output is 0-2.047 volts, so the output voltage with a value of 128 is:

2.047 * 128 / 255 = 1.038v

If we apply that voltage to the input, (with its analog range of 0-3.3v and digital range of 0 to 1023), then the number we expect to read:

1.038 / 3.3 * 1023 = 322

On my Gertboard I’m reading exactly that on channel 0 and the same on channel 1 when I move the jumper wire. Don’t worry if you don’t read exactly this – a few percent difference either way is not going to be a problem.

Try writing other values and see if you can predict the input reading… Also try the other channels too, and if you have a multimeter set to volts, then you can use that to check the output voltage.


  • Need to make sure the Gertboard is setup to use the SPI – jumpers and links need setting correctly and the kernel module loaded.
  • 2 output channels and 2 input channels.
  • Output is from 0 to 2.047 volts, input is from 0 to 3.3 volts

Next – Experimenting with analog inputs