PC/CP320 Physical Computing

Raspberry Pi SPI Analog-to-Digital Converter


Digital transmission of analog data, (such as sound and video), requires analog input signals to be converted to digital signals for processing. Once received, the signals then have to be converted to back analog. These conversions are done by analog to digital (A/D) and digital to analog (D/A) converters, respectively. Although you could construct these converters from discrete components, integrated circuits specifically designed for these purposes are usually used.


  1. To use the SPI interface on the Raspberry Pi
  2. To become familiar with analog-to-digital conversion
  3. To write a test program to show the SPI A/D in operation
  4. To introduce the infrared distance sensor


The SPI (Serial Peripheral Interface bus is a synchronous interface allowing a single master device to communicate with multiple slave devices. The master device controls a clock signal for the slaves. The bus consists of two data lines (one for each direction), the clock signal, and select lines for each slave. Although it may not always be mentioned, it's also important that the GROUND lines for the master and slaves be connted. It is now very commonly used to connect sensors to microprocessors or microcontrollers, and so will remain relevant for the forseable future. It's also one of the simplest serial protocols, and so it's easy to study. The Raspberry Pi has an SPI bus built-in to handle up to two slave devices, so it can be used for SPI communications.





The Rapsberry Pi GPIO pins can easily be damaged with improper handling. The ADC in this lab can use a 3.3V supply, so it can be powered from the 3.3V GPIO pin.

To wire the ADC, there are 4 different sub-circuits to wire. They are: You will wire each of these sections in turn.
  1. SPI GPIO pins

    Note: The Spidev library has laready been installed.

    From the GPIO diagram, identify all of the SPI pins, namely:
    • MOSI
    • MISO
    • SCLK
    • CE0
    • CE1

  2. Connect VDD.
  3. Connect AGND and DGND.
  4. Conect the REF input of the ADC to the 3.3V pin of the Raspberry Pi. The input voltages will be converted relative to the voltage on the REF input.

  5. Most of the SPI pins should already be connected to the MAX522 DAC. Daisy-chain the pins from there to the ADC. There will be two other pins that will need to come directly from the Pi.
    Since communication with the Raspberry Pi is bidirectional, which Raspberry Pi SPI signal is needed which was not needed for the DAC?
    Which SPI slave select line was already used for the DAC? What changes will you need to make to your code to use the other one with the ADC?

  6. Conect one analog input to the variable supply. Make sure your grounds are connected as well.

  7. Write a program to read values from one input of the ADC. Note that every transfer to the ADC should be a multi-byte transfer.
    Download the test program from the website.
    Replace the question marks with appropriate values as needed.
    Note: there is a potential problem with the sample above if you are using a DAC and an ADC on the same SPI port but do NOT have them as different devices; i.e. using different chip select lines. This would prevent them from being connected at the same time. Configure the code if necessary so that this is not a problem.

  8. If you have done everything correctly, you should have a circuit which can take in an analog voltage and display the value for an input between 0 and 3.3 volts.

  9. Once you have it working, replace the variable supply with the infrared distance sensor.

  10. Get rid of the magic numbers in the code sample. (Hint: Look at the ADC datasheet.)
    Demonstrate your circuit to the lab supervisor.
    Keep this circuit for future labs.


To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

(Level AA)

Wilfrid Laurier University