ADS1220 SPI conlficting with SD card

Hello guys!!

It's been a while sence I solved my arduino isues tacking advantage of this comunity.
I'm posting on the forum for the first time, mostly because I haven't found any similar posts.

I'm correnctly building my own DAQ using arduino uno (for now, later I'll use a pro micro), and ADC ADS1220, and logging the data to an SD card.

I also have an 0.92" Oled I2C display, wich shows the analog read in mV between AIN0 and AIN1 on the DAS1220, with a high enough precision.

The trubleshooting started when I added the SD card, using the SD.h library truth SPI comunication, bougth the SD card and the ADS1220 use SPI comunication, been connected to different CS_pins.

Everything works well until the SD card is initialized by the SD.begin() command, when the ADC readings go to negative lavues independently of the analog input.

The first aplication will be to measure the output in mV of an geophysical measuring device.

There will be two buttons for to different data measurments.

I'll post the arduino code bellow

//--------------------------------Constants-----------------------
const int butVert=2;
const int butHori=3;
int nSamples=100;     //Define the number of samples do colect in each line

//--------------------------------Variables------------------------

float Volts;

//------------------------------Screen-----------------------------

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

 
Adafruit_SSD1306 dsp(-1);

//------------------------------ADS1220----------------------------

#include "Protocentral_ADS1220.h"
#include <SPI.h>

#define ADS1220_CS_PIN    7
#define ADS1220_DRDY_PIN  6

Protocentral_ADS1220 pc_ads1220;
int32_t adc_data;

//-----------------------------SD---------------------------------
#include "SdFat.h"
SdFat sd;

const int chipSelect = 8;

void setup() {
  Serial.begin(115200);
//-----------------------------buttons-------------------------
pinMode(butVert,INPUT);
pinMode(butHori,INPUT);
//-----------------------------screen--------------------------
  dsp.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  dsp.clearDisplay();
  dsp.setTextColor(WHITE);
//------------------------------ADS1220-----------------
    pc_ads1220.begin(ADS1220_CS_PIN,ADS1220_DRDY_PIN);
    
//Uncoment the line to select the sample rate
    pc_ads1220.set_data_rate(DR_20SPS); //20 samples/second
   // pc_ads1220.set_data_rate(DR_45SPS); //45 samples/second
   // pc_ads1220.set_data_rate(DR_90SPS); //90 samples/second
   // pc_ads1220.set_data_rate(DR_175SPS); //175 samples/second
   // pc_ads1220.set_data_rate(DR_330SPS); //330 samples/second
   // pc_ads1220.set_data_rate(DR_600SPS); //600 samples/second
   // pc_ads1220.set_data_rate(DR_1000SPS); //1000 samples/second
     
      pc_ads1220.set_pga_gain(PGA_GAIN_1);
   
//Uncoment the line to be configure the differencial analog input
    pc_ads1220.select_mux_channels(MUX_AIN0_AIN1);  //Configure for differential measurement between A0 and A1
   // pc_ads1220.select_mux_channels(MUX_AIN0_AIN2);  //Configure for differential measurement between A0 and A2
   // pc_ads1220.select_mux_channels(MUX_AIN0_AIN3);  //Configure for differential measurement between A0 and A3
   // pc_ads1220.select_mux_channels(MUX_AIN1_AIN2);  //Configure for differential measurement between A1 and A2
   // pc_ads1220.select_mux_channels(MUX_AIN1_AIN3);  //Configure for differential measurement between A1 and A3
   // pc_ads1220.select_mux_channels(MUX_AIN3_AIN2);  //Configure for differential measurement between A3 and A2
   // pc_ads1220.select_mux_channels(MUX_AIN1_AIN0);  //Configure for differential measurement between A1 and A0
   // pc_ads1220.select_mux_channels(MUX_AIN3_AIN2);  //Configure for differential measurement between A3 and A1

    pc_ads1220.Start_Conv();  //Start continuous conversion mode

//----------------------SD-------------------------------------------
  
 /*Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  }
  Serial.println("card initialized.");
SD.end();*/
}

void loop() {
//----------------------ADS1220-------------------------------


//----------------------Screen---------------------------------
    dsp.setTextSize(2);
    dsp.setCursor(1,15);
    adc_data=pc_ads1220.Read_WaitForData();
    float Vout = (float)((adc_data*2.048*1000)/(16777216/2));
    dsp.print(Vout);
    dsp.setCursor(100,15);
    dsp.print("mV");
    dsp.setTextSize(1);
    dsp.setCursor(1,1);
    dsp.print("ponto:");
    dsp.setTextSize(1);
    dsp.setCursor(40,1);
    dsp.print("xxx"); //lugar para adicionar o numero do ponto
    dsp.display();
    dsp.clearDisplay();
//-----------------------Buttons----------------------------------
   if (digitalRead(butVert)==LOW){
      Serial.print("P1V\t");
      ads1220_Data();
    }
    if (digitalRead(butHori)==LOW){
      Serial.print("P1H\t");
      ads1220_Data();
    }
    


//---------------------------SD-------------------------------
/*File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print("xxx/t");
    dataFile.close();
    
  }*/
}
//--------------------------Functions-----------------------
float ads1220_Data(){ //reads de values converted by DAS1220 
  //will be chaged later to log to the SD_card
  dsp.clearDisplay();
  dsp.setTextSize(4);
  dsp.setCursor(15,3);
  dsp.print("Wait");
  dsp.display();
  dsp.clearDisplay();
  int i;
  for(i=0; i<=nSamples; i++){
    while(digitalRead(ADS1220_DRDY_PIN) == HIGH);
    adc_data=pc_ads1220.Read_WaitForData();
    Volts = (float)((adc_data*2.048*1000)/(16777216/2));
    Serial.print((float)Volts);
    Serial.print("\t");
  }
  Serial.println();
  return Volts;
}

I'm not a good programer so most of my code was parasitized from te examples that come with the libraries.

I think my problem issues with the SPI communication, but I don't know how to solve it, thus I'm asking for you help.

Many thanks

I'm missing the links to the used hardware (not the chip, the boards).

One reason for your problems is that the two SPI devices don't use the same SPI mode. Nowadays this shouldn't pose problems anymore as the SPI library supports transactions for years. Unfortunately the library I guess you're using (you failed to provide a link to it) for the ADS1220 is not modern enough to use that feature. So you either have to fix that library or set the SPI mode yourself before each call to that library.

Thank you Pylon for you reply,

Sorry for not providing the links:

Hardware:
Arduino Uno/Genuino

One of these cheap MicroSD readers Arduino SPI Micro SD TF Card Adapter Module

An ADS1220 SSOPI16 with home made breakout board.

I added a 47 ohm resistor the each SPI pin on the ADS1220, as shown on the datasheet: https://www.ti.com/lit/ds/symlink/ads1220.pdf

Libraries

ADS1220: GitHub - Protocentral/Protocentral_ADS1220: ADS1220 arduino library

and the simple SD library: GitHub - arduino-libraries/SD: SD Library for Arduino

I did noticed that in the Protocentral_ADS1220.cpp file, the SPI_Mode is set to Mode1, I have no ideia how to change it, if i try to edit the line SPI.setDataMode(SPI_MODE1); I will not get the right data from the ADS1220.
Is there a way to set the same SPI_Mode for the MicroSD, or will it interfere this time with the MicroSD card??

One of these cheap MicroSD readers

These cheap readers are often wired wrongly. CS is not connected to the OE of the driver chip. This results in a constant pull-down of the MISO pin which even might short circuit another device on the bus. Sensitive chips might get damaged.

I did noticed that in the Protocentral_ADS1220.cpp file, the SPI_Mode is set to Mode1, I have no ideia how to change it, if i try to edit the line SPI.setDataMode(SPI_MODE1); I will not get the right data from the ADS1220.
Is there a way to set the same SPI_Mode for the MicroSD, or will it interfere this time with the MicroSD card??

As I wrote you have to modify the library to use the transactional SPI library interface (a variable holding the SPISettings value and a call to startTransaction and endTransaction around every SPI usage in the library).

Thank you pylon for replying,

I did manage to get the right data from the ADS1220, and even initialize an create a file on the SD card, but could not open the file.

I managed it, by seting the firts BIT_ORDER and the SPI_MODE before call to each library, but it still doesn't work properly.

There another issue, that is, if I load a Arduino IDE example, let's say SD_CardInfo I'm unable to initialize the SD_card untill I manually disconnect the ADS1220 chip from the breadboard. I wonder if the problem lies in the library, the ADS1220 chip, or the Cheap SD reader.

Do you suggest another Micro_SD reader? and will it solve the issue?

If I'm using a 3.3V logic level I could connect the SD_card directly to the Arduino, like a pro mini 3.3V 8MHz. will this solve anything?

Just another question do you recomend any library on wich a can learn about the SPI_transaction protocol, and implement it to the ADS1220 library?

As I said before, I'm a noob in terms of programing, so I learn by studing others peaple code.

Thank you

There another issue, that is, if I load a Arduino IDE example, let's say SD_CardInfo I'm unable to initialize the SD_card untill I manually disconnect the ADS1220 chip from the breadboard. I wonder if the problem lies in the library, the ADS1220 chip, or the Cheap SD reader.

Most probably the reader. You find dozens of threads in this forum about these cheap readers have an SPI interface not conforming to the standard. Many of them can be fixed if you're good in soldering and cutting fine traces but I would recommend investing a few bucks to buy a reader from one of the know board makers (Adafruit, Sparkfun, etc.).

Do you suggest another Micro_SD reader? and will it solve the issue?

Take one from Adafruit or Sparkfun (not the breakout but the shield) and yes, it will probably solve your issues although I cannot guarantee for it.

If I'm using a 3.3V logic level I could connect the SD_card directly to the Arduino, like a pro mini 3.3V 8MHz. will this solve anything?

No, not if you have a broken reader. The problem is that the MISO signal will not tri-state if CS is HIGH.

Just another question do you recomend any library on wich a can learn about the SPI_transaction protocol, and implement it to the ADS1220 library?

The SD library does use it as well as the Ethernet library.

As I said before, I'm a noob in terms of programing, so I learn by studing others peaple code.

Which is one of the best ways to learn it, given you get code from someone who knew what he did. :slight_smile: