Connect DS18B20 temperature sensor to micro:bit

UPDATE: it seems that something in MakeCode – or in the DF Robot extension outlined below – has possibly changed and it may no longer work. It also causes conflicts with an OLED display extension.

Try this new alpha extension instead. It allows multiple sensors to be connected, but make sure you have a DS18B20 connected BEFORE flashing any code made with it to your micro:bit or you’ll see a sad face! Just paste this into the URL box when adding an extension in MakeCode: https://github.com/bsiever/microbit-dstemp-alpha

Thanks to Bill Siever for this new extension, to Louis Wong and Tang at DF Robot for helping troubleshoot issues with extensions for this temperature sensor.

Also here are 3 HEX files I previously compiled that may work for you, flash them direct to your micro:bit, don’t load them in the editor. Right-click and save target as…


I had a DS18B20 1-wire Temperature Sensor that came with the excellent Sensors CamJam EduKit for Raspberry Pi – I wondered if you could use this with micro:bit to get more accurate external temperature readings. It turns out, thanks to my amazing colleagues Mark and Carlos, that you can!

The version of the DS18B20 Temperature Sensor I have is waterproof, so this means I can make an alarm to tell me when my coffee is getting cold (although I’d probably need to use pin 1 or pin 2 for the sensor instead of pin 0).

Here’s what you need:

  • micro:bit
  • DS18B20 temperature sensor
  • some way of connecting it, e.g. breadboard, jumper wires and crocodile clip leads
  • 4.7kΩ resistor
  • Online MakeCode editor and internet access

Connect the sensor to the micro:bit like this:

Then in the MakeCode editor, add this extension.

You do this by clicking on the cog wheel, going to extensions, and paste https://github.com/DFRobot/pxt-ds18b20 in the ‘Search or enter project URL’ box.

You should see you have some new blocks you can use, one to show the temperature as a number, another as a string:

This project will show a bar graph of the temperature, but if your micro:bit firmware is up-to-date and you’re using a recent version of Chrome, you can also use webUSB to log temperature data on a connected computer, plot it in real time on a graph and save for later analysis in a spreadsheet or other program.

Here are a couple of simpler projects, one that just shows the temperature as a rounded integer when you press button A:

And another that uses a truncated string to show the temperature to 1 decimal place:

This entry was posted in computers, microbit and tagged , . Bookmark the permalink.

8 Responses to Connect DS18B20 temperature sensor to micro:bit

  1. Bob Green says:

    Hi, Thanks for this It has got me started with temperature data logging. I don’t know if something has changed since you wrote this or my set up is different somehow but I couldn’t get the device console to show unless I added a serial write value block. Here’s the code: https://makecode.microbit.org/_TeP36WafkWTm

    • blogmywiki says:

      Hi Bob – that’s odd! The ‘plot bar graph’ block should be enough to send serial data to the MakeCode console. Occasionally I think I may have had to restart the program or re-pair the micro:bit to make the ‘show console device’ button appear. MakeCode has been updated since I made this video, so I’ll have to try it again and see if it still works.

  2. Bob Green says:

    The ‘show device console’ appears but no data is received. I wanted the numeric data saved rather than showing the plotting a graph on the microbit LED array which the ‘plot’ block does. Webusb in Chrome is still beta which might explain it.

    • blogmywiki says:

      You could try flashing the program again using webUSB and see it it works. Another approach would be, instead of using plot graph, to use the ‘serial write value’ block, e.g. ‘serial write value t = temp’ where ‘temp’ is the name of a variable containing the temperature reading. Let me know if that works – or log a ticket on https://support.microbit.org/support/home

  3. Morten Christensen says:

    Hi.

    Thanks for your nice tutorial.

    I have run into a problem, with temperatures lower than zero degrees (c) and higher than 60 deg (C).
    Do you know why this is, and maybe a way around it?

    Cheers,

    Morten.

  4. Louis says:

    When I tried to download the codes from MakeCode, it said “Compilation failed, please check your codes for errors”

    Is there anything wrong with what I did? Is the extension still currently available on MakeCode practically?

    Thanks.

    • blogmywiki says:

      Hi Louis – it seems the extension no longer works, try flashing one of the HEX files I’ve put at the top of the article direct to a micro:bit and see if it works. I hope DF Robot can update the extension.

Leave a Reply to Louis Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>