Data logger SD card

Hi Guys, I’ve been working in this project where I generate a function with a DSS board and read it from a analog input (A0).

I’ve done pretty much everything I wanted so far, but now I’m having some problem with data logger.

This is the function generation code:

/*
 * A simple single freq AD9850 Arduino test script
 * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com
 * Modified for testing the inexpensive AD9850 ebay DDS modules
 * Pictures and pinouts at nr8o.dhlpilotcentral.com
 * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
 * Use freely
 */
 
 #define W_CLK 8       // Pin 8 - connect to AD9850 module word load clock pin (CLK)
 #define FQ_UD 9       // Pin 9 - connect to freq update pin (FQ)
 #define DATA 10       // Pin 10 - connect to serial data load pin (DATA)
 #define RESET 11      // Pin 11 - connect to reset pin (RST).
 
 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
 
 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
  for (int i=0; i<8; i++, data>>=1) {
    digitalWrite(DATA, data & 0x01);
    pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
  }
}
 
 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
  int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850
  for (int b=0; b<4; b++, freq>>=8) {
    tfr_byte(freq & 0xFF);
  }
  tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  pulseHigh(FQ_UD);  // Done!  Should see output
}
 
void setup() {
 // configure arduino data pins for output
  pinMode(FQ_UD, OUTPUT);
  pinMode(W_CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(RESET, OUTPUT);
 
  pulseHigh(RESET);
  pulseHigh(W_CLK);
  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
  Serial.begin(9600);
}
 
void loop() {
  for (unsigned long fq = 30000; fq<= 50000; fq+=50) {
  sendFrequency(fq);  // freq
  //while(1);
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5/1023.0);
  Serial.print(voltage);
  Serial.print(",");
  Serial.print(fq); 
  Serial.println();
 delay(1);
}
//while (1) {}
}

And this is where I’m stuck:

/*
 * A simple single freq AD9850 Arduino test script
 * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com
 * Modified for testing the inexpensive AD9850 ebay DDS modules
 * Pictures and pinouts at nr8o.dhlpilotcentral.com
 * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
 * Use freely
 */
 
 #define W_CLK 8       // Pin 8 - connect to AD9850 module word load clock pin (CLK)
 #define FQ_UD 9       // Pin 9 - connect to freq update pin (FQ)
 #define DATA 10       // Pin 10 - connect to serial data load pin (DATA)
 #define RESET 11      // Pin 11 - connect to reset pin (RST).
 
 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
 #include <SD.h>
 const int chipSelect = 10;
 
 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
  for (int i=0; i<8; i++, data>>=1) {
    digitalWrite(DATA, data & 0x01);
    pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
  }
}
 
 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
  int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850
  for (int b=0; b<4; b++, freq>>=8) {
    tfr_byte(freq & 0xFF);
  }
  tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  pulseHigh(FQ_UD);  // Done!  Should see output
}
 
void setup() {
 // configure arduino data pins for output
  pinMode(FQ_UD, OUTPUT);
  pinMode(W_CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(RESET, OUTPUT);
 
  pulseHigh(RESET);
  pulseHigh(W_CLK);
  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
  Serial.begin(9600);
  Serial.print("Initializing SD Card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  
}
 
void loop() {
  for (unsigned long fq = 30000; fq<= 50000; fq+=50) {
  sendFrequency(fq);  // freq
  //while(1);
    int sensorValue = analogRead(A0);
   float dataVoltage = sensorValue * (5.0/1023.0);

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog5.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataVoltage);
    dataFile.print(",");
    dataFile.print(fq); 
    dataFile.println();
    dataFile.close();
    // print to the serial port too:
    Serial.print(dataVoltage);
    Serial.print(",");
    Serial.print(fq); 
    Serial.println();
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog2.txt");
  } 
 delay(1);
}
//while (1) {}
}

I tried to use the SD example provided by the arduino software, but it says it’s not recognizing my sd card.

I could make it work once, but after that I got the same problem every time I tried.

I’ve tried to format the sd card as well.

Thanks.

It’s interfering in the frequency/voltage when I run it (oscilloscope), but when I check the program there is nothing wrong with the loop.

I think the pins (11 and 10) used both for the function generator board and for the SD board is messed up. Is there any way of using alternative pins for one of them?

felipearcaro:
Is there any way of using alternative pins for one of them?

Sure, why not ?

Then again nobody here knows what Arduino you’re using, what DDS board and what SD logger so nobody can tell you how easy or hard it’ll be to rewire the system and change the pin assignments in the code. It could be as simple as using pins 6 and 7 instead of 10 and 11 for the DDS and changing these 2 lines of code from;

 #define DATA 10       // Pin 10 - connect to serial data load pin (DATA)
 #define RESET 11      // Pin 11 - connect to reset pin (RST).

to;

 #define DATA 6       // Pin 6 - connect to serial data load pin (DATA)
 #define RESET 7      // Pin 7 - connect to reset pin (RST).