Help reading analogue voltage

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
kaxxakt244
 
Posts: 1
Joined: Fri Jun 28, 2013 11:26 am

Help reading analogue voltage

Post by kaxxakt244 »

Hello,

I have three voltage inputs connected to the GND, A0, A1 and A2 respectively and am trying to measure the voltage from them. The problem I am having is that the analogue read is showing 1023 for all 3 irrespective of the voltage from 1-5V I input. I measured the voltage across the pin inputs of the arduino, i.e. at GND and A0, GND and A1, and GND and A2, it is showing the correct voltage. Would appreciate any help. PS I'm using an Arduino Uno with an Adafruit Data Logger Shield.

Here is the code:

Code: Select all

/*SD card datalogger
 This example shows how to log data from multiple analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, 2, and D4
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10
 
 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe
 Adapted for use 21 Mar 2013 by Robert Bantz */

#include <Wire.h> // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <SD.h>
#include "RTClib.h"

// the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

RTC_DS1307 RTC; //Define RTC Clock name

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

// If you want to set the aref to something other than 5v
analogReference(EXTERNAL); //This needed!

 //Initialize RTC Communitcation
 Wire.begin();
    RTC.begin();
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

//Initialize SD card communication
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // 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()
{
  uint32_t start = millis();
  
  DateTime now = RTC.now(); //Call RTC current time
  
  // make a string for assembling the data to log:
  String dataString = "";
  dataString +=now.year();
  dataString +="/";
  dataString +=now.month();
  dataString +="/";
  dataString +=now.day();
  dataString +=",";
  dataString +=now.hour();
  dataString +=":";
  dataString +=now.minute();
  dataString +=":";
  dataString +=now.second();
  dataString +=",";
  
  // read three sensors 
  int val0;
  float temp0;
  val0=analogRead(0);
  temp0=val0/4.092;
  val0=(int)temp0;
  
  int val1;
  float temp1;
  val1=analogRead(1);
  temp1=((val1*(5/1023))-2.5)/0.05;
  val1=(int)temp1;
  
  int val2;
  float temp2;
  val2=analogRead(2);
  temp2=val2*(5.0 / 1023);
    
  dataString +=val0;
  dataString +=",";
  dataString +=val1;
  dataString +=",";
  dataString +=val2;
  dataString +=",";
         
  // 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.csv", 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");
  }
 
 // Serial and SD card access times may vary.  Rather than delay(),
  // monitor time to allow remainder of 1 minute to pass.
  while(millis() < (start + 60000));
}
Last edited by adafruit_support_bill on Fri Jun 28, 2013 11:43 am, edited 1 time in total.
Reason: Please use the 'code' button when submitting code

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help reading analogue voltage

Post by adafruit_support_bill »

You have the analog reference set to EXTERNAL. What do you have connected to AREF?

Code: Select all

// If you want to set the aref to something other than 5v
analogReference(EXTERNAL); //This needed!


Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”