Hello,
I am just beginning to work my way through the projects book and after writing the code and compiling and setting it up, I am getting the following when I run the com3 scroll. Windows systems setting shows that the Arduino is connected to com3.
Sensor value: 107 or 108, Volts: 0.00, degrees celsius: -50.00
I am wondering if I have wired it wrong or its a problem with the code.
Thanks in advance
This is my code:
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // open the serial port at 9600 baud
for(int pinNumber = 2; pinNumber < 5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
// create a variable to store the serial port reading
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
//convert the adc reading to voltage
float voltage = (sensorVal/1024) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
//convert the voltage to temperature in Celsius
float temperature = (voltage - .5) * 100;
Serial.println (temperature);
if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else if(temperature >= baselineTemp+2 && temperature >= baselineTemp+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else if(temperature >= baselineTemp+4 && temperature >=baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}
2. Bring the cursor at the very beginning of your code. Now, type this word: [ c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.
2. Bring the cursor at the very end of your code. Now, type this word: [ / c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.
I now get a voltage of .21 and a celsius temp of around -28.52. Changing the baselineTemp from 20.0 to 5.0 doesn't seem to make a difference. I'm wondering if its a wiring problem.
If 1024 steps produce 5.0V, then 'sensorVal' step(s) should give : (5.0/1024)*sensorVal volt
//(testing)connect 3.3V of UNO with A0-pin
int sensorVall = analogRead(A0);
float voltage = (5.0/1024)*sensorVal;
Serial.println(voltage, 2); // prints: 3.23; no need to cast (float) as one factor (5.0) is already float
What type of temperature sensor are you using? LM35 or LM36 or any other type? It is very important to know the sensor type as the gain and offset vary sensor to senor, and these two factors determine the response equation of the sensor.
Was about to post that the code is for a TMP36 (not LM35), but I saw GolamMostafa already did.
You get a 50 degrees C offset when you use TMP36 code for an LM35 (-28.52 + 50 = 21.48)
This is the most simple LM35 maths line to convert to tempC with a 10-bit A/D and 1.1volt Aref enabled:
(analogRead(A0) * 0.1039)
Sketch for both types of sensors attached (set for LM35).
It can be further improved with averaging/smoothing code.
Leo..
// LM35_TMP36 temp
// connect LM35 to 5volt A0 and ground
// connect TPM36 to 3.3volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"
float tempC; // Celcius
float tempF; // Fahrenheit
void setup() {
analogReference(INTERNAL); // use internal 1.1volt Aref
Serial.begin(9600);
}
void loop() {
tempC = ((analogRead(A0) * 0.1039)); // uncomment this line for an LM35
//tempC = ((analogRead(A0) * 0.1039) - 50.0); // uncomment this line for a TMP36
tempF = tempC * 1.8 + 32.0; // C to F
Serial.print("Temperature is ");
Serial.print(tempC, 1); // one decimal place
Serial.print(" Celcius ");
Serial.print(tempF, 1);
Serial.println(" Fahrenheit");
delay(1000); // use a non-blocking delay when combined with other code
}