ESP8266 & Temperature Sensor DS18B20 and Onewire library

There are a variety of sensors to measure temperature in devices such as Arduinoand ESP8266, in this case we have chosen the Sensor DS18B20this particular sensor is not connected through a GPIO Analogue, technically it is a digital sensor that uses the protocol Onewire or 1- Wire designed by dallas semiconductor, There are many tutorials on the net about this protocol.

In this case we have adapted our temperature sensor for fast connections, the sensor, the datasheet of DS18B20Maxim recommends using a resistance of 4.7k, but we will use a resistance of 3.3k, but for ease we will use 3 resistors of 10k in parallel, This modification will allow your ESP8266 modules to work with this sensor without difficulty, with the help of heat shrinkable resistors and wiring.

Required Materials

Test

We will use a module ESP8266 12E NodeMCU and the Onewire library, in this case, step by step the methods of sending and requesting sensor data.

Connections, GND, 5V, GPIO(2) PIN D4

Arduino IDE Code

In this case the code indicates step by step the process of requesting data to the probe, this example can easily be modified for other implementations with onewire, I decided to use this example to understand step by step how the Onewire protocol works.

Requires Libreria Onewire.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

#include < OneWire.h >

OneWire ds(2);

voidsetup(){

// put your setup code here, to run once:

Serial.begin(115200);

}

voidtemperature(){

bytei;

bytepresent=0;

bytetype_s;

bytedata[12];

byteaddr[8];

floatcelsius,fahrenheit;

if(!ds.search(addr)){

/// Serial.println("No more addresses.");

/// Serial.println();

ds.reset_search();

delay(250);

return;

}

if(ds.search(addr)){

Serial.println("fallo");

}

for(i=0;i<8;i++){

addr[i];

}

if(OneWire::crc8(addr,7)!=addr[7]){

Serial.println("CRC is not valid!");

return;

}

// the first ROM byte indicates which chip

switch(addr[0]){

case0x10:

type_s=1;

break;

case0x28:

type_s=0;

break;

case0x22:

// Serial.println(" Chip = DS1822");

type_s=0;

break;

default:

Serial.println("Device is not a DS18x20 family device.");

return;

}

ds.reset();

ds.select(addr);

ds.write(0x44,1);

delay(1000);

present=ds.reset();

ds.select(addr);

ds.write(0xBE);

//Serial.println("Paso 7");

for(i=0;i&lt;9;i++){

data[i]=ds.read();

}

OneWire::crc8(data,8);

int16_t raw=(data[1]&lt;&lt;8)|data[0];

if(type_s){

raw=raw&lt;&lt;3;

if(data[7]==0x10){

raw=(raw&amp;0xFFF0)+12-data[6];}

}else{

bytecfg=(data[4]&amp;0x60);

if(cfg==0x00)raw=raw&amp;~7;// 9 bit resolution, 93.75 ms

elseif(cfg==0x20)raw=raw&amp;~3;// 10 bit res, 187.5 ms

elseif(cfg==0x40)raw=raw&amp;~1;// 11 bit res, 375 ms

}

floatcelsius_web;

celsius=(float)raw/16.0;

if(isnan(celsius)){

Serial.println("isnan");

}

Serial.print("Temperature: ");

Serial.print(celsius);

}

voidloop(){

// put your main code here, to run repeatedly:

temperature();

delay(1000);

}

Conclusions

Although it is considered that we simply test the sensor, this tutorial invites to adapt the Sensor and the resistance to facilitate connection in tests, this sensor will be used in a lot of future tests.

A great advantage of the use of these sensors, since being digital and using a protocol, allows to create a network of sensors to a single input (GPIO).