WiFi101 and AD7684

Hello all.

I am trying to read data from AD7684 and send it to the internet using WiFi101.
I am able to do both, but I get a problem when I combine them together.

AD7684 works on modified SPI and has only MISO line to be connected. So I do not use any SPI mode and rather read it directly using port bit read. AD7684 shares the same SPI pins as of WiFi module.

Here is my code

When I enable the WiFi initialising part, the ADC values are -1, whereas when I disable WiFi part it gives actual values.
Please suggest.

const byte ADC_CS = 38;
const byte WIFI_CS = 53;
const byte SPICLOCK = 52;
const byte DATAIN = 50;


setup
{

  pinMode(ADC_CS, OUTPUT);
  digitalWrite(ADC_CS, HIGH);
  
  pinMode(DATAIN, INPUT); 
  pinMode(SPICLOCK, OUTPUT); 
  
  pinMode(13, OUTPUT);    //WiFi Enable Signal
  digitalWrite(13, HIGH); //Enable WiFi
  

  WiFi.setPins(WIFI_CS, 2, 10);
  
  //Initialize serial and wait for port to open:
  Serial.begin(115200);



  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) 
  {
    Serial.println("WiFi shield not present");
  }

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 1 seconds for connection:
    delay(1000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
  
  digitalWrite(WIFI_CS, HIGH);  //Desect WiFi
}



loop()
{
    Serial.println(ReadADCData());
    delay(1000);

}




int ReadADCData()
{
  pinMode(DATAIN, INPUT); 
  pinMode(SPICLOCK, OUTPUT); 
  digitalWrite(SPICLOCK,LOW);

  int adcvalue = 0;

  digitalWrite(ADC_CS,LOW);            //Select ADC

  for(int i = 0; i<6; i++)
  {
    digitalWrite(SPICLOCK,HIGH);       // 6 clock cycles for sampling
    digitalWrite(SPICLOCK,LOW);
  }

  //read bits from adc
  for (int i=15; i>=0; i--){
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    adcvalue|=digitalRead(DATAIN)<<i;  // Read data bit
    digitalWrite(SPICLOCK,LOW);
  }

  
  digitalWrite(ADC_CS, HIGH);          //turn off device
  return adcvalue;
  
}

Please suggest.

I suggest that you post code that actually compiles. setup() and loop() are functions. ALL functions must have a return type.

So I do not use any SPI mode and rather read it directly using port bit read.

This is not so clever. The pins are either in SPI mode or in GPIO mode. Why don't you use the hardware SPI to access the device?

Thanks for the replies.

Sometimes you think it is not a good idea to put all the code; as you do not wish to confuse the one who is going to help you. That is the reason I didn’t put the actual code but just the part where I was having a problem.

@pylon, I did not know which mode to be used for AD7684, and thus found it easier to use pin read mode as per datasheet timing diagram. I think that I cannot use SPICLK for AD7684 after using it for WiFi.

This code compiles:

#include <SPI.h>
#include <Wire.h>
#include <WiFi101.h>
 
 
int status = WL_IDLE_STATUS;
char ssid[] = "SSID"; //  your network SSID (name)
char pass[] = "PASSWORD";    // your network password (use for WPA, or use as key for WEP)


WiFiClient client;

const byte ADC_CS = 38;
const byte DATAIN = 50;
const byte SPICLOCK = 52;
const byte WIFI_CS = 53;



void setup() 
{
  pinMode(ADC_CS, OUTPUT);
  digitalWrite(ADC_CS, HIGH);
  
  pinMode(DATAIN, INPUT); 
  pinMode(SPICLOCK, OUTPUT); 
  
  pinMode(13, OUTPUT);    //WiFi Enable Signal
  digitalWrite(13, HIGH); //Enable WiFi
  
  //WiFi.setPins(CS, IRQ, RST);
  WiFi.setPins(WIFI_CS, 2, 10);
  
  //Initialize serial and wait for port to open:
  Serial.begin(115200);


  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) 
  {
    Serial.println("WiFi shield not present");
  }

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 1 seconds for connection:
    delay(1000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
  
  digitalWrite(WIFI_CS, HIGH);  //Desect WiFi

  Serial.println("\nStarting connection to server...");

}



void loop() 
{

  //Step size = 0.0000763 Volt = 5/(2^16)
  // Multiplied by 2 as VREF/2
  // The input range for +IN is between 0 V and VREF,
  // centered about VREF/2 and must be driven 180° out of phase with −IN.

  double SensorData_mV = ReadADCData() * 0.0763 * 2;
  //Serial.println(SensorData_mV);
  //Serial.println(" mV");
  
  //Sensor Resolution = 100mV/g
  double Vibration_g = SensorData_mV * 0.01; 
 // Serial.print("Vibration in g: ");
  Serial.println(Vibration_g);
  
  delay(1000);
  
}


int ReadADCData()
{
  pinMode(DATAIN, INPUT); 
  pinMode(SPICLOCK, OUTPUT); 
  digitalWrite(SPICLOCK,LOW);

  int adcvalue = 0;

  digitalWrite(ADC_CS,LOW);            //Select ADC

  for(int i = 0; i<6; i++)
  {
    digitalWrite(SPICLOCK,HIGH);       // 6 clock cycles for sampling
    digitalWrite(SPICLOCK,LOW);
  }

  //read bits from adc
  for (int i=15; i>=0; i--){
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    adcvalue|=digitalRead(DATAIN)<<i;  // Read data bit
    digitalWrite(SPICLOCK,LOW);
  }


  digitalWrite(ADC_CS, HIGH);          //turn off device
  return adcvalue;
  
}



void printWiFiStatus() 
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

[\code]

Hi,
Thank you all for your support.
I got this working.
I used SPI.end() after finishing WiFi usage.

And now I can also use it for reading AD7684 in bitbang mode.
This is my working code.

#include <SPI.h>
#include <Wire.h>
#include <WiFi101.h>


int status = WL_IDLE_STATUS;
char ssid[] = "SSID"; //  your network SSID (name)
char pass[] = "PASSWORD";    // your network password (use for WPA, or use as key for WEP)


WiFiClient client;

const byte ADC_CS = 38;
const byte DATAIN = 50;
const byte SPICLOCK = 52;
const byte WIFI_CS = 53;



void setup() 
{
 pinMode(ADC_CS, OUTPUT);
 digitalWrite(ADC_CS, HIGH);
 
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 
 pinMode(13, OUTPUT);    //WiFi Enable Signal
 digitalWrite(13, HIGH); //Enable WiFi
 
 //WiFi.setPins(CS, IRQ, RST);
 WiFi.setPins(WIFI_CS, 2, 10);
 
 //Initialize serial and wait for port to open:
 Serial.begin(115200);


 // check for the presence of the shield:
 if (WiFi.status() == WL_NO_SHIELD) 
 {
   Serial.println("WiFi shield not present");
 }

 // attempt to connect to WiFi network:
 while ( status != WL_CONNECTED) 
 {
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
   status = WiFi.begin(ssid, pass);

   // wait 1 seconds for connection:
   delay(1000);
 }
 Serial.println("Connected to wifi");
 printWiFiStatus();
 
 digitalWrite(WIFI_CS, HIGH);  //Desect WiFi

 Serial.println("\nStarting connection to server...");


 SPI.end();
 

}



void loop() 
{

 //Step size = 0.0000763 Volt = 5/(2^16)
 // Multiplied by 2 as VREF/2
 // The input range for +IN is between 0 V and VREF,
 // centered about VREF/2 and must be driven 180° out of phase with −IN.

 double SensorData_mV = ReadADCData() * 0.0763 * 2;
 //Serial.println(SensorData_mV);
 //Serial.println(" mV");
 
 //Sensor Resolution = 100mV/g
 double Vibration_g = SensorData_mV * 0.01; 
// Serial.print("Vibration in g: ");
 Serial.println(Vibration_g);
 
 delay(1000);
 
}


int ReadADCData()
{
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 digitalWrite(SPICLOCK,LOW);

 int adcvalue = 0;

 digitalWrite(ADC_CS,LOW);            //Select ADC

 for(int i = 0; i<6; i++)
 {
   digitalWrite(SPICLOCK,HIGH);       // 6 clock cycles for sampling
   digitalWrite(SPICLOCK,LOW);
 }

 //read bits from adc
 for (int i=15; i>=0; i--){
   //cycle clock
   digitalWrite(SPICLOCK,HIGH);
   adcvalue|=digitalRead(DATAIN)<<i;  // Read data bit
   digitalWrite(SPICLOCK,LOW);
 }


 digitalWrite(ADC_CS, HIGH);          //turn off device
 return adcvalue;
 
}



void printWiFiStatus() 
{
 // print the SSID of the network you're attached to:
 Serial.print("SSID: ");
 Serial.println(WiFi.SSID());

 // print your WiFi shield's IP address:
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);

 // print the received signal strength:
 long rssi = WiFi.RSSI();
 Serial.print("signal strength (RSSI):");
 Serial.print(rssi);
 Serial.println(" dBm");
}

[\code]