Jump to content
  • 0

Communicating with Pmod AD5 on Raspberry Pi 3


t3chxy

Question

Hi there,

I am in need of help communicating with my Pmod AD5 using the SPI on raspberry pi 3.
I am pretty new to SPI and have troubleshooted and searched the web for solutions over the past week but have yet to solve this problem. Here are some of the steps that I have done:
1) managed to read values using the Arduino Library linked on the product page and with an Arduino UNO, by connecting VCC of Pmod AD5 to 5V (I am not sure what I cannot read anything when connected to 3.3V)
2) understood what SPI is about and what the Arduino Library is doing (or what I think was logical) - read through the whole AD7193 datasheet to understand the registers etc..
3) extracted the outputs (Binary/hexa format) that the Arduino was sending to the Pmod AD5, understood them, and attempted to use them in raspberry pi with the spidev library but with no progress ..
4) tested by raspberry pi's SPI (they are enabled) using the same spidev library and successfully communicated and got values from a simpler ADC: mcp3008 using this
5) checked the wiring to ensure everything is intact and correctly connected

For my application, I intend to read ADC values up at speeds up to 1kHz over 2 channels and hence have chosen this Pmod AD5. For testing wise, I have connected a potential divider across A1 and A2 on the AD5 and am able to read the voltage changes when using Arduino but not in raspberry pi 3.

Here's my python code using the spidev on raspberry pi 3 (have connected to CE0):

####################### START OF CODE ################

import spidev
import time

spi = spidev.SpiDev()

spi.open(0,0)
spi.max_speed_hz = 50000
spi.mode=0b00

resp = spi.xfer2([0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
print('Resetting...', resp)
time.sleep(0.5)

##resp = spi.xfer2([0x08, 0x18, 0x00, 0x60])
##print('Enable DAT_STA Bit', resp)
##time.sleep(0.5)

resp = spi.xfer2([0x10, 0x00, 0x01, 0x10])
print('Set PGA Gain = 1, Buffer = 1', resp)
time.sleep(0.5)

##resp = spi.xfer2([0x08, 0x18, 0x00, 0x64])
##print('Setting filter rate select bits to 100', resp)
##time.sleep(0.5)
##
##resp = spi.xfer2([0x08, 0x98, 0x00, 0x64])
##print('Initiate internal calibration, starting w zero-scale', resp)
##time.sleep(0.5)
##
##resp = spi.xfer2([0x08, 0xB8, 0x00, 0x64])
##print('Full-scale calibration...', resp)
##time.sleep(0.5)

while True:

    #choose channel
    resp = spi.xfer2([0x10, 0x00, 0x01, 0x00])
    
    resp = spi.xfer2([0x58, 0x00, 0x00, 0x00])
    time.sleep(0.1)

    print('data:', resp)

####################### END OF CODE ################

Here's what most of my output looks like when I run the code (kind of the same code regardless of whether I have or not have anything connected in A1/A2 - it is unreactive even when I connect my potential divider and change the potential):

Resetting... [254, 170, 128, 193, 255]
Set PGA Gain = 1, Buffer = 1 [0, 0, 0, 0]
data: [0, 0, 0, 0]
data: [0, 0, 0, 0]
data: [0, 40, 128, 0]
data: [0, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 232, 191, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 232, 191, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 232, 191, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 232, 191, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 255, 255, 255]
data: [255, 232, 191, 255]
data: [255, 255, 255, 255] 
etc... etc...

This was the code I used to successfully read from the simpler MCP3008 ADC:

####MCP3008
##    adcnum = 0
####    r = spi.xfer2([1, (8+adcnum)<<4,0])
##    r = spi.xfer2([1, 0x80, 0])
##    print(r)
####    result = ((r[1]&3) << ? +r[2]
####    print(result)
##    time.sleep(0.1)

For more clarity, this is the Arduino code that I was using and got working with UNO:

/************************************************************************
*
* Test of the Pmod
*
*************************************************************************
* Description: Pmod_AD5
* The result of the A / D conversion of the AIN1 channel is displayed on the serial monitor.
*
*
* Material
* 1. Arduino Uno
* 2. Pmod AD5 (do not touch the jumper and
* dowload library https://github.com/annem/AD7193)
*
************************************************************************/

#include <SPI.h> // Call of libraries
#include <AD7193.h>

AD7193 AD7193; // Creation of the object AD7193

unsigned long valeur;
float tension;

void setup()
{
 Serial.begin(9600); // initialization of serial communication
 Init_AD7193();
}

void loop()
{
 valeur = AD7193.ReadADCChannel(0); // conversion A/N on input 1
 valeur = valeur >> 8; // Extraction of value
 tension = AD7193.DataToVoltage(valeur); // Recovery of tension
 Serial.println("");
 Serial.print("Valeur=");
 Serial.print(valeur);
 Serial.print('\t'); // tabulation
 Serial.print("Tension=");
 Serial.print(tension);
 Serial.println("V");
}

// Initialisation du module Pmod AD5
void Init_AD7193(void)
{
 AD7193.begin(); // initialization of Pmod AD5 module
 AD7193.AppendStatusValuetoData(); // configuration of Pmod AD5 module
 AD7193.SetPGAGain(1);
 AD7193.SetAveraging(100);
 AD7193.Calibrate();
 AD7193.ReadRegisterMap();
}

Would highly appreciate if anyone can help to give me tips on how to proceed or whether I am doing or understanding anything wrongly. Thank you very much.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Hello,

Starting from the Arduino sketch that you got it working, try to implement the same functionality step by step in your application. The fact that the Arduino sketch uses Arduino SPI library means that you should be able to use other generic SPI library. Please take a look on how AD7193 library is implemented (in AD7193.cpp, for example you can see the how SPI is configured and used). You can find AD7193 library  on the Internet (here for example).

On the other hand, there are more resources related to this pmod on Analog Devices wiki page for AD7193.

Good luck.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...