Interfacing Arduino with DS18B20 Sensor and ST7789 TFT Display
- Unique 1-Wire® interface requires only one port pin for communication,
- Reduce component count with integrated temperature sensor and EEPROM,
- Measures temperatures from -55°C to +125°C (-67°F to +257°F),
- ±0.5°C Accuracy from -10°C to +85°C,
- Programmable resolution from 9 bits to 12 bits,
- No external components required,
- Parasitic power mode requires only 2 pins for operation (DQ and GND),
- Simplifies distributed temperature-sensing applications with multidrop capability,
- Each device has a unique 64-bit serial code stored in on-board ROM,
- Flexible user-definable nonvolatile (NV) alarm settings with alarm search command identifies devices with temperatures outside programmed limits,
- Available in 8-pin SO (150 mils), 8-pin μSOP, and 3-pin TO-92 packages.

- Arduino uno board
- ST7789 TFT display module (1.3″, 1.54″ …)
- DS18B20 temperature sensor —-> datasheet
- 4 x 3.3k ohm resistor (+1 if the display module has CS pin)
- 4 x 2.2k ohm resistor (+1 if the display module has CS pin)
- 4.7k ohm resistor
- Breadboard
- Jumper wires

1
2
|
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
|
1
2
3
4
|
// ST7789 TFT module connections
#define TFT_CS 10 // define chip select pin
#define TFT_DC 9 // define data/command pin
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
|
1
|
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
|
1
2
3
|
// initialize the ST7789 display (240x240 pixel)
// if the display has CS pin try with SPI_MODE0
tft.init(240, 240, SPI_MODE2);
|
1
2
|
// define DS18B20 data pin
#define DS18B20_PIN A0
|
1
|
int32_t f_temp = (int32_t)c_temp * 90/5 + 5120; // 5120 = 32 x 16 x 10
|
1
|
sprintf(c_buffer, " %02u.%04u", c_temp/16, (c_temp & 0x0F) * 625);
|
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/***********************************************************************
*
* Interfacing Arduino with DS18B20 digital temperature sensor and
* ST7789 TFT display (240x240 pixel).
* This is a free software with NO WARRANTY.
* http://simple-circuit.com/
*
***********************************************************************/
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
// ST7789 TFT module connections
#define TFT_CS 10 // define chip select pin
#define TFT_DC 9 // define data/command pin
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
// initialize Adafruit ST7789 TFT library with hardware SPI module
// MOSI(SDA) ---> Arduino digital pin 11
// SCK (SCL) ---> Arduino digital pin 13
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
// define DS18B20 data pin
#define DS18B20_PIN A0
void setup(void) {
// initialize the ST7789 display (240x240 pixel)
// if the display has CS pin try with SPI_MODE0
tft.init(240, 240, SPI_MODE2);
// if the screen is flipped, remove this command
tft.setRotation(2);
// fill the screen with black color
tft.fillScreen(ST77XX_BLACK);
tft.setTextWrap(false); // turn off text wrap option
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); // set text color to green and black background
tft.setTextSize(3); // text size = 3
tft.setCursor(17, 41); // move cursor to position (15, 27) pixel
tft.print("TEMPERATURE:");
tft.setTextSize(4); // text size = 4
}
// variables
int c_temp;
char c_buffer[9], f_buffer[9];
// main loop
void loop() {
delay(1000); // wait a second
// get temperature in °C ( actual temperature in °C = c_temp/16)
if( ds18b20_read(&c_temp) ) {
// read from DS18B20 sensor OK
// calculate temperature in °F (actual temperature in °F = f_temp/160)
// °F = °C x 9/5 + 32
int32_t f_temp = (int32_t)c_temp * 90/5 + 5120; // 5120 = 32 x 16 x 10
if(c_temp < 0) { // if temperature < 0 °C
c_temp = abs(c_temp); // absolute value
sprintf(c_buffer, "-%02u.%04u", c_temp/16, (c_temp & 0x0F) * 625);
}
else {
if (c_temp/16 >= 100) // if temperature >= 100.0 °C
sprintf(c_buffer, "%03u.%04u", c_temp/16, (c_temp & 0x0F) * 625);
else
sprintf(c_buffer, " %02u.%04u", c_temp/16, (c_temp & 0x0F) * 625);
}
if(f_temp < 0) { // if temperature < 0 °F
f_temp = abs(f_temp); // absolute value
sprintf(f_buffer, "-%02u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000));
}
else {
if (f_temp/160 >= 100) // if temperature >= 100.0 °F
sprintf(f_buffer, "%03u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000));
else
sprintf(f_buffer, " %02u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000));
}
tft.setTextColor(ST77XX_RED, ST77XX_BLACK); // set text color to red and black background
tft.setCursor(0, 102);
tft.print(c_buffer);
tft.drawCircle(201, 108, 4, ST77XX_RED); // print degree symbol ( ° )
tft.drawCircle(201, 108, 5, ST77XX_RED);
tft.setCursor(210, 102);
tft.print("C");
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK); // set text color to yellow and black background
tft.setCursor(0, 170);
tft.print(f_buffer);
tft.drawCircle(201, 176, 4, ST77XX_YELLOW); // print degree symbol ( ° )
tft.drawCircle(201, 176, 5, ST77XX_YELLOW);
tft.setCursor(210, 170);
tft.print("F");
}
else {
// read from DS18B20 sensor ERROR!
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); // set text color to red and black background
tft.setCursor(0, 102);
tft.print(" Error ");
tft.fillRect(0, 170, tft.width(), 28, ST77XX_BLACK);
}
}
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
// read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value = ds18b20_read_byte();
// read temperature MSB byte and store it on raw_temp_value MSB byte
*raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8);
return(1); // OK --> return 1
}
// end of code.
|
