Thanks for the answers.
I’m using a Arduino UNO, and with some adjusts I could get pretty good peak results.
I’m sweeping the frequency between 20KHz and 40KHz.
The wiring is simple, I’m using pins 5,6,7 and 9 for the DDS Function Generator; 10, 11, 12 and 13 for the SD reader and A0 to read the voltage from the piezo.
Valen, thanks for the clear answer, I helped me a lot. I think if I post the whole code it would be better to explain things.
I will post the code to someone who may need it.
/*
* 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 5 // 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 6 // Pin 10 - connect to serial data load pin (DATA)
#define RESET 7 // Pin 11 - connect to reset pin (RST).
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
#include <SD.h>
int n=300;
int sensorValue[300];
const int chipSelect = 10;
char filename[50];
// 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.");
int m = 0;
snprintf(filename, sizeof(filename), "data%03d.txt", m); // includes a three-digit sequence number in the file name
while(SD.exists(filename)) {
m++;
snprintf(filename, sizeof(filename), "data%03d.txt", m);
}
File dataFile = SD.open(filename,FILE_READ);
Serial.println(m);
Serial.println(filename);
dataFile.close();
//now filename[] contains the name of a file that doesn't exist
}
void loop() {
for (unsigned long fq = 20000; fq<= 40000; fq+=5) {
sendFrequency(fq); // freq
//while(1);
int x=0;
int sensorPeakSum = 0;
for (int i = 2; i<= n; ++i) {
sensorValue[i] = analogRead(A0);
if (sensorValue[i-2] < sensorValue[i-1] & sensorValue[i-1]>sensorValue[i]) {
sensorPeakSum+= sensorValue[i-1];
x=x+1;
}
else {
}
}
float voltage = (sensorPeakSum/x)* (15.0/1023.0);
File dataFile = SD.open("data001.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(voltage);
dataFile.print(",");
dataFile.print(fq);
dataFile.println();
dataFile.close();
// print to the serial port too:
Serial.print(voltage);
Serial.print(",");
Serial.print(fq);
Serial.println();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening file");
}
delay(1);
}
while (1) {}
}
I’m having a problem with the file name, if instead of using “File dataFile = SD.open(“data001.txt”, FILE_WRITE);” I use “File dataFile = SD.open(filename, FILE_WRITE);” it doesn’t work.
I was working with a similar code, without the if statement to find the peaks and the “filename” was working just fine.
Does anybody know why?