system
March 17, 2012, 7:07pm
11
Ok, here is the code, as I would modify what you put:
// prend la temperature sur A0 (MCP9700), la converti en Volts puis en Celsius puis
// imprime sur port serie
int tempPin = 0;
int loLedPin = 8;
int okLedPin = 9;
int hiLedPin = 10;
int piezoPin = 11;
int analogTempRead;
float celsiusTempRead;
int aref_voltage;
// PJ - Change 1: Pre declare volt here
float volt;
void setup() {
Serial.begin(9600);
aref_voltage = 50; // *10 to avoid setting a float if using 3.3V
pinMode(loLedPin, OUTPUT);
pinMode(okLedPin, OUTPUT);
pinMode(hiLedPin, OUTPUT);
pinMode(piezoPin, OUTPUT);
Serial.println("Ready.");
}
void loop()
{
// analogTempRead = 0; // PJ - Change 2: This is not needed with the next line change!
//for (int i =0; i<16; i++) // PJ - Change 3: No need to take and average 10 readings
analogTempRead = analogRead(tempPin);
// float volt = analogTempRead * (aref_voltage/10.0)/16368; // 16368 = 1024 * 16
volt = analogTempRead * (aref_voltage/1024.0); // change 4: 1024.0, otherwise will calc integer value!!
Serial.print(volt); Serial.println(" V");
int celsiusTempRead = ((volt-.5)*100)+0.5; // +0.5 to round up
//celsiusTempRead = 150;
Serial.print("Temperature = "); Serial.print(celsiusTempRead);
Serial.println(" C");
if (celsiusTempRead >= -30 && celsiusTempRead <= 0) {
digitalWrite(loLedPin, HIGH);
digitalWrite(okLedPin, LOW);
digitalWrite(hiLedPin, LOW);
delay(300);
}
if (celsiusTempRead > 0 && celsiusTempRead <= 8) {
digitalWrite(loLedPin, LOW);
digitalWrite(okLedPin, HIGH);
digitalWrite(hiLedPin, LOW);
delay(300);
}
if (celsiusTempRead > 8 && celsiusTempRead <= 100) {
digitalWrite(loLedPin, LOW);
digitalWrite(okLedPin, LOW);
digitalWrite(hiLedPin, HIGH);
delay(300);
}
if (celsiusTempRead < -30 || celsiusTempRead > 100) {
digitalWrite(loLedPin, HIGH);
digitalWrite(okLedPin, LOW);
digitalWrite(hiLedPin, HIGH);
analogWrite(piezoPin, 1);
delay(150);
digitalWrite(loLedPin, LOW);
digitalWrite(okLedPin, HIGH);
digitalWrite(hiLedPin, LOW);
delay(150);
}
// Change 5: Not much point taking readings more than once per second
delay(1000);
}
So five changes in all:
Change 1: Pre declare volt in preamble as a global variable. No need to declare each cycle of loop
Changes 2 and 3: No need to read and average 16 values (at least not at the moment). Once you have it working for one reading, do some tests to see how stable the readings are and decide if smoothing is necessary.
Change 4: This is probably the key change. The value and type of aref_voltage is integer. If you divide 50 by 1024 as an integer calculation the result will be 0. To promote the calculation to a floating point calculation, you need to make one of the values a float, hence 1024.0. I suspect this change alone may make the values more sane.
Change 5: I would slow things down a bit - no need to take readings more frequently than once per second. I don't have anything from the data sheet to suggest frequent reading is a problem, but anyway it will consume unnecessary power.
Don't change the capacitor for a resistor. Understand the wiring diagram to see why. Have a look at the datasheet, page 7. A capacitor would only be required to help decouple capacitive loads (ie cables) running from the sensor. In your test case - not required. Do not put a resistor from Vin to out - it will just give you a full reading on the analog input pin!
Let us know how you get one! I should ppint out that I haven't compiled the modifications above, so I apologise if I have accidentally misplaced a semi-colon, or bracket.
Thanks for the suggestions. I implemented them with the following results.
I had to chage the aref_voltage from 50 to 5 due to the code modifications. As for change 4, I understand where you are coming from and I was stuck there for a while. I tried putting 10.0 or 1024.0 and it seemed it didn't change anything for the calculation, as long as there was a decimal number on the line.
Now, I tried without a resistor nor a capacitor, and I am getting readings from 23 to 27 (when actual temp is 24.6C). If I put a 10k resistor and a 1uF capacitor, then the readings are all smooth and 24C, all the time.
Regarding page 7 of the datasheet, I had a look but I couldn't understand why a resistor wouldn't work. Could you clarify please? Again, I have very very limited electronics knowledge and my maths skills are basic, I never studied physics or electronics.