Setup SPI on Raspberry pi (mcp3008, Adafruit)

Im building my own watering system and to that I will have some sensores..
They are connected to my pi over SFI and a mcp3008 from Adafruit.

 

The gear

http://www.adafruit.com/products/1989

http://www.adafruit.com/products/856

http://www.kjell.com/sortiment/el/elektronik/elektroniklab/kopplingsplatta-lodfri-p87886

http://www.elecfreaks.com/store/octopus-soil-moisture-sensor-brick-p-422.html

 

Setup the cables

Use this guide and se how the you should connect the mcp3008 and the sensore.

http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/

 

Get the Pi ready

 

1. First enable SFI on the board here

http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/

I uses the raspi-config and enabled the SFI

 

2. Test the SFI

download the tester

wget http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob_plain;f=Documentation/spi/spidev_test.c -O spidev_test.c

install the dep

apt-get install python-dev

Open the file and change this

static const char *device = "/dev/spidev0.1";

Build it

gcc spidev_test.c

Run it

./a.out

This is what you should see

spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)
00 02 20 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00 00 00 00 00 
00 00

Install python and deps

 

wget https://github.com/Gadgetoid/py-spidev/archive/master.zip
unzip master.zip
rm master.zip
cd py-spidev-master
sudo python setup.py install

get my test file to run to see output from alla channels on the mcp3008

https://github.com/mattiashem/r-pi/blob/master/sensor/test.py

 

The python file will loop over all the channels on you mcp3008 and print the output.

Leave a Reply

%d bloggers like this: