Skip to content

Commit fd1bea9

Browse files
authored
Merge pull request milesburton#10 from milesburton/master
update to latest version
2 parents b1bbcb7 + 0f76ee9 commit fd1bea9

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

DallasTemperature.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ DallasTemperature::DallasTemperature()
4646
#if REQUIRESALARMS
4747
setAlarmHandler(NO_ALARM_HANDLER);
4848
#endif
49+
useExternalPullup = false;
4950
}
5051
DallasTemperature::DallasTemperature(OneWire* _oneWire)
5152
{
5253
setOneWire(_oneWire);
5354
#if REQUIRESALARMS
5455
setAlarmHandler(NO_ALARM_HANDLER);
5556
#endif
57+
useExternalPullup = false;
5658
}
5759

5860
bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
@@ -68,6 +70,21 @@ bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
6870
}
6971
}
7072

73+
/*
74+
* Constructs DallasTemperature with strong pull-up turned on. Strong pull-up is mandated in DS18B20 datasheet for parasitic
75+
* power (2 wires) setup. (https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf, p. 7, section 'Powering the DS18B20').
76+
*/
77+
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire){
78+
setPullupPin(_pullupPin);
79+
}
80+
81+
void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
82+
useExternalPullup = true;
83+
pullupPin = _pullupPin;
84+
pinMode(pullupPin, OUTPUT);
85+
deactivateExternalPullup();
86+
}
87+
7188
void DallasTemperature::setOneWire(OneWire* _oneWire) {
7289

7390
_wire = _oneWire;
@@ -207,9 +224,12 @@ void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
207224
_wire->write(COPYSCRATCH, parasite);
208225
delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
209226

210-
if (parasite)
227+
if (parasite) {
228+
activateExternalPullup();
211229
delay(10); // 10ms delay
212-
_wire->reset();
230+
deactivateExternalPullup();
231+
}
232+
_wire->reset();
213233

214234
}
215235

@@ -407,7 +427,9 @@ void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
407427
while (!isConversionComplete() && (millis() - delms < now))
408428
;
409429
} else {
430+
activateExternalPullup();
410431
delay(delms);
432+
deactivateExternalPullup();
411433
}
412434

413435
}
@@ -428,6 +450,16 @@ int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
428450

429451
}
430452

453+
void DallasTemperature::activateExternalPullup() {
454+
if(useExternalPullup)
455+
digitalWrite(pullupPin, LOW);
456+
}
457+
458+
void DallasTemperature::deactivateExternalPullup() {
459+
if(useExternalPullup)
460+
digitalWrite(pullupPin, HIGH);
461+
}
462+
431463
// sends command for one device to perform a temp conversion by index
432464
bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
433465

DallasTemperature.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ class DallasTemperature {
4444

4545
DallasTemperature();
4646
DallasTemperature(OneWire*);
47+
DallasTemperature(OneWire*, uint8_t);
4748

4849
void setOneWire(OneWire*);
4950

51+
void setPullupPin(uint8_t);
52+
5053
// initialise bus
5154
void begin(void);
5255

@@ -132,7 +135,7 @@ class DallasTemperature {
132135
// Is a conversion complete on the wire? Only applies to the first sensor on the wire.
133136
bool isConversionComplete(void);
134137

135-
int16_t millisToWaitForConversion(uint8_t);
138+
int16_t millisToWaitForConversion(uint8_t);
136139

137140
#if REQUIRESALARMS
138141

@@ -215,6 +218,10 @@ class DallasTemperature {
215218
// parasite power on or off
216219
bool parasite;
217220

221+
// external pullup
222+
bool useExternalPullup;
223+
uint8_t pullupPin;
224+
218225
// used to determine the delay amount needed to allow for the
219226
// temperature conversion to take place
220227
uint8_t bitResolution;
@@ -242,6 +249,10 @@ class DallasTemperature {
242249
// Returns true if all bytes of scratchPad are '\0'
243250
bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);
244251

252+
// External pullup control
253+
void activateExternalPullup(void);
254+
void deactivateExternalPullup(void);
255+
245256
#if REQUIRESALARMS
246257

247258
// required for alarmSearch

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line
1616
and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The
1717
centre pin is the data line '1-wire'.
1818

19+
In case of temperature conversion problems (result is `-85`), strong pull-up setup may be necessary. See section
20+
_Powering the DS18B20_ in
21+
[DS18B20 datasheet](https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf) (page 7)
22+
and use `DallasTemperature(OneWire*, uint8_t)` constructor.
23+
1924
We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you
2025
want to slim down the code feel free to use either of these by including
2126

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <OneWire.h>
2+
#include <DallasTemperature.h>
3+
4+
// Data wire is plugged into port 2 on the Arduino, while external pullup P-MOSFET gate into port 3
5+
#define ONE_WIRE_BUS 2
6+
#define ONE_WIRE_PULLUP 3
7+
8+
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9+
OneWire oneWire(ONE_WIRE_BUS);
10+
11+
// Pass our oneWire reference to Dallas Temperature.
12+
DallasTemperature sensors(&oneWire, ONE_WIRE_PULLUP);
13+
14+
void setup(void)
15+
{
16+
// start serial port
17+
Serial.begin(9600);
18+
Serial.println("Dallas Temperature IC Control Library Demo");
19+
20+
// Start up the library
21+
sensors.begin();
22+
}
23+
24+
void loop(void)
25+
{
26+
// call sensors.requestTemperatures() to issue a global temperature
27+
// request to all devices on the bus
28+
Serial.print("Requesting temperatures...");
29+
sensors.requestTemperatures(); // Send the command to get temperatures
30+
Serial.println("DONE");
31+
32+
for(int i=0;i<sensors.getDeviceCount();i++) {
33+
Serial.println("Temperature for Device "+String(i)+" is: " + String(sensors.getTempCByIndex(i)));
34+
}
35+
}

0 commit comments

Comments
 (0)