Arduino UNO R4 - Temperature Sensor - OLED
In this guide, we will learn how to program an Arduino UNO R4 to read the temperature from a DS18B20 sensor and show it on an OLED screen.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.
Overview of OLED and DS18B20 Temperature Sensor
Learn about OLED and the DS18B20 Temperature Sensor (including pinout, functionality, and programming) in these tutorials:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
We recommend buying a DS18B20 sensor with a wiring adapter for simple setup. The adapter includes a resistor, so you don't need an extra one for the wiring.
Arduino UNO R4 Code - Temperature from DS18B20 Temperature Sensor and display it on OLED
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the OLED and temperature sensor to the Arduino Uno R4 board according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Click on the Libraries icon on the left side of the Arduino IDE.
- Type “SSD1306” in the search box, then look for the SSD1306 library by Adafruit.
- Press the “Install” button to add the library.
- You will need to install some additional libraries
- Click the Install All button to install all the required libraries.
- Type "DallasTemperature" in the search box, and look for the DallasTemperature library by Miles Burton.
- Press the Install button to install the DallasTemperature library.
- You need to install the library dependency.
- Click the Install All button to install the OneWire library.
- Copy the code above and open it in Arduino IDE
- Press the Upload button in Arduino IDE to transfer the code to Arduino UNO R4
- Place the sensor in hot or cold water, or hold it in your hand
- Check the display on the OLED for results
※ NOTE THAT:
The code automatically centers the text horizontally and vertically on the OLED display.