Thursday 15 October 2015

getting sensors in using the MCP3008

Attaching a mcp3008 chip to your pi will give you 8 channels of analogue ins, the pi's pins can be used straight away to register button presses


you can buy a mcp3008 chip here
to get it running, first make sure you have the relevant software and librarys installed, the following libraries are relevant for getting all sorts of inputs in from the pi



in a terminal window

git clone https://github.com/ptone/pyosc.git  
cd pyosc
sudo ./setup.py install
cd
sudo apt-get install python2.7-dev
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
cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
cd
sudo apt-get install python-setuptools python-dev
git clone https://github.com/Gadgetoid/WiringPi2-Python.git
cd WiringPi2-Python/
sudo python setup.py install
cd
git clone git://github.com/guyc/py-gaugette.git
cd py-gaugette
sudo python setup.py install
cd


Next make sure you have your SPI interface enabled on the pi

in terminal

sudo raspi-config

8. Advanced Options


A6. SPI


turn it on



turn on SPI kernal module on default




then reboot the pi



wire-up yer mpc3008







these pictures came from here

this page will show you the different pins on different models of raspberry pi



raspberry pi 1 pins


raspberry pi 2 pins




communicating inputs

the mpc3008 is read by a simple python script, this information is then passed to pd-extend via OSC

the pd patch starts the python patch automaticaly using the shell object
(n.b if auto-starting  the pd patch though updating the rc. file (see starting headlessly) the pd patch needs the full address of the python script


first start up the pd patch, this will open a osc channel, then will open the python script which will start sending the data.

here's some screen shots of how the pd patch works. download the patch here





inside the subpatcher pd sortmes



here is the python script, download it here




#!/usr/bin/python

import spidev
import time
import os
import OSC
import RPi.GPIO as GPIO

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Open osc

send_address = "127.0.0.1" , 9000
c = OSC.OSCClient()
c.connect(send_address)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data


# Define sensor channels
l0 = 0
l1 = 1
l2 = 2
l3 = 3
l4 = 4
l5 = 5
l6 = 6
l7 = 7


# Define delay between readings
delay = 0.1

while True:

  # Read the light sensor data
  ll0 = ReadChannel(l0)
  ll1 = ReadChannel(l1)
  ll2 = ReadChannel(l2)
  ll3 = ReadChannel(l3)
  ll4 = ReadChannel(l4)
  ll5 = ReadChannel(l5)
  ll6 = ReadChannel(l6)
  ll7 = ReadChannel(l7)


  msg = OSC.OSCMessage()
  msg.setAddress("print")  
  msg.append(ll0)
  msg.append(ll1)
  msg.append(ll2)
  msg.append(ll3)
  msg.append(ll4)
  msg.append(ll5)
  msg.append(ll6)
  msg.append(ll7)
  









i





No comments:

Post a Comment