Linker kit Base Shield for Raspberry Pi B+/2B/3 with ADC Interface V3.1

From LinkSprite Playgound
Jump to: navigation, search

Introduction

Based on 3.0 version, we add some interface and use MCP3008 in stead of MCP3004, 4-channel ADC update to 8-channel ADC

RPI BASE SHIELD V3.1-9.JPG

RPI BASE SHIELD V3.1-8.JPG


Interface

1x UART connector (UART)

1x I2C connector (I2C)

12 x Digital connector (D1~D12)

4x analog connector (ADC1~ADC4)

Usage

Enable SPI:

MCP3008 use SPI interface for communication, so we need to start SPI kernel module. In the previous OS we need enable SPI in /etc/modprobe.d/raspi-blacklist.conf. But now we can enable SPI at raspi-config, also include I2C.

$sudo raspi-config

Choose:8. Advanced Options

RPI BASE SHIELD V3.1 -1.jpg

Choose:A6 SPI, Enable SPI

RPI BASE SHIELD V3.1-2.jpg

RPI BASE SHIELD V3.1-3.jpg

RPI BASE SHIELD V3.1-4.jpg

Reboot Raspberry Pi, then input

$lsmdo

We can find SPI is enabled

RPI BASE SHIELD V3.1-5.png


Next, we install libraries:

$Sudo apt-get update

$sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git

$sudo pip install spidev

$sudo pip install wiringpi


Test Code(print 8 ADC channels value):

import spidev import time

Vref = 3.3 #ADC_Vref = Voltage Switch 3.3V / 5V

  1. A0 = 0, A1 = 1, A2 = 2, A3 =3

adc0 = 0 adc1 = 1 adc2 = 2 adc3 = 3 adc4 = 4 adc5 = 5 adc6 = 6 adc7 = 7

spi = spidev.SpiDev() spi.open(0,0)

def readadc(adcnum):

  1. read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
   if adcnum > 7 or adcnum < 0:
       return -1
   r = spi.xfer2([1,(8+adcnum)<<4,0])
   adcout = ((r[1] &3) <<8)+r[2]
   return adcout

while True: value = readadc(adc0) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V" % (adc0,volts) )

value = readadc(adc1) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V"% (adc1,volts) )

value = readadc(adc2) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V" % (adc2,volts) )

value = readadc(adc3) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V"% (adc3,volts) )

value = readadc(adc4) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V" % (adc4,volts) )

value = readadc(adc5) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V"% (adc5,volts) )

value = readadc(adc6) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V" % (adc6,volts) )

value = readadc(adc7) volts = (value * Vref) / 1024 print("ADC%ld = %5.3f V"% (adc7,volts) )

print("-------------------------") time.sleep(0.5)

RPI BASE SHIELD V3.1-7.jpg