No Reading from analog Pin

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

Moderators: adafruit_support_bill, adafruit

No Reading from analog Pin

Postby Halfback28 » Tue Apr 10, 2012 12:53 pm

Hello,

I just started using/playing around with my Arduino and the Dataloggershield 1 week ago, therefore I don't know too much about it. Additonally I am an mech. engineer and no expert on electronics.
I want to do temperature measurements and resistance measurements at the same time and log everything onto my SD card using the logger shield. For the temperature I use 2 waterproof DS18B20 that are hooked up to digital pin 8. For the resistance I build my own voltage splitter with a known resistance of 2167 Ohmm hooked up to analog pin 0.
Since I am fairly new to the entire think I have just been clueing parts of code together that I was able to find in the toturials for the different parts.
When I run the different measurement independly, they both work like a charm, but when I include the resistance measurement into the temperature and logging sketch, the analogread commant seems not the read the pin. It always returns a value of 1023, resulting in a calculated resistance for the "to-be-measured" resistors of zero. I have manually measurent the voltage form pin 0 to grd with a multimeter and read a value that I am expecting considering the resistor I hooked up as R2 for testing.
I hope this text is somewhat understandable. I am sorry for any mistake, but please consider that english is not my motherlanguage ;)
Here is the code I am using and the results from the Serial monitor:

Code: Select all
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>

#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!

OneWire ds(8); // on pin 8
RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
float temps[3]={0,0,0};
int resPin = 0; //Pin for resistance Measurment

int R1 = 2167; //first resistance in Voltage splitter

int resValue = 500;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println();
 
  // use debugging LEDs
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
 
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card
  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)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
 

  logfile.println("millis,stamp,datetime,temp1,temp2,res");   
#if ECHO_TO_SERIAL
  Serial.println("millis,stamp,datetime,temp1,temp2,res");
#endif //ECHO_TO_SERIAL

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

void loop(void)
{
  DateTime now;

  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 
  digitalWrite(greenLEDpin, HIGH);
 
  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");   
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", "); 
#endif

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');
#if ECHO_TO_SERIAL
  Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print(", ");
  Serial.print('"');
  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.print('"');
#endif //ECHO_TO_SERIAL

for(int k=0;k<1;k++)
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;
 
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    //return;
  }
 
  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      digitalWrite(redLEDpin,HIGH);
      return;
  }
  Serial.println();

   //the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      //Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      //Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      //Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
 
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
 
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }
 
  // convert the data to actual temperature

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
   digitalWrite(greenLEDpin, LOW);
   
  temps[k] = celsius;
  //logfile.println(celsius);
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.println(" Celsius, ");
}

for (int t=0;t<2;t++){
  logfile.print(",");
  logfile.print(temps[t]);
}

  Serial.print(resValue);
  analogRead(resPin);
  delay(20);
  resValue = analogRead(resPin);
  Serial.println(resValue);
  float voltage2 = resValue * 5.0 / 1023.0;
  Serial.println(voltage2);
  float R2 = R1 * voltage2 / (5.0-voltage2);
 
  logfile.print(",");
  logfile.println(R2);
 
  Serial.print("Resistance: ");
  Serial.println(R2);
 
  //logfile.println(",");

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
 
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
 
}



Code: Select all
Initializing SD card...card initialized.
Logging to: LOGGER21.CSV
millis,stamp,datetime,temp1,temp2,res
1000, 1334054979, "2012/4/10 10:49:39"
  Temperature = 20.12 Celsius,
5001023
5.00
Resistance: 0.00
3000, 1334054981, "2012/4/10 10:49:41"No more addresses.


  Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
4999, 1334054983, "2012/4/10 10:49:43"
  Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
6999, 1334054985, "2012/4/10 10:49:45"No more addresses.


  Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
8999, 1334054987, "2012/4/10 10:49:47"
  Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
10998, 1334054989, "2012/4/10 10:49:49"No more addresses.



As you can see "resValue" is 500 at the beginning (as defined at the top) and than is overwritting by the returned analogread of 1023. In this example I am only using 1 DS18B20, but I get the same results with 2 of them.

Does anybody have an idea? I tried differnt analog pin. I tried refering to them with "A0" and declaring them as input. I also found the tip to deploy a short delay after a analogread command and then reading again. Nothing has worked so far.

Thank you!
Halfback28
 
Posts: 3
Joined: Tue Apr 10, 2012 12:24 pm

Re: No Reading from analog Pin

Postby franklin97355 » Tue Apr 10, 2012 1:18 pm

You should try the example in the library download to see if your wiring is working. Simplify your code to the bare essentials until you get working code then add features.
User avatar
franklin97355
 
Posts: 1706
Joined: Mon Apr 21, 2008 1:33 pm

Re: No Reading from analog Pin

Postby adafruit_support_bill » Tue Apr 10, 2012 1:22 pm

The problem is here:
Code: Select all
    int resPin = 0; //Pin for resistance Measurment

It is reading from digital pin 0. The numeric value for analog pin 0 is 14. So:

analogRead(A0);

is the same as

analogRead(14);

but not the same as

analogRead(0);
User avatar
adafruit_support_bill
 
Posts: 15995
Joined: Sat Feb 07, 2009 9:11 am

Re: No Reading from analog Pin

Postby Halfback28 » Tue Apr 10, 2012 1:33 pm

Thank you for the fast reply.

I tried both version:

Code: Select all
int resPin = A0;

and
Code: Select all
int resPin = 14;


I still get the same result. The command analogRead(resPin) returns a value of 1023, although the manually measrured voltage from analog Pin 0 to Ground is : ~1.72 Volts. This is roughly the correct value, considering that R1=2167 ohm and R2 = 1172.

This is the version of only the resistance measurement, which works just fine:

Code: Select all
/*
Widerstandmessung
Messung eines unbekannten Widerstand durch Anwendung eines Spannungsteilers

Resistance measurement
Measuring of a unkown resistance with a voltage splitter

The circuit:
2 Resistances in Series
hooked up to 5V and GRD
between connection to Analog Pin 5 (A5)


created by:
Holger Schmeing
4.4.2012
*/

int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int R1 = 2166; //know resistance in Ohm

void setup() {
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
}


void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
  //print value
  Serial.print(sensorValue);Serial.println(" Value on Pin 0");
  //convert value into voltag
  float voltage2 = sensorValue * 5.0 /1023.0;
  //print voltage
  Serial.print(voltage2); Serial.println(" Voltage U2");
  //calculate reistance 2
  float R2 = R1 * voltage2 / (5.0-voltage2);
  //print resistance 2
  Serial.print(R2);Serial.println("  Resistance 2");
 
  delay(2500);
                 
}
Halfback28
 
Posts: 3
Joined: Tue Apr 10, 2012 12:24 pm

Re: No Reading from analog Pin

Postby adafruit_support_bill » Tue Apr 10, 2012 1:48 pm

You have analog reference specified as EXTERNAL
Code: Select all
      // If you want to set the aref to something other than 5v
      analogReference(EXTERNAL);

but your calculations appear to be based on the default 5v reference. What do you have connected to the AREF pin?
User avatar
adafruit_support_bill
 
Posts: 15995
Joined: Sat Feb 07, 2009 9:11 am

Re: No Reading from analog Pin

Postby Halfback28 » Tue Apr 10, 2012 5:08 pm

Thank you so much! This seems to have been it. Sorry for my lack of knowledge! I suspected it was something easy/minor all along.
Thanks again!
Halfback28
 
Posts: 3
Joined: Tue Apr 10, 2012 12:24 pm


Return to Arduino

Who is online

Users browsing this forum: No registered users and 5 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]