Arduino Nano 33 IoT - Temperature Sensor

This guide explains how to use the Arduino Nano 33 IoT to read temperature from the DS18B20 sensor and display it on the Serial Monitor.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor 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

DS18B20 Temperature Sensor Pinout

The DS18B20 sensor for measuring temperature has three connection points.

  • GND pin: Connect this pin to the ground (0V).
  • VCC pin: Connect this pin to the power supply (5V or 3.3V).
  • DATA pin: This is the 1-Wire data line. Connect it to any digital pin on the Arduino Nano 33 IoT.

The DS18B20 sensor comes in two types:

  • TO-92 case (it looks a bit like a transistor)
  • Waterproof sensor. We use this type in our guide.
DS18B20 temperature sensor Pinout

When you connect a DS18B20 temperature sensor to an Arduino, you usually need a pull-up resistor. This extra part can be tricky to handle. However, some companies have made it easier by offering a wiring adapter that has a built-in pull-up resistor and a screw terminal block for a simple connection.

Wiring Diagram between DS18B20 Temperature Sensor and Arduino Nano 33 IoT

  • Using a breadboard
The wiring diagram between Arduino Nano and 33 IoT Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring picture with adapter (we recommend this option)
The wiring diagram between Arduino Nano and 33 IoT DS18B20

This image is created using Fritzing. Click to enlarge image

To make connecting easier, it's a good idea to buy the DS18B20 sensor with a wiring adapter that already includes a resistor. This way, you don't have to add another resistor when you set it up.

Arduino Nano 33 IoT Code

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

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Open the Library Manager by clicking the Library Manager icon on the left side of the Arduino IDE.
  • Type DallasTemperature into the search box and find the DallasTemperature library by Miles Burton.
  • Click the Install button to add the DallasTemperature library.
Arduino Nano 33 IoT Dallas Temperature library
  • You might be asked to add a required part. Click the Install All button to add the OneWire library.
Arduino Nano 33 IoT onewire library
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile the code and send it to the Arduino Nano 33 IoT board.
  • Change the sensor's temperature by either holding the DS18B20 temperature sensor in your hand or putting it into hot or cold water.
  • Open the Serial Monitor to see the result. It will look like the example below.
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!