Go Down

Topic: Errore nella compilazione con TMP36 (Read 3806 times) previous topic - next topic

Maurotec

Per migliorare ulteriormente il task ti consiglio di trasformare val_ADC in un valore risultante dalla media di più letture adc, cioè ad esempio:

Code: [Select]

// take N samples in a row, with a slight delay
byte n_samples = 10;
float average = 0;

    for (byte i=0; i< n_samples; i++) {
         average += analogRead(A0);
        _delay_us(200);
    }
average /= n_samples;


average contiene la media aritmetica di 10 letture adc distanti le une dalle altre 200us, valore che puoi anche aumentare anche fino a 1000, cioè 1ms per 10 letture = 10ms tanto dura il task.

Non scartare l'idea di trasformare questo codice in una funzione con prototipo simile:
Code: [Select]
float readTMP36(byte n_samples)

Ciao.

antoniocnn

#31
Jun 24, 2016, 04:58 pm Last Edit: Jun 25, 2016, 10:39 pm by antoniocnn
Per migliorare ulteriormente il task ti consiglio di trasformare val_ADC in un valore risultante dalla media di più letture adc
Ho ascoltato il tuo esempio e condivido con voi per correggere gli eventuali errori e migliorare il task.

Code: [Select]

/*
   FILE:    TMP36.24GIU16
   PURPOSE: Test program to show minimum and maximum temperature with average
            Used AREF voltage to getting better precision
   PROBE:   TMP36
   AUTHOR:  Antonio Cannavale
   COMFORT:
 
   Formula keys: aref_voltage=Volts ADC, 0.5=Volts offset, 0.01=10mV/°C
 
   TMP36 connected to Arduino as follow:
   GND  - GND
   Vout - Analog Input
   Vin  - AREF voltage
*/

#define aref_voltage 3.27 // We tie 3.3V to AREF and measure it with a multimeter!
float val_ADC; // Set variable voltage
float TempC; // Set variable temperature
float MaxTempC; // Set variable maximum temperature
float MinTempC; // Set variable minimum temperature

void setup() { // Start setup
  Serial.begin(9600); // Start Serial Communication
  analogReference(EXTERNAL);
  delay(500); // Delay to let system boot
  Serial.println("TEMPERATURE TEST PROGRAM");
  Serial.println("------------------------");
  Serial.println();
  Serial.println("Type ,\tmV ,\tT(C) ,\tMin(C)  Max(C)");
  byte n_samples = 10; // Take N samples in a row, with a slight delay
  float average = 0;
    for (byte i=0; i<n_samples; i++) {
         average += analogRead(A0); // Getting voltage from sensor at Analog PIN A0
        _delay_us(200);
    }
  average /= n_samples;
  val_ADC = average; // Getting voltage from average
  TempC = (val_ADC*aref_voltage/1023-0.5)/0.01; // Formula
  MaxTempC=MinTempC=TempC;
} // End setup

void loop() { // Start loop
  byte n_samples = 10; // Take N samples in a row, with a slight delay
  float average = 0;
    for (byte i=0; i<n_samples; i++) {
         average += analogRead(A0);
        _delay_us(200);
    }
  average /= n_samples;
  val_ADC = average; // Getting voltage from average
  TempC = (average*aref_voltage/1023-0.5)/0.01; // Formula
  if (TempC<MinTempC) {
    MinTempC=TempC;
  }
  if (TempC>MaxTempC) {
    MaxTempC=TempC ;
  }
  // Serial.println(byte);
  Serial.print("TMP36,\t");
  Serial.print(average); // Print mV ADC value
  Serial.print(",\t");
  Serial.print(TempC); // Print actual celsius temperature
  Serial.print(",\t");
  Serial.print(MinTempC); // Print minimum celsius temperature
  Serial.print(",\t");
  Serial.println(MaxTempC); // Print maximum celsius temperature
  delay(120000); // Wait 2 minutes before accessing sensor again
} // End loop


Grazie
Antonio

antoniocnn

#32
Jun 27, 2016, 06:43 pm Last Edit: Jun 27, 2016, 09:00 pm by antoniocnn
ciao tutti,
eccomi con un nuovo problema.
Ho utilizzato il seguente codice
Code: [Select]

/*
   FILE:    TMP36.26GIU16
   PURPOSE: Temperature test program also show minimum, maximum
            and write data to SD_CARD file.
            Used AREF voltage to getting better precision
   PROBE:   TMP36
   AUTHOR:  Antonio Cannavale
   COMFORT: T 21C
  
   Formula keys: aref_voltage=Volts ADC, 0.5=Volts offset, 0.01=10mV/°C
 
   TMP36 connected to Arduino as follow:
   GND  - GND
   Vout - Analog Input
   Vin  - 3.3V

   SD card connected to Arduino as follows:
    _________
   |876543219\
   1 - PIN10   4 - 3.3V    7 - PIN12
   2 - PIN11   5 - PIN13   8 - NC
   3 - GND     6 - GND     9 - NC
*/

#include <SPI.h> // Include SPI library
#include <SD.h> // Include SD library

#define aref_voltage 3.3 // We tie 3.3V to AREF and measure it with a multimeter!

float val_ADC; // Set variable voltage
float TempC; // Set variable temperature
float MaxTempC; // Set variable maximum temperature
float MinTempC; // Set variable minimum temperature

File myFile;

void setup() { // Start setup
  Serial.begin(9600); // Start Serial Communication
  delay(500); // Delay to let system boot
  
  // Initialization SD Card and check to see if the file exists
  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println(" initialization failed!");
    Serial.println("TEMPERATURE TEST PROGRAM");
    Serial.println("------------------------");
    Serial.println();
    Serial.println("Type ,\tmV ,\tT(C) ,\tMin(C)  Max(C)");
    analogReference(EXTERNAL);
    byte n_samples = 10; // Take N samples in a row, with a slight delay
    float average = 0;
    for (byte i=0; i<n_samples; i++) {
       average += analogRead(A0); // Getting voltage from sensor at Analog PIN A0
       _delay_us(200);
    }
    average /= n_samples;
    val_ADC = average; // Getting voltage from average
    TempC = (val_ADC*aref_voltage/1023-0.5)/0.01; // Formula
    MaxTempC=MinTempC=TempC;
    return; // Don't do anything more
  }
  Serial.println(" initialization done.");
  if (SD.exists("example.txt")){
    Serial.println("example.txt exists.");
    Serial.println("Removing example.txt...");
    SD.remove("example.txt");
  }
  else {
    Serial.println("example.txt doesn't exists.");
  }
  /* Open a new file. Note that only one file can be open at a time,
     so you have to close this one before opening another.
  */
  Serial.print("Creating and writing to example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);

  // Check to see if the file exists
  if (SD.exists("example.txt")) {
     Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exists.");
  }
  // Delete the file
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
   Serial.println();
   Serial.println("TEMPERATURE TEST PROGRAM");
   Serial.println("------------------------");
   Serial.println();
   Serial.println("Type ,\tmV ,\tT(C) ,\tMin(C)  Max(C)");

  // If the file opened okay, write to it:
  if (myFile) {
    myFile.println("TEMPERATURE TEST PROGRAM");
    myFile.println("------------------------");
    myFile.println();
    myFile.println("Type ,\tmV ,\tT(C) ,\tMin(C)  Max(C)");
    myFile.close(); // Close the file
    }
  else {
    // If the file didn't open, print an error:
    Serial.println("error opening example.txt");
  }
} // End setup

void loop() { // Start loop
  myFile = SD.open("example.txt", FILE_WRITE); // Open the file
  byte n_samples = 10; // Take N samples in a row, with a slight delay
  float average = 0;
    for (byte i=0; i<n_samples; i++) {
         average += analogRead(A0);
        _delay_us(200);
    }
  average /= n_samples;
  val_ADC = average; // Getting voltage from average
  TempC = (val_ADC*aref_voltage/1023-0.5)/0.01; // Formula
  if (TempC<MinTempC) {
    MinTempC=TempC;
  }
  if (TempC>MaxTempC) {
    MaxTempC=TempC ;
  }
  Serial.print("TMP36,\t");
  Serial.print(average);
  Serial.print(",\t");
  Serial.print(TempC);
  Serial.print(",\t");
  Serial.print(MinTempC);
  Serial.print(",\t");
  Serial.println(MaxTempC);
  // Write file
  myFile.print("TMP36,\t");
  myFile.print(average);
  myFile.print(",\t");
  myFile.print(TempC);
  myFile.print(",\t");
  myFile.print(MinTempC);
  myFile.print(",\t");
  myFile.println(MaxTempC);
  myFile.close(); // Close the file
  delay(120000); // Wait 2 minutes before accessing sensor again
} // End loop

ma nonostante sia simile a quello utilizzato nella mia discussione per il DHT11, non inizializza la sd card.
Ho utilizzato gli stessi collegamenti.
Qualcuno può aiutarmi?

Grazie
Antonio

Go Up