|
本帖最后由 弘毅 于 2017-6-17 16:20 编辑
OneWare总线,意思就是一条数据线就可以工作,而且支持多个传感器,相关资料很多,大家可以到网上查阅。因为感兴趣做家庭环境监测,就了解了一下相关的资料。最简单的就是连接一个DS18B20,其实多个连接也很简单,可以使用寄生供电或者独立供电。
DS18B20的接法如附图所示,可以一个,也可以多个,两线或者三线都可以工作。多个传感器三线接法与单个类似,传感器直接GVS并联就可以,4.7K电阻只要接一个就可以。网友评论有时两线不稳定,所以如果不受到接线条件限制,最好还是三线。
连接完成,当然要读取数据,因为连接在一条总线上面,通过读取相应地址传感器,就能得到需要的参数,其实还是很简单。大家一般使用OneWare库,如果使用DallasTemperature库就更简单,下面是DallasTemperature自带的程序,贴在下面供大家参考。
- #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 found
- DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
- void setup(void)
- {
- // start serial port
- Serial.begin(9600);
- Serial.println("Dallas Temperature IC Control Library Demo");
- // Start up the library
- sensors.begin();
-
- // Grab a count of devices on the wire
- numberOfDevices = sensors.getDeviceCount();
-
- // locate devices on the bus
- Serial.print("Locating devices...");
-
- Serial.print("Found ");
- Serial.print(numberOfDevices, DEC);
- Serial.println(" devices.");
- // report parasite power requirements
- Serial.print("Parasite power is: ");
- if (sensors.isParasitePowerMode()) Serial.println("ON");
- else Serial.println("OFF");
-
- // Loop through each device, print out address
- for(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 device
- void 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 - faster
- float 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 bus
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures(); // Send the command to get temperatures
- Serial.println("DONE");
-
-
- // Loop through each device, print out temperature data
- for(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 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);
- }
- }
复制代码
我只增加了十秒延时,还有打印分割,总体没有变动。大家使用以前,检查一下数模输入接线端口是否正确即可。
程序运行以后,输出如下结果到串口。- 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
- :
- :
- :
复制代码
抛砖引玉,供大家参考。。。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|