int sensor = analogRead(A0); // Read in the value from
the analog pin connected to
// the temperature sensor
float voltage = (sensor / 1023.0) * 5.0; // Convert the value to voltage
float tempC = (voltage – 0.5) * 100; // Convert the voltage to temperature using the
/* scale factor; 0.5 is the deviation of the output voltage versus temperature from the
best-fit straight line derived from sensor calibration */
Serial.print(“Temperature: ”);
Serial.print(tempC); // Print the temperature on the Arduino IDE output
console
Here you see two types of variables, the integer (“int”) and the float (“float”). For an
Arduino UNO, an “int” is 16 bit long and can represent
a number ranging from
−32,768 to 32,767 (−2^15 to (2^15) − 1). A “float” in Arduino UNO is 32 bit long and
can represent a number that
has a decimal point, ranging from −3.4028235E+38 to
3.4028235E+38. Here, we define the variable of the temperature measured from the
LM35 sensor as a float type so that it can represent a decimal number and is more
accurate.
Dostları ilə paylaş: