To sense temperature using the LM35 sensor connected to the Arduino board, perform the following steps:
- Invoke the Arduino IDE. Arduino will open up with a file showing its default content, as follows:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
- Connect the Arduino board to the PC.
- From the Tools menu, select Port and confirm whether it shows COM3 (Arduino/Genuino Uno) or whatever Arduino board you have attached to your PC. Additionally, confirm whether the Board option from the Tools menu indicates the Arduino board that is attached to your PC. In my case, the Board option will show Arduino/Genuino Uno.
- Type the following program into the editor window:
float voltage;
int tempPin = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
voltage = analogRead(tempPin);
float tempInCelsius = voltage * 0.48828125;
float tempinFahrenheit = (tempInCelsius*9)/5 + 32;
Serial.print("Temperature in Celsius is: ");
Serial.print(tempInCelsius);
Serial.print("*C");
Serial.println();
Serial.print("Temperature in Fahrenheit is: ");
Serial.print(tempinFahrenheit);
Serial.print("*F");
Serial.println();
delay(1000);
}
- Save the application by clicking on the File | Save As option. Specify the application name when prompted. Let's name the application SensorApp. A folder will be created with the name SensorApp and, within that folder, the application will be created with the filename SensorApp.ino.
- Upload the application to Arduino by clicking on the Upload icon in the toolbar.