High-Precision AD HAT

From Waveshare Wiki
Jump to: navigation, search
High-Precision AD HAT
High-Precision-AD-HAT-1.jpg

Raspberry Pi AD Expansion
{{{name2}}}

{{{name3}}}

{{{name4}}}

{{{name5}}}

Primary Attribute
Category: Expansions, Raspberry Pi
{{{userDefinedInfo}}}: {{{userdefinedvalue}}}
Brand: Waveshare
Website
International: Website
Chinese: 官方中文站点
Onboard Interfaces
RPi AD
Related Products


Overview

High-Precision AD HAT For Raspberry Pi, ADS1263 10-Ch 32-Bit ADC

Specification

  • Resolution(Bits): 32
  • Input channels: 10
  • Sample rate(MAX): 38kSPS
  • PGA Magnification(MAX): 32
  • BUS: SPI
  • Structure: Delta-Sigma
  • Input type: differential, single-end
  • Reference voltage: internal, external
  • Input voltage range (MAX): 2.5V,5V
  • Input voltage range(MIN):-2.5V, 0V

Fatures

  • Adopts ADS1263 chip, low noise, low-temperature drift, 10-ch 32-bit high precision ADC (5-ch differential input), 38.4kSPS Max sampling rate
  • with embedded 24-bit auxiliary ADC, internal ADC test signal, IDAC, 2.5V internal reference voltage, 8x multiplexing GPIO, PGA (32 times Max)
  • Onboard AD header input, compatible with Waveshare sensor pinout, for connecting sorts of sensor modules
  • Onboard AD screw terminal input, allows connecting analog signal and analog power supply, general-purpose interface
  • Onboard control header, make it easy to control the module by other hosts in addition to Raspberry Pi
  • Three-wire RTD (resistor temperature detector) circuit, enabled by soldering 0R resistor

Pinout

PIN Raspberry Pi(BCM) Raspberry Pi(WiringPi) Description
DRDY P17 P0 ADS1263 data output ready, low active
RESET P18 P1 ADS1263 reset input
CS P22 P3 ADS1263 chip select, low active
DIN P10 P12 SPI data input
DOUT P9 P13 SPI data output
SCK P11 P14 SPI clock

Hardware configuration

Configure single-end

This board is set to the single-end mode by default. That is AVDD is connected to 5V, AVSS is connected to GND, and COM is connected to GND and set as negative end input.
You can connect the GND (or COM) and any IN pin to the target device for measuring.

Configure differential

If you want to measure a differential signal, you need to configure the board:

  • Connect the differential signals to IN0&IN1 pins (or other pins)
  • (Optional)If the voltage measured is small, you should use PGA. Check the demo codes and configure the REF to internal mode (±2.5V).
    The PGA is not supported by signal-end measuring by default. You should modify the REF value to 2.4 in the main.c file and adjust the PGA according to the actual situation. For examples:
#Modify the internal REF voltage:
#Change the define in the ADS1263_ConfigADC1() of ADS1263.c from
UBYTE REFMUX = 0x24;
#to 
UBYTE REFMUX = 0x00;
  • Use the differential mode for measuring
#Modify line 33 of main.c, change 
ADS1263_SetMode(0);
#to 
ADS1263_SetMode(1);
Note: The channel number for Differential measuring should be smaller than 5. You can change it according to the actual situation.

Configure RTD

If you need to use the RTD function, please configure it as below:

  • Solder four 0ohm resistors on the "RTD 0R" pads which is on the backside.

High AD HAT RTD1.png

  • Remove the jumpers of COM-GND

High AD HAT Jump1.png

  • Connect the positive end of the three-wire RTD module to IN7 and the less two to IN6 and IN4.

High AD HAT RTD2.png

  • Modify the codes

Communication protocol

SPI Protocol

The AD HAT uses an SPI interface, for more information about the protocol, you can refer to datasheet Page 11-12
SPI timing:
High AD HAT SPI 0.png
High AD HAT SPI.png
CS is chip select. The chip is active when the cs is low;
SCLK is the clock pin of SPI;
DIN is the data input pin, that is MOSI, master output, and slave input;
DOUT is the data output pin, that is MISO, master input, and slave output;
DRDY is the data ready output pin, when the data of ADC1 is ready to output, it is low.
SPI communication has data transfer timing, which is combined by CPHA and CPOL.

  • CPOL determines the level of the serial synchronous clock at the idle state. When CPOL = 0, the level is Low. However, CPOL has little effect on the transmission.
  • CPHA determines whether data is collected at the first clock edge or at the second clock edge of the serial synchronous clock; when CPHL = 0, data is collected at the first clock edge.

There are 4 SPI communication modes. SPI0 is commonly used, in which CPHL = 0, CPOL = 0.
As you can see from the figure above, data transmission starts at the first falling edge of SCLK, and 8 bits of data are transferred in one clock cycle. Here, SPI0 is in used, and data is transferred by bits, MSB first.

C examples and Python examples are provided for quick starting.

Configure interface

Enable SPI interface

  • Open terminal, use command to enter the configuration page
sudo raspi-config
Choose Interfacing Options -> SPI -> Yes  to enable SPI interface

RPI open spi.png
Reboot Raspberry Pi:

sudo reboot

Please make sure that SPI interface was not used by other devices

Install libraries

  • Install BCM2835 libraries
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.64.tar.gz
tar zxvf bcm2835-1.60.tar.gz 
cd bcm2835-1.64/
sudo ./configure
sudo make
sudo make check
sudo make install
#For more details, please refer to http://www.airspayce.com/mikem/bcm2835/
  • Install WiringPi libraries
sudo apt-get install wiringpi

#For Pi 4, you need to update it:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
#You will get 2.52 information if you install it correctly
  • python
sudo apt-get update
sudo apt-get install ttf-wqy-zenhei
sudo apt-get install python-pip 
sudo pip install RPi.GPIO
sudo pip install spidev

Download demo codes

Open a terminal and run the follow commands to download demo codes:

sudo apt-get install p7zip-full
wget http://www.waveshare.com/w/upload/6/64/High-Pricision_AD_HAT_Code.7z
7z x High-Pricision_AD_HAT_Code.7z -r -o./High-Pricision_AD_HAT_Code
cd High-Pricision_AD_HAT_Code/

Examples

C

  • Compile the codes
cd ~/High-Pricision_AD_HAT_Code/c
sudo make clean
sudo make
  • If you want to use other libraries, you need to modify the Makefile file.
sudo vim Makefile
Change USELIB_RPIwhich is on line 13-15
  • main file is generated after compiling, then you can run the file
sudo ./main
  • For this demo, you can also make some modifications to achieve different functions. About the details please refer to the following part "Function Introduction".

Python

  • Open a terminal and run the python codes
cd ~/High-Pricision_AD_HAT_Code/python

sudo python main.py
or
sudo python3 main.py
  • For this demo, you can also make some modifications to achieve different functions. About the details please refer to the following part "Function Introduction".

Function Introduction

Hardware section

The ADC of this module used is ADS1263 using the SPI communication, which needs to pay attention to those three parameters: mode (mode 1), rate (maximum 8M), data format (MSB).

  • It is mainly used to send commands and read/write registers: The commands are used to start/end ADC conversion, read/write registers, restart, etc; The registers are mainly used to write the configuration parameters, and the core of the demo is to read and write the registers.
  • The Read/Write register is a special command, which is different from an ordinary command(a single byte), it uses multi-byte(read/write register command + data to be written/received).

Software section

Because the functions of the C code and the Python code are the same, the following takes the code of ADS1263.c as an example, the corresponding function can also be found in ADS1263.py:

  • Reset function, DEV_RST_PIN is the IC reset pin, low level active. The reset operation is required before each use, and then the register configuration will be restored to the default value after reset and it needs to be reconfigured for the next use.
static void ADS1263_reset(void)
  • Data read and write functions, these are three functions: write command, write register, read register. Each SPI operation needs to pull down the DEV_CS_PIN (chip select, CS) pin, read and write registers will send 0x00 once because the command is two bytes.
static void ADS1263_WriteCmd(UBYTE Cmd)
static void ADS1263_WriteReg(UBYTE Reg, UBYTE data)
static UBYTE ADS1263_Read_data(UBYTE Reg)
  • Data check function——Sum Check function, the read data of this demo uses Sum Check, you can also configure to use CRC Check through the register

The main principle of the Sum Check is to add the low eight bits of the data, and finally add a fixed parameter. Use the low eight bits of this final sum XOR with the check byte which is read. If the data is read correctly, it will return 0.

static UBYTE ADS1263_Checksum(UDOUBLE val, UBYTE byt)
  • Data waiting function, DEV_DRDY_PIN is the DRDY pin, which is valid when ADC1 performs data conversion and sends a low level after the data is ready.

【Note】 ADC2 is not applicable to this function.

static void ADS1263_WaitDRDY(void)
  • Chip Read function, if the return value is 1, which means the IC is ADS1263, and return 0 for ADS1262.
UBYTE ADS1263_ReadChipID(void)
  • Mode setting function, set to 1 for the differential mode, the default value is 0 for single-ended mode
void ADS1263_SetMode(UBYTE Mode)
  • ADC config function, which can set the ADC rate, PGA gain multiple, reference voltage, etc.

The information of the register you can check in the ADS1263. If you need to configure other registers, it is better to read and verify after each writing according to the routine manner.

In order to ensure data stability and reduce crosstalk between multiple channels and two ADCs, the data conversion delay used by the demo is relatively large and the conversion rate is relatively slow. But you can modify it if you need to increase the speed.

【Note】 It is not recommended to turn on PGA when using single-ended measurement for ADC1. The PGA is off by default.

void ADS1263_ConfigADC1(ADS1263_GAIN gain, ADS1263_DRATE drate)
void ADS1263_ConfigADC2(ADS1263_ADC2_GAIN gain, ADS1263_ADC2_DRATE drate)
  • Chip initialization functions, including resetting, verifying, and configuring the ADC device.
UBYTE ADS1263_init(void)
  • Select channel function, call this function before switching ADC channel.
static void ADS1263_SetChannal(UBYTE Channal)
static void ADS1263_SetChannal_ADC2(UBYTE Channal)
void ADS1263_SetDiffChannal(UBYTE Channal)
void ADS1263_SetDiffChannal_ADC2(UBYTE Channal)
  • Read ADC data function, the return value of ADC1 is 32-bit data, for ADC2 is 24-bit data.
static UDOUBLE ADS1263_Read_ADC1_Data(void)
static UDOUBLE ADS1263_Read_ADC2_Data(void)
  • Obtain the data of the specified channel, there will be different effects depending on the mode.
UDOUBLE ADS1263_GetChannalValue(UBYTE Channel)
UDOUBLE ADS1263_GetChannalValue_ADC2(UBYTE Channel)
  • Obtain the data of all channels, encapsulate it for other files to call, and get the data cyclically of a specified number of channels.

To obtain 32-bit or 24-bit raw data, and then use this data by the maximum range and then multiply it by the reference voltage, you can get the voltage data. For example, the voltage of Raspberry Pi used with the demo is 5V (measured 5.08V), so the final voltage data is: ADC[i]/0x7fffffff*REF, the same applies to the negative voltage.

void ADS1263_GetAll(UDOUBLE *ADC_Value)
void ADS1263_GetAll_ADC2(UDOUBLE *ADC_Value)
  • RTD test function,including the RTD register configuration, the data conversion and the reading of ADC1.

To get temperature data

You need to calculate the resistance of the thermal resistance first. According to R=U/I, divide the two resistances (thermal resistance, reference resistance):

R(thermal resistance)/R(reference resistance) = U (thermal resistance) * U (reference resistance) * 2

So, RES = (ADC[0] / 0x7fffffff * 1) * 1 * 2 * 2000

Finally, the temperature can be obtained according to the thermal resistance formula, and the thermal resistance of the demo uses Pt100.

So the TEMP = (RES/100-1) / 0.00385

UDOUBLE ADS1263_RTD(ADS1263_DELAY delay, ADS1263_GAIN gain, ADS1263_DRATE drate)
  • DAC test function, used for the voltage setting, output pin setting (positive and negative polarity), open or close the DAC output.
void ADS1263_DAC(ADS1263_DAC_VOLT volt, UBYTE isPositive, UBYTE isOpen)

【Note】In the main file, you can modify the macro definition to quickly convert the required test(ADC/RTD). The REF is the reference voltage, you may need to modify it according to the actual situation for measuring accurately.



If you require technical support, please go to the Support page and open a tickets.