How to use Arduino and LM35 temperature sensor to monitor temperature? Here I will give you a brief introduction
SuppliesLM35 temperatore sensor
arduino
breadboard
Step 1: Place the Sensor on the BreadboardLet's connect these things first. Put the sensor on the breadboard, and then connect it to the Arduino according to the schematic below.
Step 2: Connecting to ArduinoThe left pin is the power pin, connected to the 5V power hole of Arduino with a red wire, the rightmost is the ground, connected to the GND hole of Arduino with a black wire, and the middle pin is the temperature data output, which we connect to the analog signal port 0 (A0). Below is the code to read the temperature value from A0. If yours doesn't work, it is likely that the left and right are reversed, just change the direction and reconnect it.
Step 3: NoticeYou may have noticed that there is a red CC3000 WIFI shield expansion board on the Arduino. I use this to connect to the Internet. I won’t use it now, so you can ignore it.
Step 4: CodingNow it’s time to write some code. Open the Arduino IDE and enter the following code:
float temp = 0;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
Serial.println(F("reading temperature begin. \n"));
}
// the loop routine runs over and over again forever:
void loop() {
static unsigned long sensortStamp = 0;
if(millis() - sensortStamp > 100){
sensortStamp = millis();
// read the LM35 sensor value and convert to the degrees every 100ms.
int reading = analogRead(0); //注意到我们是把LM35的输出端连接到了A0,所以这里是analogRead(0)
temp = reading *0.0048828125*100;
Serial.print(F("Real Time Temp: "));
Serial.println(temp);
}
}
Step 5: Upload the Code to ArduinoAfter writing the code, you can upload it to the Arduino and execute it. After uploading, open the serial editor and you should be able to see the current temperature output. You can find the "Serial Monitor" in the "Tools" menu of the Arduino IDE. You can choose the baud rate in the lower right corner of the serial monitor. We have to choose 115200 baud because we set it in setup: Serial.begin(115200); Otherwise you may not see the output.
To verify, you can use a hair dryer to heat the sensor to see if the temperature changes. :)
Comments
Please log in or sign up to comment.