Sensors into OSC with Python

By Haig on June 8, 2018 — 1 min read

How do we move from analogue to digital? There seems to be quite a number of ways including 2D & 3D scanning, photography, and sensors. For the purposes of intentionally limiting our options we’re going to create a physical interface with potentiometers and buttons. We’ll be using the LOP board with a Raspberry Pi.

Once we’ve soldered the potentiometers and header on the LOP board we have to install a few things and enable some of the system level software on the RPi.

Starting with enabling SPI

  1. Run sudo raspi-config .
  2. Use the down arrow to select 9 Advanced Options.
  3. Arrow down to A6 SPI .
  4. Select yes when it asks you to enable SPI,
  5. Also select yes when it asks about automatically loading the kernel module.
  6. Use the right arrow to select the <Finish> button.

Installing the Python Tools:

sudo apt-get update
sudo apt-get install python-setuptools
sudo apt-get install python-pip python-dev

Now we're ready to run the code, wait! we need to 


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

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

# Open osc

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

# 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)
 print("channel 1: ",ll0)

# send off the OSC message with sensor values

msg = OSC.OSCMessage()
Posted in: Code, RaspberryPi

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get in Touch

Your contact message.