SPI usage with ADC MCP3008

68 views
Skip to first unread message

Roel Janssen

unread,
Jul 12, 2019, 7:56:01 AM7/12/19
to Pi4J
As a beginner, I'm having trouble understanding the MCP3008 usage example from the PI4J repository. Please note, this is my first time working with an ADC or the SPI interface. 

In the lines 65 and 66:

        // we must specify which chip select (CS) that that ADC chip is physically connected to.
       final AdcGpioProvider provider = new MCP3008GpioProvider(SpiChannel.CS0);

The chip select is specified. I'm not sure what this does. I know what chip select does, but how do i know what channel I am connected to? Also, I'm unclear about what GPIO pins I need to connect to from the MCP3008 chip. 

I tried following a tutorial for connecting the mcp3008 chip, but after running the PI4J sample program I'm getting a lot of fluctuation in every channel, while I'm only connected to channel 0. My current setup is exactly the same as in the tutorial, but with an alcohol gas sensor instead of a potentiometer and led.
Message has been deleted

GeertVc

unread,
Jul 14, 2019, 3:24:37 AM7/14/19
to Pi4J
Maybe this website might help you in understanding the SpiChannel.CS0 meaning: https://pinout.xyz/pinout/spi.  In the top-right corner select SPI and you will see the valid SPI pins highlighted for the Raspberry Pi, among others, the possible CS-pins.  It's up to you to choose whatever CS you're going to connect to your MCP3008 (CS0 or CS1).
The chip select is necessary to indicate which device has to pick up the SPI data on the SPI bus.

Note that the MCP3008 only supports SPI modes 0 and 3 (0,0 and 1,1).  See here: https://en.wikipedia.org/wiki/Serial_Peripheral_Interface#Mode_numbers for the meaning of the modes.

Connections from the RPi to the MCP3008:

- GPIO pin 19 of the RPi (MOSI) to pin 11 (Din) of the MCP3008
- GPIO pin 21 of the RPi (MISO) to pin 12 (Dout) of the MCP3008
- GPIO pin 23 of the RPi (SCLCK) to pin 13 (CLK) of the MCP3008
- GPIO pin 24 of the RPi (CE0) to pin 10 (CS/SHDN)of the MCP3008 if you use parameter SpiChannel.CS0
- GPIO pin 26 of the RPi (CE1) to pin 10 (CS/SHDN)of the MCP3008 if you use parameter SpiChannel.CS1

Note that you can also use a second SPI set on the RPi (see also https://pinout.xyz/pinout/spi) but therefore you have to modify the content of the file /boot/config.txt of your RPi.
Reply all
Reply to author
Forward
0 new messages