Ex 6
const int sensorPin = A0; // Analog input pin that the sensor is attached to
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read the ADC value (0-1023)
// Convert the ADC value to voltage
float voltage = sensorValue * (5.0 / 1023.0); // 5.0 for 5V reference, 1023 for 10-bit ADC
// Convert the voltage to temperature in Celsius
// TMP36: 500mV offset, 10mV per degree
float temperatureC = (voltage - 0.5) * 100.0;
// For LM35: (no need to subtract 0.5)
// float temperatureC = voltage * 100.0;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
delay(1000); // Wait for 1 second before the next reading
}