Hi all,
I'm working on a project and I have a sinusoïdal signal (positive and negative voltages). I'd like to read it with an analog pin and print the results on an SD card. The problem is it is seams that the Arduino can only read positive values.
For example: I used a signal generator and injected in analog pin A0 a sinusoidal signal of 2V peak-to-peak. Which means the signal varies from -1V to 1V. When I read the analog pin, I get values somewhat like this: 0, 0.3, 0.6, 0.9, 1, 0.8, 0.6, 0.2, 0, 0, 0, 0, 0, 0, 0, 0.3, 0.5, 0.8 and it goes on. Obviously, the negative values are automatically put to 0. It's driving me crazy because I absolutely need those negative values!
Is there any way to get those values??? I'd appreciate some help. Thank you!
Here's the code I'm using:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
double Time = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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:
return;
}
Serial.println("card initialized.");
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
int X = 1; // Gain that can be varied
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 1; analogPin++) {
double sensor = analogRead(analogPin);
float voltage = sensor * (3.3 / 1023.0); // conversion factor to output voltages correctly
dataString += String(Time) + " ";
dataString += String(voltage*X);
}
// 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("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1); // delay at which the values are taken
Time += 0.001;
}
I'm working on a project and I have a sinusoïdal signal (positive and negative voltages). I'd like to read it with an analog pin and print the results on an SD card. The problem is it is seams that the Arduino can only read positive values.
For example: I used a signal generator and injected in analog pin A0 a sinusoidal signal of 2V peak-to-peak. Which means the signal varies from -1V to 1V. When I read the analog pin, I get values somewhat like this: 0, 0.3, 0.6, 0.9, 1, 0.8, 0.6, 0.2, 0, 0, 0, 0, 0, 0, 0, 0.3, 0.5, 0.8 and it goes on. Obviously, the negative values are automatically put to 0. It's driving me crazy because I absolutely need those negative values!
Is there any way to get those values??? I'd appreciate some help. Thank you!
Here's the code I'm using:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
double Time = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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:
return;
}
Serial.println("card initialized.");
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
int X = 1; // Gain that can be varied
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 1; analogPin++) {
double sensor = analogRead(analogPin);
float voltage = sensor * (3.3 / 1023.0); // conversion factor to output voltages correctly
dataString += String(Time) + " ";
dataString += String(voltage*X);
}
// 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("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1); // delay at which the values are taken
Time += 0.001;
}