Temperature sensor DS18B20 experiment
Experimental phenomena
Read the DS18B20 temperature sensor value, and then send it to the PC to display it through the serial port
Theoretical study
Schematic diagram
Code writing
Need to install OneWire and DallasTemperature libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// 数据输出脚接开发板数字引脚2
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(9600);
sensors.begin();
}
void loop(void)
{
sensors.requestTemperatures(); // 发送命令获取温度
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(500);
}