Getting Started With a Temperature Sensor





Introduction: Getting Started With a Temperature Sensor

About: We bring hardware products to life, specializing in engineering, prototyping, and mass manufacturing. We've worked with over 20 Kickstarter projects.

Using basic electronic sensors, we can build our own heat detector. The main component, which is the LM35DZ temperature sensor, is used to measure the current room temperature. Based on the measurement, the detector will warn the user using other sensors such as the buzzer and the LED to indicate whether the room temperature exceeds the limit. This tutorial explains what kind of equipment we are using, how to program, and what to expect from it.

Step 1: Hardware

If you would like to follow along with this tutorial, then you will need the following materials:

Step 2: Hardware Assembly - Connecting Wires

Place a black and red wire on the breadboard. Connect the red wire to 5V on the Arduino while the black wire is connected to the GND.

Step 3: Hardware Assembly - Connecting the RGB LED

Place the RGB LED on the breadboard and connect the longer pin of the RGB to the GND of the Arduino. The right most pin (next to the longer pin) of the RGB LED should be connected to digital pin 8 on the Arduino while the left most pin should connect to the digital pin 7 on the Arduino. Note that LEDs act like a diodes, which means they are direction sensitive and don’t have internal resistance. Always use small resistors to prevent them from burring the metal.

Step 4: Hardware Assembly - Placing the Temperature Sensor

Place the LM35DZ Temperature Sensor on the breadboard. Then you are ready to connect the right most pin to the ground, the center pin to the analog pin A0 on the Arduino, and the left pin to the power supply (5V of the Arduino).

Step 5: Hardware Assembly - Adding the Buzzer

Place the buzzer on the breadboard. Connect the positive pin to the PWM (Pulse Width Modulation) pin 5 on the Arduino and the negative pin to the GND on the Arduino.

Step 6: Software Code & Explanation

// Declaring global variable <br> int R_LED = 8; // assigning the Red LED of the RGB to be connected at pin 8
 int B_LED = 7;// assigning 
 int Buz= 5 ;
 const int temp = 0; // assigning the temperature sensor pin connection
void setup() 
 Serial.begin (9600);// opening serial comminication between the computer and Arduino
 pinMode (R_LED, OUTPUT); // declaring the LED as an output
 pinMode (B_LED, OUTPUT);
 pinMode (Buz, OUTPUT);
 pinMode (temp, INPUT); } void loop() 
 float Real_Voltage = analogRead(temp)* 0.004882814; // reading the sensor value and convert it to voltage
 float Temp_C = Real_Voltage * 100.0;// changing the voltage into Celsius
 float Temp_F = Temp_C * (9.0/5.0) + 32.0;
 // printing the value of the serial monitor
 Serial.print("Voltage: ");
 Serial.print("Deg C: ");
 Serial.print("Deg F: ");
 // check if the sensor is above the maximum limit 
 if ( Temp_C >= 26.0)
   Serial.println("Warning: The system is hot");
   digitalWrite(R_LED, HIGH); // Red LED on
   delay(2000);// wait for one sec
   digitalWrite(R_LED, LOW);// Red LED off
   delay(2000); // wait for one sec
 // check if the sensor is above the mimimum limit  
 else if (Temp_C <= 25.0)
   Serial.println("Warning: The system is cold");
   digitalWrite(B_LED, HIGH);// Blue LED on
   digitalWrite(B_LED, LOW);
   delay(2000); // wait for a sec 
   Serial.println("The system is functioning well ");
   digitalWrite(R_LED, LOW);
   digitalWrite(B_LED, LOW);

The first part of the code defines the global variablepin that is used by the RGB LED, temperature sensor, and the buzzer. This pin can be used by any function declared in this program. The RGB is connected to a digital pin while the others to an analog pin on the Arduino.

Inside the setup function we have opened a serial communication between the computer and the Arduino using specific frequency band, which in this case is 9600. Then we defined which pin is used as an output or input. Question for you: what happens if you don’t declare temperature sensor pin and RGB pin as an input and output pin respectively? Why do you think that is?

When we look at the loop function, most of the main code resides in here. The first thing we did is read the value from the temperature sensor and converts it into voltage. The function analogRead() return is an integer value between 0 to 1023 since Arduino board contains 10 bit analog to digital converter (2^10 = 1024). In order to map this 10 bit character read by the temperature sensor into analog value, we need to multiply the sensor value with the resolution of the Arduino board, which is approximately 0.0049( 5V/1024 units ). After getting the voltage value, it needs to be converted into degree Celsius using the scale factor 0.01V/0C of LM35DZ temperature sensor.

Step 7: Check Our Work!

Then we check using a control structure like the “if statement” function to check whether it reached the maximum temperature (hot) or minimum temperature (cold). If it does, it will give out a warning using the LED and the buzzer as well as display the warning on the serial monitor.

If the detector doesn’t work after running the code, check the connection as well as any mistyping when writing the code. As we conclude this tutorial, we can now use this project to detect our surrounding or any other things you planned it to monitor. You should be able to see the blue LED on the RGB blink when the temperature reads below 25 °C. Also, the red LED on the RGB should blink when the temperature is above 26 °C.

If you have any questions about this tutorial, don't hesitate to post a comment, shoot us an email, or post it in our forum!

Need materials to get started? Check out Jaycon Systems online store!



    • Woodworking Contest

      Woodworking Contest
    • Planter Challenge

      Planter Challenge
    • Oil Contest

      Oil Contest

    We have a be nice policy.
    Please be positive and constructive.




    I made it! Thanks! I added the green LED for when the temperature is within parameters.


    im trying to upload the program on UNO board but its not getting uploaded

    i verified and dry run the program its correct. and

    on Proteus its working then i rechecked my board, serial port everything n and tested arduino board too but problem is not getting solved

    3 replies

    Hi! Thanks for reading!

    I'm not sure why you are having that kind of problem because you said you had rechecked the board, serial port, and so on. I know the code works and you said you had verified it.

    My best advice going off your comment would be to first recheck that you have the correct serial port. Then double check that you have your wires hooked up properly, sometimes it can be as simple as some mixed wires. Next, try unplugging and then replugging the connection between the computer and the board, and then try again. If all of that does not work, then try restarting the computer and rechecking that the arduino is working properly. There are a lot of things you do during setup, so I'm not positive exactly where the problem is coming from. If you are still having problems, let us know and we'll try and trouble shoot your project.


    hy JayconSystems thankyou i replaced my board with the new one and the code is getting uploaded

    Great! We're glad to hear that your project is now back on track! let us know how the project turned out and what you ended up using it for! Thanks for reading!

    I'm in the middle of building this but the piezo buzzer doesn't seem to beep very long how do I just the beep length and the volume of it?

    3 replies

    To change the tone level of the buzzer you need to change the voltage intensity through analogWrite(Buz, x). Note "x" can be from 0 to 255.

    To change how long the buzzer should make the sound is to increase the delay. Eg) delay (5000) should make the buzzer buzz for 5sec.

    I hope this answers your questions and helps you finish making this project.
    Thank you for reading!

    Thanks that helped a lot still had problems getting the buzzer to work so I just hooked it up to the red led and took the buzzer out of the program

    Thanks for the update on your project. Hope your projects are going well! Best wishes, and thanks for reading!