One Wire Digital Temperature Sensor – DS18B20

נקנה ב Sparkfun.
טווח: (55C to 125C (+/-0.5C-
הסבר טכני:
החיישן מתקשר באופן דיגיטלי עם מיקרוקונטרולר (ארדואינו).
לפי הוראה מהארדואינו, החיישן מודד את הטמפרטורה ומעביר את הנתון.
יכולות נוספות:
- חיבור של יותר מסנסור אחד לארדואינו.
- “תכנות” הסנסור, כך שימסור לארדואינו הודעה רק אם הטמפרטורה חורגת מגבולות מסויימים.
בעזרת התוכנה המצורפת, ההפעלה והחיבור פשוטים מאד.
מעגל:
רכיבים – נגד 4.7 קילואוהם
חיבורים (כאשר מסתכלים מול הצלע השטוחה):
רגל ימין – +5V
רגל אמצעית – לפין דיגיטלי של ארדואינו
רגל שמאל – Gnd
הנגד – מתחבר לרגל האמצעית ולימנית.
תוכנה:
התוכנה מורכבת אך פשוטה מאד לשימוש – ערך הטמפרטורה נקרא ע”י קריאה לפונקציה HBreadTemp.
/*
Reading temperature using the DS18B20
This code is for reading only a single device connected individualy to a digital pin.
Note that every reading takes almost a full second. If you need faster readings, refer to the datasheet to learn how to configure the device for lower resolution.
*/
#define HBpin 3
#define CONVERT_T 0×44
#define SKIP_ROM 0Xcc
#define READ_SCRATCHPAD 0xbe
#define T_LSB 0
#define T_MSB 1
#define CONFIG_REG 4
void HBreset() {
digitalWrite (HBpin, LOW);
delayMicroseconds (480);
digitalWrite (HBpin, HIGH);
}
boolean HBinitialization() {
HBreset();
pinMode (HBpin, INPUT);
delayMicroseconds (60);
boolean ok = !digitalRead (HBpin);
delayMicroseconds (420);
pinMode (HBpin, OUTPUT);
return ok;
}
void HBwriteBit (boolean bit) {
digitalWrite (HBpin, LOW);
delayMicroseconds (1);
if (bit) {
digitalWrite (HBpin, HIGH);
delayMicroseconds (60);
} else {
delayMicroseconds (60);
digitalWrite (HBpin, HIGH);
}
}
void HBtransmit (byte data) {
for (byte b=0; b<8; b++) {
HBwriteBit (data & 0×01);
data = data >> 1;
}
}
byte HBreadBit() {
digitalWrite (HBpin, LOW);
delayMicroseconds (1);
pinMode (HBpin, INPUT);
delayMicroseconds (6);
byte d = digitalRead (HBpin);
delayMicroseconds (60);
pinMode (HBpin, OUTPUT);
digitalWrite (HBpin, HIGH);
delayMicroseconds (1);
return d;
}
byte HBrecieveByte() {
byte data=0;
for (byte b=0; b<8; b++) {
data += HBreadBit() << b;
}
return data;
}
boolean HBsendCommand(byte data) {
if (!HBinitialization()) {
return false;
}
HBtransmit (SKIP_ROM);
HBtransmit (data);
return true;
}
float HBreadTemp() {
byte lsb, msb;
HBsendCommand (CONVERT_T);
pinMode (HBpin, INPUT);
while (digitalRead(HBpin) == LOW) {}
pinMode (HBpin, OUTPUT);
if (!HBsendCommand (READ_SCRATCHPAD)) {
Serial.println(“Error!”);
} else {
lsb = HBrecieveByte();
msb = HBrecieveByte();
int temp = lsb + msb*256;
return float(temp) / 16;
}
}
void setup() {
pinMode (HBpin, OUTPUT);
digitalWrite (HBpin, HIGH);
Serial.begin (9600);
}
void loop() {
// Read the temperature and store it in tmp:
float tmp = HBreadTemp();
// All the following is a method to send a floating point fraction to be presented on the pc:
int frac = abs((tmp – float(int(tmp))) * 1000);
Serial.print (int(tmp));
Serial.print (‘.’);
Serial.println (frac);
delay(2000);
}

