Arduino and DS18B20 1-Wire Temperature Sensor
Arduino and DS18B20 1-Wire Temperature Sensor
Arduino and DS18B20 1-Wire Temperature Sensor
blog
Hans-Petter Halvorsen
Contents
• Introduction to Arduino
• DS18B20 Temperature Sensor
• Arduino Examples
– Read Temperature Data from
DS18B20 Sensor
– Write Temperature Data to
ThingSpeak
https://www.halvorsen.blog
Arduino
Hans-Petter Halvorsen Table of Contents
Arduino
• Arduino is an open-source electronics platform
based on easy-to-use hardware and software.
• It's intended for anyone making interactive projects,
from kids to grown-ups.
• You can connect different Sensors, like Temperature,
etc.
• It is used a lots in Internet of Things projects
• Homepage:
https://www.arduino.cc
Arduino
• Arduino is a Microcontroller
• Arduino is an open-source platform
with Input/Output Pins (Digital
In/Out, Analog In and PWM)
• Price about $20
• Arduino Starter Kit ~$40-80
with Cables, Wires, Resistors, Sensors, etc.
Arduino UNO Digital ports (2-13)
6
Reset button
3
USB for PC
2
connection
5V, GND 4 5
Arduino UNO WiFi Rev 2
The Arduino Uno
WiFi is functionally
the same as the
Arduino Uno Rev3,
but with the
addition of WiFi /
Bluetooth and
some other
enhancements.
Arduino Software
Upload Code to Arduino Board Save
DS18B20
1-Wire Temperature Sensor
2c
m
DS18B20 1-Wire Temperature Sensor
• DS18B20 is a 1-Wire Digital Temperature Sensor, this means
DS18B20 the Sensor only need 1 Pin for Communication (+ one pin for
GND and one pin for 5V)
1 2 3
• Accuracy +/−𝟎. 𝟓℃
• 9 to 12-bit resolution (Programmable)
• Temperature range −𝟓𝟓°𝐂 to +𝟏𝟐𝟓°𝐂
• Price: About $4
• Datasheet:
GND Data 5V https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/
https://www.halvorsen.blog
Arduino Examples
Hans-Petter Halvorsen Table of Contents
https://www.halvorsen.blog
Read
Temperature Data
Hans-Petter Halvorsen Table of Contents
Equipment
Breadboard
Arduino
DS18B20
Wires
Resistor 𝑅 = 4.7kΩ
Wiring
Be careful to get the DS18B20 the right way around. The curved
edge should be to placed as shown in the figure below. If you
put it the wrong way around, it will get hot and then break.
DI2 DS18B20
Data
GND
5V
𝑅 = 4.7𝑘Ω
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempCelcius=0;
void setup(void)
{
Serial.begin(9600);
sensors.begin();
}
void loop(void)
{
sensors.requestTemperatures();
tempCelcius = sensors.getTempCByIndex(0);
Serial.print("T = ");
Serial.print(tempCelcius);
Serial.println("ºC");
delay(1000);
}
Serial Monitor
https://www.halvorsen.blog
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
Here you see the main code structure: DallasTemperature
sensors(&oneWire);
float tempCelcius=0;
WiFiClient client;
We have created separate Functions for: int wait = 20000;
• ThingSpeakWrite() }
ThingSpeak.begin(client);
void loop(void)
The Functions are presented on the next pages. {
ConnectWiFi();
ReadSensorData();
ThingSpeakWrite();
delay(wait);
}
Arduino Code
void CheckWiFi()
{
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
}
void ConnectWiFi()
Secrets.h {
char ssid[] = SECRET_SSID;
#define SECRET_SSID "xxxxxx" char pass[] = SECRET_PASS;
#define SECRET_PASS "xxxxxx" if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
#define SECRET_CH_ID xxxxxx Serial.println(SECRET_SSID);
#define SECRET_WRITE_APIKEY "xxxxxx" while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
}
Arduino Code
void ReadSensorData()
{
sensors.requestTemperatures();
tempCelcius = sensors.getTempCByIndex(0);
Serial.print("T = ");
Serial.print(tempCelcius,1);
Serial.println("ºC");
}
Arduino Code Secrets.h
#define SECRET_SSID "xxxxxx"
#define SECRET_PASS "xxxxxx"
void ThingSpeakWrite()
{
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
int channelField = 1;
E-mail: [email protected]
Web: https://www.halvorsen.blog