Some readers wrote me asking more “real” examples using this display… in this post I’ll write a sketch to use it as a thermometer.
I chose the DS18B20 sensor from Maxim Integrated to read the room temperature.
The connection to Arduino is very simple: the sensor has 3 pins (VDD, GND e DQ) and I joined DQ to pin 2 of my Arduino:
This sensor provides 2 supply modes:
- using the VDD pin
- parasite, through the DQ pin (in this mode, VDD must be grounded)
In both modes, DQ pin requires a 4.7Kohm pull-up resistor.
Miles Burton developed a convenient library to talk with Maxim’s sensor… here’s how to use it:
- include in your sketch both OneWire and DallasTemperature libraries:
#include <OneWire.h> #include <DallasTemperature.h>
- init the library passing the pin you connected the sensor to:
#define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire);
- read the temperature value (float) – if you connected only a sensor, it will have index 0:
sensors.requestTemperatures(); float float_temp = sensors.getTempCByIndex(0);
Use the sprintf function to prepare the complete text to be displayed:
sprintf(display_string, "Temperature: %s", string_temp);
A little problem is that sprintf does not support float type; you have to convert it into a string variable.
The sketch is available on Github, here’s how it works: