11238浏览
查看: 11238|回复: 3

[常见问题] 如何在arduino上扩展多个DS18B20温度传感器?

[复制链接]
本帖最后由 lauren 于 2014-6-30 17:03 编辑

问题如题,经过网上的一些调研,找到了一些结果,能够直接通过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 )***********


lauren  高级技师
 楼主|

发表于 2014-6-30 17:03:46

同时附上单总线设备地址扫描样例程序哈。

  1. // This sketch looks for 1-wire devices and
  2. // prints their addresses (serial number) to
  3. // the UART, in a format that is useful in Arduino sketches
  4. // Tutorial:
  5. // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
  6. #include <OneWire.h>
  7. OneWire  ds(3);  // Connect your 1-wire device to pin 3
  8. void setup(void) {
  9.   Serial.begin(9600);
  10.   discoverOneWireDevices();
  11. }
  12. void discoverOneWireDevices(void) {
  13.   byte i;
  14.   byte present = 0;
  15.   byte data[12];
  16.   byte addr[8];
  17.   
  18.   Serial.print("Looking for 1-Wire devices...\n\r");
  19.   while(ds.search(addr)) {
  20.     Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
  21.     for( i = 0; i < 8; i++) {
  22.       Serial.print("0x");
  23.       if (addr[i] < 16) {
  24.         Serial.print('0');
  25.       }
  26.       Serial.print(addr[i], HEX);
  27.       if (i < 7) {
  28.         Serial.print(", ");
  29.       }
  30.     }
  31.     if ( OneWire::crc8( addr, 7) != addr[7]) {
  32.         Serial.print("CRC is not valid!\n");
  33.         return;
  34.     }
  35.   }
  36.   Serial.print("\n\r\n\rThat's it.\r\n");
  37.   ds.reset_search();
  38.   return;
  39. }
  40. void loop(void) {
  41.   // nothing to see here
  42. }
复制代码
回复

使用道具 举报

Phoebe  高级技匠

发表于 2014-6-30 17:32:17

不明觉厉如何在arduino上扩展多个DS18B20温度传感器?图1
回复

使用道具 举报

Grey  中级技匠

发表于 2014-7-1 09:53:57


不明觉厉   
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail