如何在arduino上扩展多个DS18B20温度传感器?

2014-06-301.4万
AI 快速预览详细 收起
本文介绍了如何在Arduino上扩展多个DS18B20温度传感器。通过共享一个上拉电阻,并使用单总线地址扫描和更新的方法,可以轻松驱动多个温度传感器。样例程序展示了如何配置和读取每个传感器的温度数据。这种方法适用于需要多点温度监测的项目,节省了硬件端口资源。


问题如题,经过网上的一些调研,找到了一些结果,能够直接通过Arduino驱动多个18B20温度探头。硬件连接就是多个探头共享使用同一个上拉电阻,并且通过一些样例程序扫描多个探头的单总线地址,更新到样例软件里面即可驱动。

单总线是个相当暴力的东西,硬件端口资源占用少,而18B20经过一些伙伴的帖子评测,也证实是温度传感器中的战斗机。样例程序如下,和大家分享下哈。

关于单总线设备地址的扫描程序,如样例程序的备注,在:http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html 可以找到哈。

样例中包含了LCD显示的程序,所以使用时可以忽略或者在开发自己程序的时候删掉哈。
/* YourDuino.com Example: Multiple DS18B20 Temperature Sensors
   Displayed on 4x20 character LCD display
   
   DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 10):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V
   
   terry@yourduino.com */

/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page ... ure_Control_Library
#include <DallasTemperature.h>
// Wire (I2C) Library
#include <Wire.h>
// LCD Library
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
//Download: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move original LiquidCrystal library elsewhere, copy this in it's place


/*-----( Declare Constants and Pin Numbers )-----*/
// Data wire is plugged into port 10 on the Arduino (can be changed)
#define ONE_WIRE_BUS 10    // NOTE: No ";" on #define  

#define I2C_ADDR    0x20  // Define I2C Address for the PCF8574A
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays
#define BACKLIGHT_PIN  7
#define En_pin  4
#define Rw_pin  5
#define Rs_pin  6
#define D4_pin  0
#define D5_pin  1
#define D6_pin  2
#define D7_pin  3

#define  LED_OFF  0
#define  LED_ON  1

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass address of our oneWire instance to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Start the LCD display library
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutor ... address-finder.html

// WP 1
DeviceAddress Probe01 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 }; // "4"
DeviceAddress Probe02 = { 0x28, 0xE1, 0xC7, 0x40, 0x04, 0x00, 0x00, 0x0D }; // "5"
DeviceAddress Probe03 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 }; // "4" Again for test
DeviceAddress Probe04 = { 0x28, 0x10, 0xA4, 0x57, 0x04, 0x00, 0x00, 0xA9 };


void setup()   /****** SETUP: RUNS ONCE ******/
{
//------- Initialize the Temperature measurement library--------------
  sensors.begin();
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);

//---------------- Initialize the lcd ------------------  
  lcd.begin (20,4);  // 20 characters, 4 lines
// Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
  lcd.setBacklight(LED_ON);  

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  sensors.requestTemperatures(); // Send the command to get temperatures

  lcd.clear();  // Reset the display  
  lcd.home();
  lcd.backlight();  //Backlight ON if under program control  
  
// Print our characters on the LCD
// NOTE: Line number and character number start at 0 not 1
  
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("1: ");
  displayTemperature(Probe01);  
  
  lcd.setCursor(0,1); //Start at character 0 on line 1
  lcd.print("2: ");
  displayTemperature(Probe02);  
  
  lcd.setCursor(0,2); //Start at character 0 on line 2
  lcd.print("3: ");
  displayTemperature(Probe03);  
  
  lcd.setCursor(0,3); //Start at character 0 on line 3
  lcd.print("4: ");
  displayTemperature(Probe04);  
  
  delay(2000);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void displayTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00) // Measurement failed or no device found
   {
    lcd.print("Temperature Error");
   }
   else
   {
   lcd.print("C=");
   lcd.print(tempC);
   lcd.print(" F=");
   lcd.print(DallasTemperature::toFahrenheit(tempC)); // Convert to F
   }
}// End printTemperature

//*********( THE END )***********


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(3)
Grey
Grey

不明觉厉
phoebe.wang40
phoebe.wang40
不明觉厉

lauren
lauren
作者
同时附上单总线设备地址扫描样例程序哈。
[code]// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
#include
OneWire ds(3); // Connect your 1-wire device to pin 3
void setup(void) {
Serial.begin(9600);
discoverOneWireDevices();
}
void discoverOneWireDevices(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
Serial.print("Looking for 1-Wire devices...\n\r");
while(ds.search(addr)) {
Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
}
Serial.print("\n\r\n\rThat's it.\r\n");
ds.reset_search();
return;
}
void loop(void) {
// nothing to see here
}
[/code]
- 没有更多了 -