A simple sensor using AtoD conversion

When the Gertboard was released by element14 last year I immediately pre-ordered one. However, when it arrived as a bag of components I just didn’t have the motivation to solder lots of surface mount resistors and capacitors onto the PCB so it sat in a draw for 7 months.  Recently I got the urge to fire up my soldering iron and put the board together; it took about 4 hours but it was very satisfying to do such a neat job.

Having built the board it was time to set about testing it.  The first thing to do is to download the Gertboard manuals and development software.  The following page provides links to all the important stuff.


I’ve got a V1 Gertboard (self-assembly kit) so I downloaded the appropriate test software in C and Python using the links. So after making LEDs on the board flash using the provided C programs I needed another simple project.  I wanted to test the Analogue to Digital converter on the Gertboard by building a light sensing circuit.  To get the Python version of the AtoD programs working you need py-spidev installed on your system as discussed in the README file included with the GB_Python software.

So here’s the circuit that I’ve set up to test the AtoD chip on the Gertboard:



I have just represented the Gertboard as the MCP3002 IC with the connections to the appropriate pins on the RPi.  The pot (or fixed resistor) and LDR act as a potential divider whose output is fed into the AtoD on the Gertboard.

The code to get the data from the circuit is:

# Python 2.7 version by Alex Eames of http://RasPi.TV
# functionally equivalent to the Gertboard atod test by
# Gert Jan van Loo & Myra VanInwegen
# Use at your own risk - I'm pretty sure the code is harmless,
# but check it yourself.
# This will not work unless you have installed py-spidev (see README.txt)
# spi must also be enabled on your system (also in README.txt)

# import Python 3 print function to prevent line breaks. All prints need ()
from __future__ import print_function
from time import sleep

# reload spi drivers to prevent spi failures
import subprocess
unload_spi = subprocess.Popen('sudo rmmod spi_bcm2708', shell=True, stdout=subprocess.PIPE)
start_spi = subprocess.Popen('sudo modprobe spi_bcm2708', shell=True, stdout=subprocess.PIPE)

import spidev
import sys
board_type = sys.argv[-1]

def get_adc(channel): # read SPI data from MCP3002 chip
 if ((channel > 1) or (channel < 0)): # Only 2 channels 0 and 1 else return -1
 return -1
 r = spi.xfer2([1,(2+channel)<<6,0]) # these two lines are explained in more detail at the bottom
 ret = ((r[1]&31) << 6) + (r[2] >> 2)
 return ret

iterations = 0 # initial value for iteration counter
channel = 0 # set channel to 3 initially so it will ask for user input (must be 0 or 1)

raw_input("When ready hit enter.\n")

spi = spidev.SpiDev()
spi.open(0,0) # The Gertboard ADC is on SPI channel 0 (CE0 - aka GPIO8)

while iterations < 6000:
 adc_value = (get_adc(channel))
 print ('\r',' Analogue Value = ',"{0:.2f}".format(adc_value*3.3/1023),'\r', sep='', end='')
 sleep(0.05) # need a delay so people using ssh don't get slow response
 iterations += 1 # limits length of program running to 30s [600 * 0.05]

# SPI communication based on a code snippet anonymously posted here...
# http://proxy.obd2tool.com/index.php?url=1n151A1t1r1r1D1z0I1CyX1s0UyGyFyMyb1v121t1lyd0Kyj1tyN1xya

# r = spi.xfer2([1,(2+channel)<<6,0])
# Send start bit, sgl/diff, odd/sign, MSBF
# channel = 0 sends 0000 0001 1000 0000 0000 0000
# channel = 1 sends 0000 0001 1100 0000 0000 0000
# sgl/diff = 1; odd/sign = channel; MSBF = 0

# ret = ((r[1]&31) << 6) + (r[2] >> 2)
# spi.xfer2 returns same number of 8-bit bytes as sent. In this case, three 8-bit bytes are returned
# We must then parse out the correct 10-bit byte from the 24 bits returned. The following line discards
# all bits but the 10 data bits from the center of the last 2 bytes: XXXX XXXX - XXXX DDDD - DDDD DDXX

This is a modification of the atod.py program included in GB_Python.  When run it will get the voltage level from the Gertboard and display it on screen.


One thought on “A simple sensor using AtoD conversion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s