Arduino MicroPython Temperature Sensor
This guide shows you how to use the DS18B20 1-wire temperature sensor with Arduino and MicroPython. You will learn:
- How to connect the DS18B20 temperature sensor to an Arduino
- How to connect the DS18B20 temperature sensor to an Arduino using an adapter
- How to write MicroPython code for Arduino to read the temperature from the DS18B20 sensor
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.
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.
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.
This image is created using Fritzing. Click to enlarge image
- How to connect Arduino and DS18B20 temperature sensor using adapter.
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
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.
- 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.