Arduino Thermometer with DS18B20 sensor and NOKIA 5110 LCD
- Arduino board
- Nokia 5110 LCD screen
- DS18B20 temperature sensor —-> datasheet
- 5 x 3.3k ohm resistor
- 5 x 2.2k ohm resistor
- 4.7k ohm resistor
- Breadboard
- Jumper wires


1
2
3
|
#include <SPI.h> // include SPI library
#include <Adafruit_GFX.h> // include adafruit graphics library
#include <Adafruit_PCD8544.h> // include adafruit PCD8544 (Nokia 5110) library
|
1
2
|
// Nokia 5110 LCD module connections (CLK, DIN, D/C, CS, RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
|
1
2
|
// define DS18B20 data pin connection
#define DS18B20_PIN A0
|
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* Arduino Thermometer using DS18B20 sensor and Nokia 5110 LCD.
* This is a free software with NO WARRANTY.
* http://simple-circuit.com/
*/
#include <SPI.h> // include SPI library
#include <Adafruit_GFX.h> // include adafruit graphics library
#include <Adafruit_PCD8544.h> // include adafruit PCD8544 (Nokia 5110) library
// Nokia 5110 LCD module connections (CLK, DIN, D/C, CS, RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
// define DS18B20 data pin connection
#define DS18B20_PIN A0
void setup()
{
// initialize the display
display.begin();
// you can change the contrast around to adapt the display
// for the best viewing!
display.setContrast(50);
display.clearDisplay(); // clear the screen and buffer
display.display();
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
display.setCursor(15, 4);
display.print("ARDUINO +");
display.setCursor(0, 14);
display.print("DS18B20 SENSOR");
display.setCursor(7, 27);
display.print("TEMPERATURE:");
display.display();
}
// variable declarations
int raw_temp;
char *temp = "000.0000 C";
// main loop
void loop()
{
if(ds18b20_read(&raw_temp))
{
if(raw_temp & 0x8000)
{ // if the temperature is negative
temp[0] = '-'; // put minus sign (-)
raw_temp = abs(raw_temp); // absolute value
}
else
{
if((raw_temp >> 4) >= 100) // if the temperature >= 100 °C
temp[0] = '1'; // put 1 of hundreds
else // otherwise
temp[0] = ' '; // put space ' '
}
// put the first two digits (for ones and tens)
temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + '0'; // put tens digit
temp[2] = (raw_temp >> 4) % 10 + '0'; // put ones digit
// put the 4 fraction digits (digits after the decimal point)
// why 625: because we're working with 12-bit resolution (default resolution)
temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + '0'; // put tenths digit
temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + '0'; // put hundredths digit
temp[6] = (((raw_temp & 0x0F) * 625) / 10 ) % 10 + '0'; // put thousandths digit
temp[7] = ( (raw_temp & 0x0F) * 625) % 10 + '0'; // put ten-thousandths digit
display.setCursor(9, 37);
display.print(temp); // print temperature
display.drawRect(59, 37, 3, 3, BLACK); // print degree symbol ( ° )
display.display();
}
else
{
display.setCursor(9, 37);
display.print(" Error! ");
display.display();
}
delay(1000); // wait 1 second
}
bool ds18b20_start()
{
bool ret = 0;
digitalWrite(DS18B20_PIN, LOW); // send reset pulse to the DS18B20 sensor
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(500); // wait 500 us
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(100); // wait to read the DS18B20 sensor response
if (!digitalRead(DS18B20_PIN))
{
ret = 1; // DS18B20 sensor is present
delayMicroseconds(400); // wait 400 us
}
return(ret);
}
void ds18b20_write_bit(bool value)
{
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, value);
delayMicroseconds(80);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(2);
}
void ds18b20_write_byte(byte value)
{
byte i;
for(i = 0; i < 8; i++)
ds18b20_write_bit(bitRead(value, i));
}
bool ds18b20_read_bit(void)
{
bool value;
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(5);
value = digitalRead(DS18B20_PIN);
delayMicroseconds(100);
return value;
}
byte ds18b20_read_byte(void)
{
byte i, value;
for(i = 0; i < 8; i++)
bitWrite(value, i, ds18b20_read_bit());
return value;
}
bool ds18b20_read(int *raw_temp_value)
{
if (!ds18b20_start()) // send start pulse
return(0);
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0x44); // send start conversion command
while(ds18b20_read_byte() == 0); // wait for conversion complete
if (!ds18b20_start()) // send start pulse
return(0); // return 0 if error
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0xBE); // send read command
*raw_temp_value = ds18b20_read_byte(); // read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8); // read temperature MSB byte and store it on raw_temp_value MSB byte
return(1); // OK --> return 1
}
// end of code.
|
