麻瓜学习笔记:02_OneWare总线多个DS18B20数据读取

OneWare总线,意思就是一条数据线就可以工作,而且支持多个传感器,相关资料很多,大家可以到网上查阅。因为感兴趣做家庭环境监测,就了解了一下相关的资料。最简单的就是连接一个DS18B20,其实多个连接也很简单,可以使用寄生供电或者独立供电。



DS18B20的接法如附图所示,可以一个,也可以多个,两线或者三线都可以工作。多个传感器三线接法与单个类似,传感器直接GVS并联就可以,4.7K电阻只要接一个就可以。网友评论有时两线不稳定,所以如果不受到接线条件限制,最好还是三线。



连接完成,当然要读取数据,因为连接在一条总线上面,通过读取相应地址传感器,就能得到需要的参数,其实还是很简单。大家一般使用OneWare库,如果使用DallasTemperature库就更简单,下面是DallasTemperature自带的程序,贴在下面供大家参考。



ARDUINO 代码复制打印下载

#include <OneWire.h>#include <DallasTemperature.h>// Data wire is plugged into port 2 on the Arduino#define ONE_WIRE_BUS 3#define TEMPERATURE_PRECISION 12// 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);int numberOfDevices; // Number of temperature devices foundDeviceAddress tempDeviceAddress; // We'll use this variable to store a found device addressvoid setup(void){// start serial portSerial.begin(9600);Serial.println("Dallas Temperature IC Control Library Demo");// Start up the librarysensors.begin();// Grab a count of devices on the wirenumberOfDevices = sensors.getDeviceCount();// locate devices on the busSerial.print("Locating devices...");Serial.print("Found ");Serial.print(numberOfDevices, DEC);Serial.println(" devices.");// report parasite power requirementsSerial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON");else Serial.println("OFF");// Loop through each device, print out addressfor(int i=0;i<numberOfDevices; i++){  // Search the wire for address  if(sensors.getAddress(tempDeviceAddress, i))    {        Serial.print("Found device ");        Serial.print(i, DEC);        Serial.print(" with address: ");        printAddress(tempDeviceAddress);        Serial.println();        Serial.print("Setting resolution to ");        Serial.println(TEMPERATURE_PRECISION,DEC);        // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)        sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);       Serial.print("Resolution actually set to: ");        Serial.print(sensors.getResolution(tempDeviceAddress), DEC);         Serial.println();    }else{        Serial.print("Found ghost device at ");        Serial.print(i, DEC);        Serial.print(" but could not detect address. Check power and cabling");    }}}// function to print the temperature for a devicevoid printTemperature(DeviceAddress deviceAddress){// method 1 - slower//Serial.print("Temp C: ");//Serial.print(sensors.getTempC(deviceAddress));//Serial.print(" Temp F: ");//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit// method 2 - fasterfloat tempC = sensors.getTempC(deviceAddress);Serial.print("Temp C: ");Serial.print(tempC);Serial.print(" Temp F: ");Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit}void loop(void){ // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the busSerial.print("Requesting temperatures...");sensors.requestTemperatures(); // Send the command to get temperaturesSerial.println("DONE");// Loop through each device, print out temperature datafor(int i=0;i<numberOfDevices; i++){  // Search the wire for address  if(sensors.getAddress(tempDeviceAddress, i))    {        // Output the device ID        Serial.print("Temperature for device: ");        Serial.println(i,DEC);        // It responds almost immediately. Let's print out the data        printTemperature(tempDeviceAddress); // Use a simple function to print out the data    }     //else ghost device! Check your power requirements and cabling}delay(10000);Serial.println(":");Serial.println(":");Serial.println(":");}// function to print a device addressvoid printAddress(DeviceAddress deviceAddress){for (uint8_t i = 0; i < 8; i++){  if (deviceAddress[i] < 16) Serial.print("




我只增加了十秒延时,还有打印分割,总体没有变动。大家使用以前,检查一下数模输入接线端口是否正确即可。


程序运行以后,输出如下结果到串口。ARDUINO 代码复制打印
Dallas Temperature IC Control Library DemoLocating devices...Found 3 devices.Parasite power is: OFFFound device 0 with address: 2828843B0400009FSetting resolution to 12Resolution actually set to: 12Found device 1 with address: 28D8AA3B040000B7Setting resolution to 12Resolution actually set to: 12Found device 2 with address: 28DC6754040000AFSetting resolution to 12Resolution actually set to: 12Requesting temperatures...DONETemperature for device: 0Temp C: 18.62 Temp F: 65.52Temperature for device: 1Temp C: 19.00 Temp F: 66.20Temperature for device: 2Temp C: 19.06 Temp F: 66.31:::
Dallas Temperature IC Control Library Demo
Locating devices...Found 3 devices.
Parasite power is: OFF
Found device 0 with address: 2828843B0400009F
Setting resolution to 12
Resolution actually set to: 12
Found device 1 with address: 28D8AA3B040000B7
Setting resolution to 12
Resolution actually set to: 12
Found device 2 with address: 28DC6754040000AF
Setting resolution to 12
Resolution actually set to: 12
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 18.62 Temp F: 65.52
Temperature for device: 1
Temp C: 19.00 Temp F: 66.20
Temperature for device: 2
Temp C: 19.06 Temp F: 66.31
:
:
:


抛砖引玉,供大家参考。。。



via - 极客工坊

标签: Arduino教程