Arduino MicroPython Temperature Sensor

This guide shows you how to use the DS18B20 1-wire temperature sensor with Arduino and MicroPython. You will learn:

Arduino MicroPython temperature sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

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

Pinout

The DS18B20 temperature sensor has three pins:

  • GND pin: Connect to GND (0 volts)
  • VCC pin: Connect to VCC (5 volts or 3.3 volts)
  • DATA pin: Used for 1-Wire Data; connect to a digital pin on the Arduino

There are two main types of DS18B20 sensors: the TO-92 package, which looks like a transistor, and the waterproof probe. This guide will focus on using the waterproof probe.

DS18B20 temperature sensor Pinout

Connecting a DS18B20 temperature sensor to an Arduino usually needs a pull-up resistor, which can be tricky to set up. Luckily, some manufacturers provide wiring adapters with a pull-up resistor and a screw terminal block, making the setup easier.

Wiring Diagram

  • How to connect Arduino and DS18B20 temperature sensor using breadboard.
The wiring diagram between Arduino MicroPython Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • How to connect Arduino and DS18B20 temperature sensor using adapter.
The wiring diagram between Arduino MicroPython DS18B20

This image is created using Fritzing. Click to enlarge image

We recommend getting a DS18B20 sensor with a wiring adapter. The adapter makes it easy to connect the sensor since it already includes a resistor, so you don’t need to add one yourself.

Arduino MicroPython Code

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-temperature-sensor """ import machine import onewire import time from machine import I2C, Pin from DIYables_MicroPython_DS18X20 import DS18X20 ds_pin = machine.Pin('D2') # The Arduino Giga WiFi pin D2 connected to the DS18B20 sensorsensor # Create the onewire object OneWire = onewire.OneWire(ds_pin) # Create the DS18X20 object DS18B20 = DS18X20(OneWire) # Scan for devices on the bus sensor_addresses = DS18B20.scan() print('Arduino found DS18B20 devices:', sensor_addresses) # Main loop to read and print the temperature every second while True: DS18B20.convert_temp() time.sleep_ms(750) # waiting for temperature conversion for address in sensor_addresses: temperature = DS18B20.read_temp(address) if temperature is not None: print('Temperature: {:.2f} °C'.format(temperature)) else: print('Error: Failed to read temperature (CRC mismatch)') time.sleep(1)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Follow the given diagram to attach the DS18B20 1-wire temperature sensor to the Arduino.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-DS18X20”, then find the DS18X20 library created by DIYables.
  • Click on DIYables-MicroPython-DS18X20, then click Install button to install DS18X20 library.
Arduino MicroPython DS18X20 temperature library
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Put the sensor in hot and cold water, or hold it in your hand.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Found DS18B20 sensor with address: bytearray(b'(\xffP\x05.\x04\x00\x07') Temperature: 21.00 °C Temperature: 21.31 °C Temperature: 23.44 °C Temperature: 24.63 °C Temperature: 25.50 °C Temperature: 26.06 °C Temperature: 26.56 °C Temperature: 27.06 °C Temperature: 28.25 °C
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!