Hi everyone,
I'm reading voltages on the 6 analog pins and 5 seem to be working fine. Pin A4 however, always gives me a higher voltage no matter what I connect to it. This is the same when I use a different Uno. I'm a little baffled by this and was wondering if anybody had this issue or knows what's going on?
My serial output:
12:55:41.777 -> 3.43
12:55:41.777 -> 3.41
12:55:41.777 -> 3.42
12:55:41.777 -> 3.41
12:55:41.777 -> 4.12
12:55:41.810 -> 3.42
My code:
/*
Setup of Arduino and SD card
* analog sensors on analog A0 to A5
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** SCK - pin 13
** CS - pin 4
** power to 3.3, need to check output as USB doesn't provide enough power to run it. Gives error of particition not found if not enough juice
RTC
VCC to 5V
GND to GND
SDA to SDA
SCL to SCL
SQW unused
// Thermistor connection
// Vin (5V) --\/\thermistor\/\---- Vout to analog input pin
// |
// ---(\/\/\R2\/\/\)--- GND
// For best results select R1 equal to the thermistor resistance
// at the temperature being read (maximizes resolution of ADC)
// (25k Ohm coresponds to ~6C)
// Thermistor: Thermistors, US Sensor PS103J2, 10K ohm 615-1003-ND Digikey
// Resistor: RESISTOR, 0.01% 25K; Resistance:25kohm; Resistance Tolerance:± 0.01% USR2-0808-25KT-ND Digikey
print_time() returns a string in the format mm-dd-yyyy hh:mm:ss
*/
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
//RTC_DS1307 rtc;
const int chipSelect = 4; // where SD CS is connected
File dataFile;
/*String print_time(DateTime timestamp) {
char message[120];
int Year = timestamp.year();
int Month = timestamp.month();
int Day = timestamp.day();
int Hour = timestamp.hour();
int Minute = timestamp.minute();
int Second= timestamp.second();
sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month,Day,Year,Hour,Minute,Second);
return message;
}*/
void setup(){
Serial.begin(9600); // opens connection to arduino
pinMode(4, OUTPUT); // select pin for output, where CS is on SD card
if (!SD.begin(4)){
Serial.println("Error: SD card would not initiate.");
} else {Serial.println("initialization done.");
}
/* Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}*/
dataFile = SD.open("TMSt1.csv", FILE_WRITE); // makes file and read and writes
if (dataFile) {
Serial.println("writing to TMSt1.csv ");
dataFile.println("Date, Time, TM1, TM2, TM3, TM4, TM5"); // writes header into file once, if reset pressed it will do it again at the end of the file before new reading begin
dataFile.close(); // ensures that header is saved, always need to close file to save
} else {
Serial.println("TMSt1.csv didn't open ");
}
}
void loop(){ // as a loop it keeps reading and writing the values into file on sd card
int sensorValue_A0 = analogRead(A0); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A0 = sensorValue_A0 * (5 / 1023.0);
int sensorValue_A1 = analogRead(A1); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A1 = sensorValue_A1 * (5 / 1023.0);
int sensorValue_A2 = analogRead(A2); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A2 = sensorValue_A2 * (5 / 1023.0);
int sensorValue_A3 = analogRead(A3); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A3 = sensorValue_A3 * (5 / 1023.0);
int sensorValue_A4 = analogRead(A4); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A4 = sensorValue_A4 * (5 / 1023.0);
int sensorValue_A5 = analogRead(A5); //pin at which thermistor is connected (A1-A5)
// convert analog to voltage
float voltage_A5 = sensorValue_A5 * (5 / 1023.0);
//DateTime now = rtc.now(); //gets current date and time from rtc
Serial.println(voltage_A0); // to check it's reading something and if it makes sense, need to check how to write all readings in one line
Serial.println(voltage_A1);
Serial.println(voltage_A2);
Serial.println(voltage_A3);
Serial.println(voltage_A4);
Serial.println(voltage_A5);
/*Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);*/
Serial.println();
File dataFile = SD.open("TMSt1.csv", FILE_WRITE);
if (dataFile){
// dataFile.println(now); //write date and time reading in file
//dataFile.println(","); //seperates voltage from date and time
dataFile.print(voltage_A0); //writes voltage in file
dataFile.println();
dataFile.print(",");
dataFile.print(voltage_A1);
dataFile.println();
dataFile.print(",");
dataFile.print(voltage_A2);
dataFile.print(",");
dataFile.print(voltage_A3);
dataFile.print(",");
dataFile.print(voltage_A4);
dataFile.print(",");
dataFile.println(voltage_A5);
dataFile.close(); //works perfectly now, write everything until println in same line
}
else{ Serial.println("couldn't open file");
}
delay(5000); //prints time and voltage every 5 seconds
}

