Connect DS18B20 temperature sensor to micro:bit

UPDATE March 2021: Bill Siever’s Make Code extension has been updated to support both V1 and V2 of the micro:bit and is now an approved extension. Just search for ‘dstemp’ in the extension search (or use the new URL: https://github.com/bsiever/microbit-dstemp).


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.

41 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.

  5. Bill Siever says:

    I’ve made some improvements to the extension I created for the DS18B20 @ https://github.com/bsiever/microbit-dstemp-alpha. It no longer shows errors if the sensor isn’t connected. I plan to make a few more improvements in the coming months.

    • blogmywiki says:

      Thank you Bill – sorry it’s taken me 2 months to approve this comment, I get a huge amount of comment spam on this blog so every comment has to be manually approved – very annoying that the spam drowns useful comments like yours!

  6. Stein Fluijt says:

    Hi,
    When I impletement the code to just read the temperature: (On ButtonA pressed: Shownumber-Round-Pin01-Temperature number) it just returns de number 0. Now, I dont have space for a breadboard, so I have soldered the wires of the DS18B20 straight to the microbit. Do you have any idea why the DS18D20 is not sending any information (or only a 0) to the microbit?

    • blogmywiki says:

      Hi – apologies for delay, I get so many spam comments it’s hard to find the real ones! Maybe try a newer version of the extension, see Bill’s comment above. Let me know how you get on – Giles

  7. ryan clark says:

    Hi there,

    Using the new extension I’m getting a string reading ‘infinity’ displaying on the Microbit LEDs. Do you know if this updated extension is working?

    • blogmywiki says:

      Hi Ryan – I’m not sure I’m afraid, I need to look into this.

    • Bill Siever says:

      If it’s negative infinity, that means it’s unable to communicate with the sensor. This is usually due to a wiring problem, incorrect resistor value, or bad sensor.

      • Ryan says:

        Hi Bill,

        Still having no luck…

        Wiring seems fine. The same wiring works on an Arduino, just doesn’t want to work on the Microbit. Have you had this working recently? Any change you could have a look and see if it still works…I’m at a loss?

  8. ryan says:

    Hi again,

    Still getting the infinity message? The wiring seems fine (the exact setup on an Arduino works everytime?)
    Both of the extensions on this page seem to be faulty? Have you managed to get this working recently?

  9. Rob says:

    As a teacher wanting a really quick solution to use the trusty accurate DS18B20 sensor with the micro:bit, this is awesome! Thanks for this and the coders who wrote the extension.

  10. BAFOUN says:

    I Have problem. After add the lib I get massage
    “Compilation failed, please check your code for errors.”
    Do you have any sollution for this bug.

  11. Hello.
    The micro: bit has been upgraded and a compile error has come to occur. It seems that the internal structure has changed. If you know how to fix it, please let me know.

  12. Bill says:

    The URL shown above (https://github.com/bsiever/microbit-dstemp-alpha) no longer works. It has been updated to support both V1 and V2 of the micro:bit and is now an approved extension. Just search for “dstemp” in the extension search (or use the new URL: https://github.com/bsiever/microbit-dstemp).

    Bill

  13. Nick says:

    Hi, and thanks for this useful post. I’m working on a project and have added the new DSTemp extension

    However, I keep getting errors when trying to use the new block “Temp in Celsius on P0″ Have tried using it simply e.g. when button A pressed, Show number “Temp in Celsius on P0″ and it works fine in the simulator until I actually try downloading it onto the Microbit and then I get a “Compilation failed please check code for errors” message.

    Any ideas would be appreciated? Am very much a beginner!

    • blogmywiki says:

      Hi Nick, thanks for getting in touch. I’ve not had chance to test the new extension yet, it’s probably worth you creating an issue in Bill’s GitHub repo for it: https://github.com/bsiever/microbit-dstemp/issues
      Good luck and keep in touch!

    • Bill says:

      Nick,

      Are you using a v1 or v2 micro:bit?

      Can you check that you’re using v0.1.20 of the extension by: a) Click on the Python or JavaScript button in the top center of the MakeCode window (this lets you see code in Python/JavaScript rather than blocks), b) Click the down arrow on the “Explorer” button that appeared in the simulator pane, c) The version number will be to the right of the “microbit-dstemp” entry.

  14. Espen Espeseth Clausen says:

    Hi. Trying to follow your code and setup, but get a strange -Infinity error on the microbit when trying to run the code on the Microbit. Any ideas?

    • blogmywiki says:

      Hi there – this article is really out of date now. Are you using the most recent extension mentioned at the top of the page, https://github.com/bsiever/microbit-dstemp)?
      best wishes
      Giles

    • Bill says:

      That error usually means that it wasn’t successfully able to read the temperature after three tries. There could be a few reasons for it. Can you share where you got your temperature sensor from? (It looks like there are a lot of “clones” that may have slightly different behaviors. This article explains some problems: https://github.com/cpetrich/counterfeit_DS18B20. I tested the work with a sensor from a Dallas Semiconductors reseller)

      Also, could you check which version of the extension you’re using? Click on the Python or JavaScript button in the top center of the MakeCode window (this lets you see code in Python/JavaScript rather than blocks), b) Click the down arrow on the “Explorer” button that appeared in the simulator pane, c) The version number will be to the right of the “microbit-dstemp” entry.

  15. Bill says:

    For anyone having trouble with the DSTemp extension, could you try this:
    https://makecode.microbit.org/_e2w1hiDpLM6A

    See if it shows an error code and message. If so, what are they?

    Thanks for any feedback,
    Bill

    • shaby sheik says:

      Hi Bill
      I get the – infinity, followed by “1 not connected”
      This happens on a group of 10 external thermometers that I purchased via ebay (china I think).

  16. kim says:

    Thank you all the time for your help.
    Please give us your feedback on this issue.
    -Infinity
    3 read time out

    • Bill says:

      Hi kim,

      Did you get your sensor anywhere in North America? If so, can you let me know where so I can get one for debugging? (There are a lot of knockoffs that behave slightly differently. I may be able to fix the extension to work if I can do some testing with a non-working sensor)

      You are also welcome to try to the “2 wire” version of the extension, which doesn’t use a resistor. Search for the “dstemp-2wire” extension in MakeCode. It’s a lot easier to connect in this mode. Directions are here: https://github.com/bsiever/microbit-dstemp-2wire. (That being said, if the normal, 3-wire approach didn’t work I doubt this one will either).

  17. Juuso Lymy says:

    I have used ds18b20 tempsensor with Micro:bit V1 and it has work well with
    a dstemp extension. The plot bar graph of temp p0 shows a temperature. But now I try to use Micro:bit V2 and can’t get it works. Only value is “Infinity”. No temperature. How can I make V2 works as well as V1?

    • Bill says:

      @Juuso: The extension is very sensitive to timing. In my tests it has worked on the V2 with genuine Maxim sensors, but not with most of the “clones” that are sold as DS18B20s. If you let me know where you got your sensor, I may be able to identify what’s going wrong.

  18. Ryan says:

    Hi all,

    Extension works great!

    I went through 4 different suppliers to find genuine DS18B20s. Some retailers were selling ‘genuine DS18B20′s’ which when tested using the Github link Bill provided, turned out to be fake.

    • Bill says:

      Thanks for sharing Ryan! I’m glad it’s working for someone. I haven’t gotten any samples of the non-working sensors, so I haven’t been able to try to identify what the problem is for all those who are having problems with it.

      Bill

  19. Juuso Lymy says:

    Micro:bit V1 works fine but V2 shows “infinity”. What could be wrong?

Leave a Reply to BAFOUN 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>