|
LM35温度传感器是一款用于测量环境温度的理想温度传感器。它提供与温度成比例的线性输出,0V对应于0℃,每度C变化的输出电压变化为10 mV。 LM35比热敏电阻和热电偶更容易使用,因为它们是线性的,不需要信号调理。您可以使用Arduino开发板通过LM35温度传感器监控空气温度。
LM35的输出可以直接连接到Arduino模拟输入。由于Arduino模数转换器(ADC)的分辨率为1024位,参考电压为5 V,因此用于根据ADC值计算温度的公式为: - temp = ((5.0 * analogRead(TemperaturePin)) / 1024) * 100.0
复制代码
为了显示温度,我们将使用液晶显示屏(LCD1602)。
实验
本实验的目的是使用LM35、 LCD1602和Arduino开发板制作一个温度监控器。
需要的硬件 ● Arduino Mega2560开发板 ● LCD1602显示屏 ● 5k电位器 ● 面包板 ● LM35温度传感器 ● 1k电阻 ● 连接导线
接线图
如上图所示连接组件。 LM35输出和GND之间连接一个1kΩ的电阻,以限制电流而不影响输出电压。
LCD连接到Arduino开发板,如下所示。电位器的中间端子连接到LCD的引脚3,用来改变LCD背光的亮度。电位器的另外两个引脚分别连接到5 V和GND。 Enable连接到Arduino的引脚9,RS连接到Arduino的引脚8,RW接地。 DB4 -----> 4脚 DB5 -----> 5脚 DB6 -----> 6脚 DB7 -----> 7脚 RS -----> 8脚 EN -----> 9脚
代码 该程序使用LiquidCrystal.h库将数据写入显示器。在loop()中,传感器输出的值被连续读取,转换为摄氏度C,然后显示在LCD显示屏上。 - #include //arduino lcd library
- LiquidCrystal lcd(8,9,4,5,6,7); //defining lcd pins
- int value=0; //initializing variables
- float volts=0.0;
- float temp=0.0;
- float tempF=0.0;
- void setup()
- {
- pinMode(3,INPUT); //setting arduino pin3 as input
- Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
- lcd.begin(16,2); // set up the LCD's number of columns and rows
-
-
- }
- void loop()
- {
- value=analogRead(A0); //read from A0
- volts=(value/1024.0)*5.0; //conversion to volts
- temp= volts*100.0; //conversion to temp Celsius
- tempF=temp*9/5+32; //conversion to temp Fahrenheit
- //display temp no lcd
- Serial.print("temperature= ");
- Serial.println(temp);
- lcd.setCursor(0,0);
- lcd.print("TEMP= ");
- lcd.print(temp);
- lcd.print(" C");
- lcd.setCursor(0,1);
- lcd.print("TEMP= ");
- lcd.print(tempF);
- lcd.print(" F");
-
-
- delay(500);
- }
复制代码
|