Arduino Uno 与 18B20温度传感器 实验详录

DS18B20 单线数字温度传感器,即“一线器件”,其具有独特的优点:      

( 1 )采用单总线的接口方式 与微处理器连接时 仅需要一条口线即可实现微处理器与 DS18B20 的双向通讯。 单总线具有经济性好,抗干扰能力强,适合于恶劣环境的现场温度测量,使用方便等优点,使用户可轻松地组建传感器网络,为测量系统的构建引入全新概念。      

( 2 )测量温度范围宽,测量精度高 DS18B20 的测量范围为 -55 ℃ ~+ 125 ℃ ; 在 -10~+ 85°C 范围内,精度为 ± 0.5°C 。      

( 3 )持多点组网功能 多个 DS18B20 可以并联在惟一的单线上,实现多点测温。      

( 4)供电方式灵活 DS18B20 可以通过内部寄生电路从数据线上获取电源。因此,当数据线上的时序满足一定的要求时,可以不接外部电源,从而 使系统结构更趋简单,可靠性更高。      

( 5 )测量参数可配置 DS18B20 的测量分辨率可通过程序设定 9~12 位。    

DS18B20 具有体积更小、适用电压更宽、更经济、可选更小的封装方式,更宽的电压适用范围,适合于构建自己的经济的测温系统,因此也就被设计者们所青睐。

 

产品封装 
DS18B20产品封装 
时序图 
DS18B20时序图 
典型应用电路

  1. 典型电路 
    典型外部供电方式取电
  2. 寄生供电方式 
    寄生供电方式

支持命令集 
命令集 
复位时序 
复位
读写时序 
读写

 

具体操作:

1、打开IDE,项目-加载库-管理库,搜索下载安装相应的库,不然程序写好了,编译时会报错;

Arduino Uno 与 18B20温度传感器 实验详录_第1张图片

Arduino Uno 与 18B20温度传感器 实验详录_第2张图片

搜索 18B20 ,看到18B20相关的库,点击 安装好,

打开 文件-示例,第三方库刚安装好的库,找到第一个例子Alarm;

Arduino Uno 与 18B20温度传感器 实验详录_第3张图片

看到除了DallasTemperature.h,还需要另一个库OneWire.h,再按之前步骤,搜索 OneWire 安装即可;ONE_WIRE_BUS 2 意思是 数据口连接开发版引脚pin 2;

Arduino Uno 与 18B20温度传感器 实验详录_第4张图片

2、Arduino 开发版用USB连接电脑,选择对应的开发版和端口,编译上传烧录,

Arduino Uno 与 18B20温度传感器 实验详录_第5张图片

Arduino Uno 与 18B20温度传感器 实验详录_第6张图片

3、接线,必须加电阻,不加电阻检测不到设备;

管脚定义:面朝印字面,左为GND,右为VCC,中间为数字输出引脚(须接上4.7K—10K的上拉电阻)本例4.7K电阻;

BOM表

Arduino Uno *1

18B20温度传感器  *1

4.7K电阻*1

 

接线

Arduino Uno     <------>         18B20温度传感器  颜色

Pin 2                <------>           DO       白色

5V                       <------>          VCC   红色

GND                   <------>          GND   黄色

 

Arduino接线图

Arduino Uno 与 18B20温度传感器 实验详录_第7张图片

4、接好线后,再用USB连接电脑,打开串口监视器查看结果;

Arduino Uno 与 18B20温度传感器 实验详录_第8张图片

 

Arduino Uno 与 18B20温度传感器 实验详录_第9张图片

 

Alarm案例代码,可根据需要自行修改:

#include 
#include 

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  
  // locate devices on the bus
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // search for devices on the bus and assign based on an index.
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  Serial.print("Device 0 Alarms: ");
  printAlarms(insideThermometer);
  Serial.println();
  
  Serial.print("Device 1 Address: ");
  printAddress(outsideThermometer);
  Serial.println();

  Serial.print("Device 1 Alarms: ");
  printAlarms(outsideThermometer);
  Serial.println();
  
  Serial.println("Setting alarm temps...");

  // alarm when temp is higher than 30C
  sensors.setHighAlarmTemp(insideThermometer, 30);
  
  // alarm when temp is lower than -10C
  sensors.setLowAlarmTemp(insideThermometer, -10);
  
  // alarm when temp is higher than 31C
  sensors.setHighAlarmTemp(outsideThermometer, 31);
  
  // alarn when temp is lower than 27C
  sensors.setLowAlarmTemp(outsideThermometer, 27);
  
  Serial.print("New Device 0 Alarms: ");
  printAlarms(insideThermometer);
  Serial.println();
  
  Serial.print("New Device 1 Alarms: ");
  printAlarms(outsideThermometer);
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
}

void printAlarms(uint8_t deviceAddress[])
{
  char temp;
  temp = sensors.getHighAlarmTemp(deviceAddress);
  Serial.print("High Alarm: ");
  Serial.print(temp, DEC);
  Serial.print("C/");
  Serial.print(DallasTemperature::toFahrenheit(temp));
  Serial.print("F | Low Alarm: ");
  temp = sensors.getLowAlarmTemp(deviceAddress);
  Serial.print(temp, DEC);
  Serial.print("C/");
  Serial.print(DallasTemperature::toFahrenheit(temp));
  Serial.print("F");
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  printTemperature(deviceAddress);
  Serial.println();
}

void checkAlarm(DeviceAddress deviceAddress)
{
  if (sensors.hasAlarm(deviceAddress))
  {
    Serial.print("ALARM: ");
    printData(deviceAddress);
  }
}

void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");

  // Method 1:
  // check each address individually for an alarm condition
  checkAlarm(insideThermometer);
  checkAlarm(outsideThermometer);
/*
  // Alternate method:
  // Search the bus and iterate through addresses of devices with alarms
  
  // space for the alarm device's address
  DeviceAddress alarmAddr;

  Serial.println("Searching for alarms...");
  
  // resetAlarmSearch() must be called before calling alarmSearch()
  sensors.resetAlarmSearch();
  
  // alarmSearch() returns 0 when there are no devices with alarms
  while (sensors.alarmSearch(alarmAddr))
  {
    Serial.print("ALARM: ");
    printData(alarmAddr);
  }
*/

}

 

参考链接:

【学习笔记】Arduino 与 DS18B20 数字温度传感器 联合调试

https://blog.csdn.net/qq_25915701/article/details/81591628

树莓派 GPIO学习笔记(3)--DS18B20温度传感器

https://blog.csdn.net/github_27696095/article/details/45288085

DS18B20温度传感器 - arduino

https://blog.csdn.net/chriz_w/article/details/52098165

Arduino Uno 和 DHT11/DHT22 温湿度传感器

https://blog.csdn.net/ling3ye/article/details/57075416

你可能感兴趣的:(智能小车机器人配件,Arduino)