Introduction: Getting Started With a Temperature Sensor

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.println(analogRead(temp));
 Serial.print("Voltage: ");
 Serial.println(Real_Voltage);
 Serial.print("Deg C: ");
 Serial.println(Temp_C);
 Serial.print("Deg F: ");
 Serial.println(Temp_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
   analogWrite(Buz,20);
   delay(2000);// wait for one sec
   digitalWrite(R_LED, LOW);// Red LED off
   analogWrite(Buz,0);
   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
   analogWrite(Buz,80);
   delay(2000);
   digitalWrite(B_LED, LOW);
   analogWrite(Buz,0);
   delay(2000); // wait for a sec 
 }
 else
 {
   Serial.println("The system is functioning well ");
   digitalWrite(R_LED, LOW);
   digitalWrite(B_LED, LOW);
   delay(2000);
 }
}

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!

Comments

author
KobusTheron (author)2016-11-12

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

author
Ghum (author)2016-03-06

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

author
JayconSystems (author)Ghum2016-03-10

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.

author
Ghum (author)JayconSystems2016-03-10

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

author
JayconSystems (author)Ghum2016-03-10

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!

author
chrisb50 (author)2016-02-18

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?

author
JayconSystems (author)chrisb502016-02-19

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!

author
chrisb50 (author)JayconSystems2016-03-01

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

author
JayconSystems (author)chrisb502016-03-08

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

About This Instructable

5,334views

117favorites

License:

Bio: We bring hardware products to life, specializing in engineering, prototyping, and mass manufacturing. We've worked with over 20 Kickstarter projects.
More by JayconSystems:How to Measure a Corner's Radius Using a 3D Printed ToolSending Commands Via the LCD BackpackProgramming a Single 7 Segment Display
Add instructable to: