In this tutorial, you will use an LCD1602 display to show the temperature.
(1) x Elegoo Uno R4 WIFI
(1) x OLED Dispaly Module
(1) x 10k ohm resistor
(1) x Thermistor
(1) x 400 tie-points Breadboard
(9) x M-M wires (Male to Male jumper wires)
Thermistor
A thermistor is a thermal resistor - a resistor that changes its resistance with temperature. Technically, all resistors are thermistors - their resistance changes slightly with temperature - but the change is usually very small and difficult to measure. Thermistors are made so that the resistance changes drastically with temperature. It can be 100 Ohms or more, per degree of change.
There are two kinds of thermistors, NTC (negative temperature coefficient) and PTC (positive temperature coefficient). In general, you will see NTC sensors used for temperature measurement. PTC's are often used as resettable fuses - an increase in temperature increases the resistance which means that as more current passes through them, they heat up and 'choke back' the current, quite handy for protecting circuits!
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and selected the Arduino UNO R4 WiFi board. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
Before you can run this, make sure that you have installed the <Adafruit_GFX.h> and <Adafruit_SSD1306.h> libraries via the Library Manager. Otherwise, your code won't work.
With this code, the Arduino UNO R4 WiFi reads the analog voltage from an NTC thermistor divider on pin A0 at 12-bit resolution, applies noise filtering, calculates temperature using the Beta-parameter equation, and displays it on an OLED screen. The temperature is updated every second.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Library Includes:
#include <Wire.h>: Built-in library for I2C communication, used to communicate with the OLED display.#include <Adafruit_GFX.h>: Adafruit Graphics Library, provides basic graphics drawing functions for the OLED display.#include <Adafruit_SSD1306.h>: Adafruit SSD1306 OLED driver library, used to control the OLED display.const int tempPin = A0; // ADC input pin for NTC thermistor (Arduino UNO R4 analog pin A0)
ADC Pin Definition: const int tempPin = A0;
Defines the ADC input pin for the NTC thermistor using the UNO R4 analog header pin A0.
// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
OLED Display Configuration:
#define SCREEN_WIDTH 128: Defines the OLED display width as 128 pixels.#define SCREEN_HEIGHT 64: Defines the OLED display height as 64 pixels.#define OLED_RESET -1: Defines the OLED reset pin, not used with UNO R4 so set to -1.Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);: Creates an OLED display object with the specified parameters.// Series resistor used in the voltage divider with the NTC thermistor
const float seriesResistor = 10000.0; // 10kΩ// NTC thermistor parameters (Standard 10k NTC: 25°C = 10kΩ, Beta = 3950)
const float B_COEFFICIENT = 3950.0;
const float NOMINAL_TEMP = 298.15; // 25°C in Kelvin (25 + 273.15)
const float NOMINAL_RES = 10000.0; // Nominal resistance at 25°C// Calibration offset
const float TEMP_OFFSET = -2.6;
Thermistor Parameters & Calibration:
const float seriesResistor = 10000.0;: Defines the value of the pull-down series resistor (10kΩ).const float B_COEFFICIENT = 3950.0;: Defines the Beta coefficient of the NTC sensor.const float NOMINAL_TEMP = 298.15;: Base temperature in Kelvin ().const float NOMINAL_RES = 10000.0;: Base resistance () at nominal temperature.const float TEMP_OFFSET = -2.6;: Calibrates sensor output against reference ambient temperature.void setup() {
Serial.begin(115200);// Set UNO R4 WiFi ADC to 12-bit resolution (0-4095 range)
analogReadResolution(12);// Initialize I2C
Wire.begin();// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (1);
}delay(1000);
display.clearDisplay();
display.setTextColor(WHITE);
}
Setup Function:
Serial.begin(115200);: Initializes serial communication at 115200 baud rate for debugging.analogReadResolution(12);: Configures the UNO R4 ADC resolution to 12-bit (0–4095) to match the conversion range.Wire.begin();: Initializes I2C communication.if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { ... }: Initializes OLED display with internal charge pump and address 0x3C.display.clearDisplay();: Clears previous contents on the screen.float calculateTemperature() {
// --- 1. Take multiple ADC samples for noise reduction ---
long tempReading = 0;
for (int i = 0; i < 10; i++) {
int val = analogRead(tempPin);// Constrain ADC reading to avoid extreme outliers (0 or 4095) tempReading += constrain(val, 10, 4085); delay(2);}
float avgReading = tempReading / 10.0; // Average value
Temperature Calculation Function (Part 1):
float calculateTemperature(): Function to sample the ADC, calculate resistance, and compute temperature.for (int i = 0; i < 10; i++) { ... }: Takes 10 consecutive ADC samples to reduce reading jitter.constrain(val, 10, 4085): Clips extreme saturation outliers.avgReading = tempReading / 10.0: Computes the smoothed mean reading.// --- 2. Convert ADC reading to thermistor resistance ---
// High-side divider setup: 5V -> NTC -> A0 -> 10k -> GND
float ratio = (4095.0 / avgReading) - 1.0;
if (ratio <= 0.001) {
return -1.0; // Error flag
}
float ntcResistance = seriesResistor * ratio;
Temperature Calculation Function (Part 2):
float ratio = (4095.0 / avgReading) - 1.0;: Calculates the voltage division ratio across the NTC.float ntcResistance = seriesResistor * ratio;: Calculates actual NTC resistance.if (ratio <= 0.001) { return -1.0; }: Safety check to prevent math errors when ADC readings hit upper rails.// --- 3. Apply the Beta-parameter equation ---
float tempK = 1.0 / ((1.0 / NOMINAL_TEMP) + (log(ntcResistance / NOMINAL_RES) / B_COEFFICIENT));float tempC = (tempK - 273.15) + TEMP_OFFSET; // Convert Kelvin -> Celsius and add offset
// Safety bounds check
if (tempC < -40.0 || tempC > 125.0) {
return -1.0;
}
return tempC;
}
Temperature Calculation Function (Part 3):
float tempK = 1.0 / ((1.0 / NOMINAL_TEMP) + (log(ntcResistance / NOMINAL_RES) / B_COEFFICIENT));: Calculates absolute temperature in Kelvin using the standard Beta formula.float tempC = (tempK - 273.15) + TEMP_OFFSET;: Converts Kelvin to Celsius and applies the calibration offset.if (tempC < -40.0 || tempC > 125.0): Checks if the output falls within normal physical boundaries.void loop() {
float tempC = calculateTemperature();display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 3);
display.print("Temp:");display.setCursor(20, 30);
// Display error message if the reading was invalid
if (tempC == -1.0) {
display.print("Err");
} else {
display.print(tempC, 1);
display.print(" C");
}display.display();
delay(1000);
}
Loop Function:
float tempC = calculateTemperature();: Calls the calculation function once per second.display.clearDisplay();: Clears previous frame.display.print("Temp:");: Labels output on OLED.display.print(tempC, 1); display.print(" C");: Renders the temperature value to 1 decimal place with unit symbol.display.display();: Writes frame buffer to the physical panel.delay(1000);: Pauses 1 second before the next sampling cycle.0x3C.A0 and both power lines (5V, GND) are securely attached.115200.