Page 1 of 1

Waveshare High-Res ADC board (SPI) code

Posted: Mon Feb 25, 2019 3:12 am
by jbeale
I noticed a "Waveshare High Precision AD/DA Board" board with an 8 channel 24-bit ADC, the TI ADS1256 on Amazon for a surprisingly reasonable price. The board also has some sort of DAC, which I didn't try. I see there have been various posts about it here. Waveshare has a wiki with a C code example, but it always cycles through all channels and does not allow using the part anywhere near its full speed. There's also a usable Python library "PiPyADC" .

I modified the sample C code to read out only one channel at a time, which enables you to run the board near the full 30 kHz speed, although you don't want to do that as the noise is extremely high. So much so I think there's probably some readout timing problem, although it wasn't obvious on the scope. Anyway if you only want 2 kHz or slower it is quite usable and pretty hard to beat for the price, given it's a working 24-bit ADC that plugs right into the Pi.

My code is ... 256_read.c

It's simple and crude, it takes 100% of one CPU core spinning in a tight loop waiting for the ADC DataReady/ signal to drop while waiting to read out each sample. I suppose an interrupt routine or DMA engine would make sense here, but that would take some time to figure out.

Here are some RPi2B + Waveshare ADC results for a low-noise source (NiCd AA battery) on channel AD2 for 10 seconds, single-ended mode, gain=1 at some different sampling rates:

Code: Select all

 *  " 30 kHz"  actual 25393 Hz, pk-pk noise = 10223504 counts, = 24090
 *  " 15 kHz"  actual 14551 Hz, pk-pk noise =     3755 counts, = 101.4
 *  "7.5 kHz"  actual  7403 Hz, pk-pk noise =     3575 counts, = 66.1
 *  "  2 kHz"  actual  1998 Hz, pk-pk noise =      508 counts, = 26.4
 *  "  1 kHz"  actual  1000 Hz, pk-pk noise =      217 counts, = 21.6
 *  "500  Hz"  actual   500 Hz, pk-pk noise =      120 counts, = 17.7
 *  "100  Hz"  actual   100 Hz, pk-pk noise =       67 counts, = 6.68
 *  " 60  Hz"  actual  60.0 Hz, pk-pk noise =       40 counts, = 5.85
 *  " 50  Hz"  actual  50.0 Hz, pk-pk noise =       33 counts, = 4.89
 *  " 10  Hz"  actual  10.0 Hz, pk-pk noise =       14 counts, = 2.94
 *  "2.5  Hz"  actual 2.500 Hz, pk-pk noise =        5 counts, = 1.19
I measured my board's calibration at 1696 counts per millivolt, so the slowest rate has less than 1 microvolt RMS noise, not bad for $34.
The board uses the LM285D-2.5 as an onboard 2.5V reference. Below are readings from my ~1.3 volt AA battery over several minutes. This is at the slowest 2.5 Hz rate but the higher value here is from analog voltage drift, likely influenced by slight temperature changes, during each 40-second measurement.

Code: Select all

   date      time  , samples,  raw,    stdev, pk-pk, volts
2019-02-25 12:06:12, 100, 2242273.470, 4.448, 19, 1.3221506
2019-02-25 12:06:52, 100, 2242276.700, 4.227, 17, 1.3221525
2019-02-25 12:07:32, 100, 2242290.560, 2.872, 14, 1.3221606
2019-02-25 12:08:12, 100, 2242294.220, 5.782, 19, 1.3221628
2019-02-25 12:08:52, 100, 2242299.230, 3.609, 16, 1.3221657
2019-02-25 12:09:32, 100, 2242308.310, 5.817, 22, 1.3221711
2019-02-25 12:10:12, 100, 2242312.930, 4.989, 19, 1.3221738
2019-02-25 12:10:52, 100, 2242317.200, 3.918, 16, 1.3221763
2019-02-25 12:11:32, 100, 2242325.950, 5.677, 21, 1.3221815
2019-02-25 12:12:12, 100, 2242325.700, 3.506, 14, 1.3221814
2019-02-25 12:12:52, 100, 2242328.740, 3.308, 14, 1.3221831
2019-02-25 12:13:32, 100, 2242328.240, 3.409, 16, 1.3221829

Re: Waveshare High-Res ADC board (SPI) code

Posted: Fri Mar 01, 2019 7:34 pm
by jbeale
Update: I am not as enthusiastic about this board as I was previously. It does "mostly" work, but running the PiPyADC python code overnight I see there was some SPI glitch (I presume) at some point that put the board in a strange mode. I wonder what happens if there is a Linux context-switch away from the code during a SPI write transaction, I don't know if that's possibly a problem.

At any rate I'm using another (cheaper) ADS1256 board from ebay connected to an Arduino type microcontroller (Teensy 3.2) and that works as expected without high noise even at 30 kHz output. It looks like the Fc/4 or 1.92 MHz serial clock allowed by the data sheet is actually too fast, and even a 1.50 MHz SCLK sometimes causes glitches like this:
I was able to prevent these kinds of glitches using a 1.20 MHz SCLK.

Re: Waveshare High-Res ADC board (SPI) code

Posted: Sun Mar 03, 2019 5:24 pm
by jbeale
By the way, if you're testing ADC boards, here are two really useful programs:

Kst2 (KST-Plot) is great for quickly plotting large CSV files (no problem with millions of points), and can even do filtering and plot frequency spectrum (FFT function). It reads a CSV file but can be "semi-real-time" as it will automatically replot as the CSV changes, if your logging program is writing to it.

Serialplot by Hasan Yavuz ÖZDERYA will open a serial port and plot live data in real time. It accepts binary as well as CSV type ASCII data. it will also log the data to a file for later work. Not as complex and powerful as KST but very responsive and has a nice clean looking UI.

Re: Waveshare High-Res ADC board (SPI) code

Posted: Sun Jun 09, 2019 5:51 pm
by goodarzi128
Hi and thanks for try Waveshare High-Res ADC board.
I also downloaded the Python code from the Waveshare site and tested it on Pi 3 B+. But the sampling rate was not exceed 2000 sps for all 8 channel. Do you have a modified Python code that can acquiring data in 25000 sps for all 8 channel ? Thank you very much for your help.

Re: Waveshare High-Res ADC board (SPI) code

Posted: Sat Jun 29, 2019 11:08 pm
by jbeale
goodarzi128 wrote:
Sun Jun 09, 2019 5:51 pm
I also downloaded the Python code from the Waveshare site and tested it on Pi 3 B+. But the sampling rate was not exceed 2000 sps for all 8 channel. Do you have a modified Python code that can acquiring data in 25000 sps for all 8 channel ? Thank you very much for your help.
Sorry to say, as far as I know the board is simply incapable of full performance on the Raspberry Pi, because the timing of the data transfer is not consistent. The results in my first post were the best I could do on the Pi, and the highest two speeds are not really working correctly, I believe the larger-than-expected standard deviation of the error is not just noise, it is due to occasional data transfer failures where an entire byte of the data word is simply missing. As I note in my second post, it works better using a microcontroller, but even so the chip's data sheet may not be correct about the true speed capability of the serial bus. I think it is OK if you only need slow ADC results like 1 kHz sample rate on a single channel, or 100 Hz sample rate on all 8 channels.