Ashwin K WhitchurchVenkatesh Bhat
Published © MIT

Resistive-sensor Measurement using Current Source

We show you how to excite a force-sensitive resistor using a current source, instead of the traditional Wheatstone bridge-based approach.

IntermediateProtip1 hour4,877
Resistive-sensor Measurement using Current Source

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
ADS1220 24-bit, 4-channel Low Noise ADC Breakout Board
ProtoCentral Electronics ADS1220 24-bit, 4-channel Low Noise ADC Breakout Board
×1
ProtoCentral Electronics PROTOPI-T V2 GPIO BREAKOUT WITH USB SERIAL ACCESS FOR RASPBERRY PI 3 / 2
×1

Story

Read more

Code

Python Script for reading the ADS1220 over SPI

Python
#!/usr/bin/env python
print "test"

import spidev
import time
import sys
import RPi.GPIO as GPIO
from array import *

#ads1220 registers
RREG  =   0x20
WREG  =   0x40
START =   0x08
STOP  =   0x0A
RDATAC=   0x10 
SDATAC	=	0x11	
RDATA	=	0x12

GPIO.setmode(GPIO.BOARD)

# Pin declararion
DRDY_PIN  = 13
START_PIN = 15
CS_PIN    = 18
RESET_PIN = 16

GPIO.setwarnings(False);

#Set pin direction
GPIO.setup(CS_PIN,GPIO.OUT)
GPIO.setup(DRDY_PIN,GPIO.IN)

spi = spidev.SpiDev()				# create spi object
spi.open(0,1)					# open spi port 0, device (CS)1
spi.max_speed_hz = (500000)
spi.mode = (1)

print "test"
count = 0

def Reg_Write(address,data):    # write Fibonacci series up to 
	print ("Writing %s to Register %s" %( hex(data),hex(address)))
	print(data);
    opcode1 = (address<<2) | 0x40

	GPIO.output(CS_PIN, False)
	time.sleep(0.002)               # sleep for 0.1 seconds
	GPIO.output(CS_PIN, True)
	time.sleep(0.002)               # sleep for 0.1 seconds

	GPIO.output(CS_PIN, False)
	time.sleep(0.002)               # sleep for 0.1 seconds
	resp = spi.xfer2([opcode1])        # transfer one byte
	resp = spi.xfer2([data])        # transfer one byte
	time.sleep(0.002)               # sleep for 0.1 seconds
	GPIO.output(CS_PIN, True)
	return;

def Reg_read(address):    # write Fibonacci series up to
	print ("Reading from Register %s" %(hex(address)))
        opcode1 = address | 0x20

        GPIO.output(CS_PIN, False)
        time.sleep(0.002)               # sleep for 0.1 seconds
        GPIO.output(CS_PIN, True)
        time.sleep(0.002)               # sleep for 0.1 seconds

        GPIO.output(CS_PIN, False)
        time.sleep(0.002)               # sleep for 0.1 seconds
        resp = spi.xfer2([opcode1])        # transfer one byte
        resp = spi.xfer2([0xff])        # transfer one byt
        time.sleep(0.002)               # sleep for 0.1 seconds
        GPIO.output(CS_PIN, True)

	#print("Data read is %s" %(hex(resp)))
	print(resp);
	return;

def Spi_command(command):    # write Fibonacci series up to
        GPIO.output(CS_PIN, False)
        time.sleep(0.002)               # sleep for 0.1 seconds
        GPIO.output(CS_PIN, True)
        time.sleep(0.002)               # sleep for 0.1 seconds

        GPIO.output(CS_PIN, False)
        time.sleep(0.002)               # sleep for 0.1 seconds
        resp = spi.xfer2([command])        # transfer one byte
        time.sleep(0.002)               # sleep for 0.1 seconds
        GPIO.output(CS_PIN, True)

        return;


def initialisation1():  
	print ("INITIALISATION");
	#GPIO.output(CS_PIN, False)


	Spi_command(START);				
	time.sleep(0.1);
	Spi_command(STOP);				
	time.sleep(0.1);

	Spi_command(SDATAC);				
	time.sleep(0.3);

	Reg_Write(0x00, 0x01); 		
	time.sleep(0.01); 
	Reg_Write(0x01, 0x04);	
	time.sleep(0.01); 
	Reg_Write(0x02, 0x10);		
	time.sleep(0.01); 
	Reg_Write(0x03, 0x00);	
	time.sleep(0.01); 
	
	Reg_read(0x00);
	time.sleep(0.1);
	Reg_read(0x04);
	time.sleep(0.1);
	Reg_read(0x08);
	time.sleep(0.1);
	Reg_read(0x0c);
	


        
	Spi_command(RDATAC);				
	time.sleep(0.3); 

	Spi_command(START);
	time.sleep(0.1);
	 

	GPIO.output(CS_PIN, True)
	return;


def toHex(dec):
	x = (dec % 16)
	digits = "0123456789ABCDEF"
	rest = dec / 16
	if (rest == 0):
		return digits[x]
	return toHex(rest) + digits[x]

def initialisation2():

	time.sleep(0.2)
	print "STARTED"

	#GPIO.output(CS_PIN, False)
	resp = spi.xfer2([0x11])        # transfer one byte
	time.sleep(0.3)	
	return;

def Read_Data():
	buff = [0,0,0,0,0,0,0,0,0];
	GPIO.output(CS_PIN, False)
	buff = spi.xfer2([0xff,0xff,0xff])
	GPIO.output(CS_PIN, True)
	
	print("0x %x %x %x\t"%(buff[0],buff[1],buff[2]))
	value = buff[0]<<16 | buff[1]<<8 | buff[2]
	print(value)
	volt = (float(value) * .000244140625)
        print  volt,"mV"
	return buff;
	#return r

def Read_adcvoltage():
	result = Read_Data()
	return result
	
	
voltage = 0

def ADCRead():
	#global voltage
	while(GPIO.input(DRDY_PIN) == True):
		time.sleep(0.001)		

	if (GPIO.input(DRDY_PIN) == False):
		#if 1:
		data = Read_adcvoltage();
		#val = ((data[5]&0xff) << 16) | ((data[6]&0xff) << 8) | (data[7]&0xff)
		val = ((data[6]&0xff) << 16) + ((data[7]&0xff) << 8) + (data[8]&0xff)
		#val = (data&0xff0000) + (data&0xff00) + (data&0xff)
		val = int(val)

		if( (val&(1<<(24-1))) != 0 ):
			value = val - 0x1000000			
		else:
			value = val
			
		voltage = (float(value)*100/8388607)
	return voltage	


while True:

	Read_Data()
	time.sleep(0.5)

How to install and use this code

Plain text
Install Python Libraries

To install Python packages: 
Open the terminal type:
sudo apt-get update

sudo apt-get install python-dev
git clone https://github.com/Gadgetoid/py-spidev
cd py-spidev
sudo make install 

To install raspberry spi module
mkdir python-spi	
cd python-spi
wget https://raw.github.com/doceme/py-spidev/master/setup.py
wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
sudo python setup.py install

To install raspberry gpio module
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

Install time package
pip install time

Note:  Make Sure is serial interface is SPI is enabled.
Check Menu-> Preferences-> Raspberry Pi configuration -> SPI-> Enable
Connections

Raspberry ------> ads1220 breakout
Pin1 (3.3V) -----> VDD
Pin9 (GND) -----> GND
Pin13 -------> DRDY  
Pin 15 -------> START
Pin16 -------> RESET
Pin 18 ------->CS
Pin19 -------> MOSI
Pin 21 -------> MISO
Pin23 -------> SCK  
Running script
Copy the example code “example.py” to “python-spi “ created.

Run the script using “sudo python example.py”

Code explanation

ADCRead()
Reads the ADC value and convert it to corresponding voltage to be plotted

initialisation1()
Intialises the ads1292 to be in a Continuous conversion mode, 125SPS .

Reg_Write(address,data)
Writes one register to an address

Reg_read(address)
Reads  value from an address.

Spi_command(command)
Sends a SPI command to the ads1292

generator()
Copies the ADC valus to an array, and send it to the plotter to be plotted

Credits

Ashwin K Whitchurch

Ashwin K Whitchurch

14 projects • 106 followers
Venkatesh Bhat

Venkatesh Bhat

10 projects • 35 followers

Comments