分享

Arduino one wire temperature DS18B20

 开启美好每一天 2014-01-14

  The one wire Digital Temperature Sensor - DS18B20 from Maxim (formerly Dallas) is a great chip for measuring temperature in your projects. Luckily, there is a Dallas Temperature library for the arduino which makes using this sensor very easy.

The most recent version of this library can be found at
http://www./?title=Dallas_Temperature_Control_Library

We used version 3.6.0 for this tutorial which can also be downloaded here

Download a copy of the library. In it you will find two folders

Dallas Temperature Library

Drag and drop the DallasTemperture folder into your arduino/libraries folder

If you dont already have the OneWire library then you will need to copy that folder into the libraries folder also.

Now, before we get to the programming part, lets wire up our temperature sensor. The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to 0V and the VDD pin to +5V from the Arduino. However, the DS18B20 can also extract its power from the data line which means we only effectively need two wires to connect it up. This makes it great for use as an external sensor.

DS18B20 pinout

So we will use the 2 wire method. Simply connect both the GND and VDD pins to 0V (yes both). Then connect the DQ pin to pin 2 on the Arduino board (can be any Arduino digital pin)

A 4K7 ohm pullup resistor is required on the DQ pin to pull it up to 5V

Here is our sensor setup on a breadboard

DS18B20 on breadboard

Here's a closeup. The brown wire connects pins 1 and 3 together, and the black wire connects them both to 0V. The orange wire is our signal wire to pin 2 on the Arduino, and there is a 4K7 pullup resistor to 5V

DS18B20 breadboard closeup

Next, load up the sketch below and upload it to your Arduino board. Using the Dallas Temperature Library makes getting the temperature easy.

 

#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 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);
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}
 
 
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");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
}


When you have uploaded the sketch, click on the Serial Monitor and you should see output like the following. The temperature is in degrees C

Dallas Temperature IC Control Library Demo
 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...DONE
Temperature for Device 1 is: 24.37 Requesting temperatures...


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多