Making a Thermometer with Trinket M0 and DS18B20

Breadboard and DS18B20

Build Diary: How I made myself an armpit thermometer with an Adafruit Trinket M0, a DS18B20 temperature sensor, and CircuitPython.

Table of Contents

Gathering Parts & Tools

Parts list

Tools

Where to buy?

I usually order parts and tools from Adafruit, Amazon, DigiKey, or Mouser (the links point to their COVID-19 update pages).

You can learn more about the parts, tools, and skills I used by reading these Adafruit product pages and learning guides:

Soldering Pins & Wires to Prepare for Breadboarding

Solder Trinket M0 Pins

The Trinket M0 comes as a kit with header pins that allow it to plug into a breadboard, but you have to solder the pins on yourself.

Adafruit’s Trinket M0 learning guide doesn’t explain how to solder the header pins, but some of their other guides do. For a good example, check out the Soldering in Plain Headers section of the Feather M4 Express learning guide.

Solder DS18B20 Wires

The DS18B20 temperature sensor comes with a cable that has three thin stranded wires. Black is ground, red is positive, and yellow is the 1-Wire data bus.

Soldering solid 22 AWG hookup wire onto the ends of the thin stranded wires makes it possible to plug them into the breadboard.

Insulate DS18B20 Wires

To prevent the solder splices from touching each other, I insulated the splices with heat shrink tubing. Wrapping each wire splice in electrical tape would have worked too.

Where to learn more about soldering & heat shrink?

Topic Learning Guide Link
Soldering basics Guide To Excellent Soldering
Soldering a wire splice Soldering to EL Wire (scroll to the part about the center wire)
Insulating with heat shrink Soldering to EL Wire (after the part about the center wire)

Loading CircuitPython Code onto the Trinket M0

Download CircuitPython & driver bundle files

  1. Download the latest stable release of CircuitPython from the circuitpython.org Trinket M0 page. As of March 17, 2020, the latest stable release is version 5.0.0. Click the button that says “DOWNLOAD .UF2 NOW”. You should receive a file in your Downloads folder with a name like:

    adafruit-circuitpython-trinket_m0-en_US-5.0.0.uf2

  2. Download the latest release of Adafruit’s CircuitPython driver bundle from Adafruit’s CircuitPython Bundle releases page on GitHub. As of March 17, 2020, the latest release is named “March 16, 2020 auto-release”. There are several release files. But, you should click the link for the file with “bundle-5.x-mpy” in the middle of its name. You should receive a file in your Downloads folder with a name like:

    adafruit-circuitpython-bundle-5.x-mpy-20200316.zip

Update CircuitPython to v5.0+

  1. Plug the Trinket M0 into the breadboard. Do not plug anything else into the breadboard yet.

  2. Connect the Trinket M0 to the computer with the micro-USB cable.

  3. Put the Trinket M0 in bootloader mode by double-clicking the Trinket M0’s RESET button. To avoid damaging the Trinket M0 with a static discharge from my finger, I like to press the button with something non-conductive like a guitar pick or pencil eraser. If you get the double-click timing right, the RGB LED should flash red then turn green. You should see a new USB removable storage icon named TRINKETBOOT show up on your computer.

  4. Copy the file adafruit-circuitpython-trinket_m0-en_US-5.0.0.uf2 (or whatever the latest release is called) onto TRINKETBOOT. After the LED flashes for a while, the TRINKETBOOT drive should go away, and a CIRCUITPY drive should appear in its place.

Copy OneWire & DS18B20 libraries into lib/

  1. Unzip adafruit-circuitpython-bundle-5.x-mpy-20200316.zip (or whatever the latest 5.x-mpy bundle file is called) onto your computer. This should give you a folder like adafruit-circuitpython-bundle-5.x-mpy-20200316 with another folder inside of it called lib.

  2. Inside the adafruit-circuitpython-bundle-5.x-mpy-20200316/lib/ folder, find these items and copy them into the lib folder on your CIRCUITPY drive:

    • adafruit_onewire (copy this whole folder)
    • adafruit_ds18x20.mpy

Copy thermometer code into code.py

  1. Make sure you have a text editor on your computer. On macOS, I like using BBEdit or emacs. Adafruit recommends using Mu. For more information about Mu, see the Installing Mu Editor and Creating and Editing Code sections of Adafruit’s Welcome to CircuitPython learning guide.

  2. With your text editor, create a file named code.py on your CIRCUITPY drive. If there was already a code.py file, then just open it.

  3. Copy this code and paste it into code.py (replacing any existing code):

    import board
    import busio
    from adafruit_onewire.bus import OneWireBus
    from adafruit_ds18x20 import DS18X20
    import time
    
    #       Trinket M0   [blk,red,yel = DS18B20]
    #        Bat USB
    # blk -- Gnd   0
    #        4     1 -- 4.7kΩ to 3V, yel
    #        3     2
    #        Rst  3V -- 4.7kΩ to 1, red
    
    ow = OneWireBus(board.D1)
    ds18b20 = DS18X20(ow, ow.scan()[0])
    while True:
        # This takes 0.75 seconds
        C = ds18b20.temperature
        F = C * 9/5 + 32
        print('{0:0.1f} °F'.format(F))
        # Show a measurement every 3 seconds
        time.sleep(2.25)
    
  4. Save code.py and quit your text editor. Your Trinket M0 should automatically start running the new code. But, it won’t work right yet because you haven’t connected the temperature sensor.

Why does updating CircuitPython matter?

Adafruit regularly updates CircuitPython with bug fixes and new features. I wrote my thermometer code for CircuitPython version 5.0. Depending on where and when you purchase a Trinket M0, it might come installed with an older version.

Adafruit also provides many hardware driver libraries to extend CircuitPython. Because the Trinket M0 doesn’t have enough storage space for all the libraries, you have to pick the ones you need and install them individually. My thermometer code uses the OneWire and DS18X20 driver libraries.

Where to learn more about CircuitPython?

The firmware update instructions above come from links and procedures spread across several areas of Adafruit’s documentation. If you want more background, try reading these learning guides:

Finishing Up: Assemble & Test the Thermometer Circuit

Connecting wires on the breadboard

  1. Tell your computer to eject the CIRCUITPY USB removable storage drive, wait a few seconds for it to finish ejecting, then unplug the USB cable.

  2. Connect the 4.7 kΩ resistor and hookup wires from the DS18B20 to your breadboard as shown in this picture:

    breadboard closeup

    1. Black wire to Gnd pin
    2. Resistor between pins 3V and 1
    3. Red wire to 3V pin
    4. Yellow wire to pin 1
  3. Double check your wiring.

Monitor temperature measurements on the serial console

  1. Plug the USB cable back into the computer. Other than the Trinket M0’s LED’s flashing a bit, nothing interesting will happen until you connect to the Trinket M0’s USB serial port with a serial console. The Mu editor has a built-in serial console, but I like to use a command line program called screen.

  2. Find the Trinket M0’s USB serial port device and connect to it with a serial console. For example, using the Terminal app on macOS:

    Find the serial port device (the number at the end can change):

    $ ls /dev/tty.usb*
    /dev/tty.usbmodem14201
    

    Connect to the serial port device using screen as a serial console:

    $ screen /dev/tty.usbmodem14201
    

    Once screen connects, it should show a new temperature measurement from the print() statement in code.py every 3 seconds. This is what I got from briefly holding the DS18B20 in my armpit (my DS18B20 takes about five minutes to warm up to body temperature):

    69.6 °F
    69.6 °F
    70.1 °F
    71.8 °F
    73.3 °F
    74.7 °F
    76.0 °F
    77.0 °F
    77.9 °F
    

    If you’re used to using command line tools, you might expect that typing control-c or control-d would disconnect screen from the serial port. That won’t work.

    To disconnect screen from the USB serial port, type control-a, then k, then y. Using the notation of emacs key bindings, that would be C-a k y

    If that sounds confusing, you should probably try using the Mu editor.

Where to learn more about serial consoles?

When your CircuitPython code uses a print() statement, the text output goes to the Trinket M0’s USB serial port. You can view text from a USB serial port by connecting to it with a serial console. The Mu editor has a serial console.

You can read more about serial consoles in the Connecting to the Serial Console and Interacting with the Serial Console sections of Adafruit’s Welcome to CircuitPython learning guide.

3/28/2020