Categories

Display temperature on I2C OLED display

Taking our hello world I2C OLED example a bit further, this is our next creation. We connect a DS18B20 temperature sensor and display the temperature on the display

Coding

You need to download and install various libraries and copy them into your Arduino libraries folder.

Dallas 18b20 Temperature Library
“1-Wire” Library

Multi LCD library

 


#include "Arduino.h"
#include "Wire.h"
#include "OneWire.h"
#include "MicroLCD.h"
#include "DallasTemperature.h"

#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

//LCD_SH1106 lcd; /* for SH1106 OLED module */
LCD_SSD1306 lcd; /* for SSD1306 OLED module */

void setup()
{
lcd.begin();
sensors.begin();
}

void loop()
{

sensors.requestTemperatures();
lcd.clear();
lcd.setFontSize(FONT_SIZE_MEDIUM);
lcd.print(sensors.getTempCByIndex(0));
lcd.print(" celsius");
delay(1000);
}

 

Links

Share

DS18B20 and LCD display

This DS18B20 example we output the temperature to our LCD display. The LCD display we use is the LCD Keypad Shield that is available for sale from various sources.

Here is the temperature displayed on the LCD, as you can see its a shield that can be fitted to your Arduino Uno. Links at the bottom of the article

lcd temperature display

lcd temperature display

Code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

//DS18b20 connected to D13
#define DS18B20 13
// Connections: Sainsmart LCD/Keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
//Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
//setup the LCD
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print(“TEMPERATURE”);
}

void loop()
{
//read temperature and output via LCD
sensors.requestTemperatures();
lcd.setCursor(0,1);
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(6,1);
lcd.print(“celsius”);
}

Links

 

LCD and Keypad Shield on Amazon US

LCD and keypad shield on Amazon UK

Share

DS18B20 basic warning example

In a previous example we showed you a basic usage of a DS18B20 which outputted the temperature to the Serial output.

A simple addition to the code can show a practical example, in this case if the temperature exceeds a certain value then a message is displayed. In real life this could be an alarm of some direction.

 

Code

#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
//this sets the max temp
int maxTemp = 30;
//read temperature and output via serial
sensors.requestTemperatures();
//if temperature exceeds maxTemp value
if(sensors.getTempCByIndex(0)>maxTemp)
{
Serial.println("DANGER");
}
else
{
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" degrees C");
}
}

Share

DS18B20 temperature sensor

The DS18B20 is a  temperature sensor that can be used in various simple projects.  This part uses the 1 wire (I2C) bus and you can connect multiple sensors up to your Arduino.

The part is also relatively low cost and only requires an additional 4k7 pull up resistor. In the example below we shall make a basic example that reads the temperature and outputs via serial and can be verified using the serial monitor in the Arduino IDE.

Lets look at the parts list

Parts List

Label Part Type
DS1 DS18B20 1-Wire Temperature Sensor
Part1 Arduino Uno (Rev3)
R1 4.7k Ω Resistor

Layout

As always be careful not to get the connections incorrect, you can refer to the pinout for the device below to help . The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to 0V and the VDD pin to +5V from the Arduino Duo.

ds18b20 pinout

ds18b20 pinout

 

Here is the connection diagram showing how to connect your arduino to the resistor and sensor.

ds1820 breadboard

ds1820 breadboard

Coding

You need to download 2 libraries and copy them into your Arduino libraries folder.

Dallas 18b20 Temperature Library
“1-Wire” Library

Now copy and paste this into a new sketch

#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
//read temperature and output via serial
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” degrees C”);
}

When you upload the sketch, go to Tools -> Serial Monitor and you should see something like the following. Put your finger  on the DS18B20 and you should see the temperature vary.

 

ds18b20 output

ds18b20 output

Links

DS18B20 links

Buy DS18B20 from Amazon UK

Buy DS18B20 from Amazon US

Share