Arduino MicroPython Temperature Sensor LCD

This guide shows you how to use an Arduino and MicroPython code to read the temperature from a DS18B20 sensor and display it on an LCD I2C display.

Arduino MicroPython Temperature Sensor LCD I2C

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×LCD I2C
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 Temperature Sensor and LCD

If you're new to DS18B20 temperature sensor, LCD I2C, or MicroPython programming for the Arduino, I recommend checking out these tutorials:

These tutorials will provide you with a comprehensive understanding of DS18B20 temperature sensor and LCD, how to connect these components to the Arduino, and how to effectively control their behavior using MicroPython code.

Wiring Diagram

The wiring diagram between Arduino MicroPython Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

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-lcd """ import machine import onewire import time from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C from DIYables_MicroPython_DS18X20 import DS18X20 ds_pin = machine.Pin('D10') # The Arduino Giga WiFi pin D10 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() if not sensor_addresses: raise Exception("No DS18B20 sensor found!") sensor_address = sensor_addresses[0] print('Arduino found DS18B20 sensor with address: ', sensor_address) # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Use the address found using the I2C scanner # Define the number of rows and columns on your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize I2C i2c = I2C(1) # Arduino Giga R1 WiFi I2C1 pins: SCL-D8, SDA-D9 # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Setup function lcd.backlight_on() lcd.clear() # Custom character for the degree symbol degree_char = chr(0xDF) # Main loop to read and print the temperature every second while True: DS18B20.convert_temp() time.sleep_ms(750) # waiting for temperature conversion temperature = DS18B20.read_temp(sensor_address) print('Temperature: {:.2f} °C'.format(temperature)) lcd.clear() lcd.set_cursor(0, 0) # Move to the beginning of the first row lcd.print("Temp: {:.2f}{}C".format(temperature, degree_char)) time.sleep(1)

※ NOTE THAT:

Depending on the manufacturer, the I2C address for the LCD can vary. In our example, we used address 0x27 as specified by the manufacturer, DIYables.

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.
  • Connect the Arduino board to the DS18B20 temperature sensor and LCD I2C according to the provided diagram.
  • 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-LCD-I2C”, then find the LCD I2C library created by DIYables.
  • Click on DIYables-MicroPython-LCD-I2C, then click Install button to install LCD I2C library.
Arduino MicroPython LCD I2C library
  • 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 Arduino 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.
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: 24.07 °C Temperature: 24.11 °C Temperature: 26.54 °C Temperature: 27.24 °C Temperature: 28.44 °C Temperature: 30.10 °C Temperature: 31.86 °C Temperature: 34.24 °C Temperature: 34.39 °C
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
  • Put the sensor on hot or cold water, or hold it in your hand.
  • Look at the display on the LCD screen. It looks like the below image:
Arduino Giga ds18b20 LCD

If the LCD screen is blank, please visit Troubleshooting for LCD I2C for help.

Code Explanation

Look at the comments in the source code for explanations of each line!

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!