Arduino Mega - Temperature Sensor

This guide shows you how to use the DS18B20 1-Wire temperature sensor with the Arduino Mega board. We will learn in detail:

Arduino Mega temperature sensor

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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 One Wire Temperature Sensor DS18B20

Pinout

The DS18B20 temperature sensor has three pins.

  • GND pin: connect to ground (0 V).
  • VCC pin: connect to VCC (5 V or 3.3 V).
  • DATA pin: this is the 1-Wire data line. Connect it to a digital pin on the Arduino Mega.

The sensor comes in two kinds: a TO-92 package that looks like a transistor, and a waterproof probe. In this tutorial, we will use the waterproof probe.

DS18B20 temperature sensor Pinout

To connect a DS18B20 temperature sensor to an Arduino Mega, you need a pull-up resistor, and this can be tricky. Some manufacturers offer a wiring adapter that already includes a pull-up resistor and a screw terminal block, which makes the setup easier.

Wiring Diagram

  • How to wire a breadboard
The wiring diagram between Arduino Mega Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • Diagram showing how wires connect and the adapter
The wiring diagram between Arduino Mega DS18B20

This image is created using Fritzing. Click to enlarge image

We suggest getting a DS18B20 temperature sensor with a wiring adapter. This adapter is easy to connect because it has a built-in resistor, so you don’t need to add another one.

How To Program For DS18B20 Temperature Sensor

  • Add the library:
#include <OneWire.h> #include <DallasTemperature.h>
  • Make a OneWire object and a DallasTemperature object for the pin that connects to the sensor's DATA pin.
OneWire oneWire(SENSOR_PIN); // Set up a OneWire bus on the sensor pin DallasTemperature DS18B20(&oneWire); // Link DallasTemperature to the OneWire bus
  • Install the sensor
DS18B20.begin(); // Initialize DS18B20 temperature sensor
  • Run the command to check the temperatures:
DS18B20.requestTemperatures();
  • Find out what the temperature is in Celsius.
temperature_C = DS18B20.getTempCByIndex(0);
  • (Optional) Convert Celsius temperature to Fahrenheit.
temperature_F = temperature_C * 9 / 5 + 32;

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 4 // The Arduino UNO R4 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library void setup() { Serial.begin(9600); // initialize serial DS18B20.begin(); // initialize the sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius float temperature_F = temperature_C * 9 / 5 + 32; // convert Celsius to Fahrenheit Serial.print("Temperature: "); Serial.print(temperature_C); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(temperature_F); // print the temperature in Fahrenheit Serial.println("°F"); delay(500); }

Detailed Instructions

Follow these instructions one step at a time.

  • Connect the DS18B20 1-wire temperature sensor to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose Arduino Mega as your board and the correct COM port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Search for DallasTemperature and select the library by Miles Burton.
  • Click the Install button to add the DallasTemperature library.
Arduino Mega Dallas Temperature library
  • You need to install a library the program needs.
  • Click the Install All button to install the OneWire library.
Arduino Mega onewire library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Put the sensor in hot water and in cold water, or just hold it in your hand.
  • See the results on the Serial Monitor.
COM6
Send
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!