Connect DS18B20 temperature sensor to micro:bit

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:

Posted in computers, microbit | Tagged , | Leave a comment

Snow Leopard Mac won’t boot – startup disk full

or: what to do when your son tells you 10 minutes before he needs to go to school that the old family iMac is broken and he can’t get his work off that needs handing in like right now.

Symptom: old Mac running MacOS Snow Leopard 10.6.8 will get to login screen, but when you log in you get a blank desktop, no icons, no menu, just a mouse pointer.

Cause: the startup disk is utterly full despite like nobody downloading anything on it like, ever, oh my god.

Solution: using target disk mode wasn’t an option as we didn’t have the right adaptors or leads – the idea is you boot it up in a special way (hold down letter T on keyboard as it boots) so it becomes a Firewire or Thunderbolt disk, you mount it on another Mac, delete some files, disconnect, reboot. So that left me with the command line Single User Mode.

Here’s what I did. Rebooted the Mac holding down cmd S. This takes you to a command line where – if you are very careful and know what you’re doing – you can manually delete some files or folders to free up some space. What the first two guides I found didn’t tell me, however, is that this mounts the disk as read only, so you need to type sudo /sbin/mount -uw / to mount the drive in a way that allows you to delete files.

You can see how much space is free using df -h

Remove files using the rm command – I’m not going to explain how to remove folders because it’s risky – have a look online, and be careful!

Proceed with extreme caution. Luckily, I knew there was an old Dropbox folder, not updated in years, on my login so I was able to delete one folder and free up 2GB of space, enough to restart the Mac (type reboot).

Note that Single User Mode only works on old Macs – MacOS High Sierra 10.13 or earlier – presumably because this is a massive security hole allowing anyone to read your files.

Posted in Apple, MacOS X | Tagged , , | Leave a comment

KettleRun3

Back in 2016, I made KettleRun, a micro:bit game where you had to navigate your space ship through a canyon in order to be rewarded with a nice, hot cup of tea.

Now I’ve made a version for the Pimoroni scroll:bit – this is a bright 17×7 pixel (odd dimensions, right?) external LED display that you can program from MakeCode by adding the scroll:bit extension.

My new version of the game is a bit different – you navigate your ship (a bright dot on the left of the screen) through the canyon by tilting the micro:bit down and up. It has no levels, but the game gets faster as you go. You get more points the longer you survive, losing one of your 9 lives if you bash into rocks. Your lives are shown with a bar-graph on the micro:bit’s own 5×5 red LED display.

Download the code for the game here.

Posted in microbit | Tagged , , , | Leave a comment

Surreal random micro:bit story printer

This is a slight twist on my original Little Box of Poems I originally made back in 2012. It mashes that up with Papert-inspired random poetry to create surreal haiku-like poems or story starters.

This project uses a BBC micro:bit and a serial thermal printer. Wiring it up is very simple: connect the black data wire on the printer to GND on the micro:bit, and the printer’s yellow data RX wire to pin 8 on the micro:bit (you could use another pin, I just happened to use pin 8). The printer needs 5 volts to power it, so it needs a separate power supply. Luckily USB sockets give 5v, and thanks to this tutorial from the awesome Tanya Fish I chopped up an old unwanted USB lead to make my own source of printer power.

How to wire a serial printer to a micro:bit

Parts list:

  • micro:bit with power source, battery or USB.
  • Some way of connecting pins – edge connector and jumper wires or crocodile clip leads.
  • A serial thermal till-roll printer like this one and some thermal paper.
  • A 5v power source for the printer – an old USB lead chopped up and a USB power source will do nicely.
  • Python program below and here – you can flash it onto the micro:bit using the online micro:bit Python editor or Mu.

The Python program is in two parts – the first part is all the functions to send control codes for different styles of printing – most of these aren’t used in this project but you might have fun playing with them – you can also print different kinds of barcode if you look here.

The second part creates lists of different kinds of words: animal / people nouns, verbs, adverbs, prepositions, adjectives and place nouns. Each time you press button A, it picks a random word from each category and prints them out. You could use this as a story starter, use it as a passphrase or just enjoy the surreal haiku-like poems. Add your own words. How long a story can you get a machine to write? Let me know what you do with this idea, using a printer, 2-line LCD or just the built-in LED display.

Download Python program here.


import microbit
import random
microbit.uart.init(baudrate=19200, bits=8, parity=None, stop=1, tx=microbit.pin8, rx=None)

# Print lines of text in buffer
def thermal_print_ln(msg):
    microbit.uart.write(msg+"\n")

# Send text but don't print - you need to send a
# newline (\n) character to empty buffer & print.
# This enables mixed print modes on same line.
def thermal_print(msg):
    microbit.uart.write(msg)

def doubleHeightOn():
    microbit.uart.write("\x1B\x21\x10") 

def doubleHeightOff():
    global print_mode
    microbit.uart.write("\x1B\x21\x00") 

def smallFontOn():
    microbit.uart.write("\x1B\x21\x01") 

def smallFontOff():
    microbit.uart.write("\x1B\x21\x00") 

def boldOn():
    microbit.uart.write("\x1B\x45\x01") 

def boldOff():
    microbit.uart.write("\x1B\x45\x00") 

def wideOn():
    microbit.uart.write("\x1B\x0E") 

def wideOff():
    microbit.uart.write("\x1B\x14") 

def inverseOn():
    microbit.uart.write("\x1B\x21\x02") 

def inverseOff():
    microbit.uart.write("\x1B\x21\x00") 

def upsideDownOn():
    microbit.uart.write("\x1B\x7B\x01") 

def upsideDownOff():
    microbit.uart.write("\x1B\x7B\x00") 

def underlineOn():
    microbit.uart.write("\x1B\x2D\x02") 

def underlineOff():
    microbit.uart.write("\x1B\x2D\x00") 

def largeFontOn():
    microbit.uart.write("\x1D\x21\x11") 

def largeFontOff():
    microbit.uart.write("\x1D\x21\x00") 

def leftAlign():
    microbit.uart.write("\x1B\x61\x00")

def centreAlign():
    microbit.uart.write("\x1B\x61\x01")

def rightAlign():
    microbit.uart.write("\x1B\x61\x02")

# prints test page
def printerTest():
    microbit.uart.write("\x12\x54")

# resets the printer to default values
def printerReset():
    microbit.uart.write("\x1B\x40")

# press button A to activate demo
# increase printing temperature and time
microbit.uart.write("\x1B\x37\x07\xFF\xFF")

noun_list = ["fish ", "dog ", "cat ", "girl ", "boy ", "duck ", "teacher ", "snake ", "kitten ", "puppy ", "bird ", "bee ", "spider ", "ant "]

verb_list = ["walks ", "runs ", "eats ", "sings ", "smiles ", "waves ", "swims ", "talks ", "stands ", "looks ", "nods ", "sleeps ", "flies ", "floats "]

adverb_list = ["quickly ", "slowly ", "happily ", "sadly ", "gracefully ", "hungrily ", "well ", "badly ", "bravely ", "lazily ", "joyfully ", "crossly ", "loudly ", "quietly "]

preposition_list = ["under ", "over ", "beneath ", "through ", "into ", "above "]

adjective_list = ["dark ", "spooky ", "lonely ", "ancient "]

place_list = ["sky ", "pond ", "forest ", "ocean ", "lake ", "city ", "village "]

while True:
    if microbit.button_a.was_pressed():
        poem = noun_list[random.randint(0, len(noun_list)-1)] \
        + verb_list[random.randint(0, len(verb_list)-1)] \
        + adverb_list[random.randint(0, len(adverb_list)-1)] \
        + preposition_list[random.randint(0, len(preposition_list)-1)] \
        + "the " + adjective_list[random.randint(0, len(adjective_list)-1)] \
        + place_list[random.randint(0, len(place_list)-1)]
        thermal_print_ln("micro:bit poem or story")
        thermal_print_ln("specially made for you!")
        largeFontOn()
        thermal_print_ln(noun_list[random.randint(0, len(noun_list)-1)] + verb_list[random.randint(0, len(verb_list)-1)])
        thermal_print_ln(adverb_list[random.randint(0, len(adverb_list)-1)] + preposition_list[random.randint(0, len(preposition_list)-1)])
        thermal_print_ln('the ' + place_list[random.randint(0, len(place_list)-1)])
        thermal_print_ln(" ")
        thermal_print_ln(" ")
        largeFontOff()
    microbit.sleep(300)

p.s. I’ve tried printing bitmaps from the micro:bit with no luck, but this article may offer a clue.

Posted in microbit | Tagged , , | 3 Comments

Add a 16×2 LCD display to a micro:bit

I had one of these cheap 16 x 2 LCD display modules hanging around that I bought to go with some Arduino or Raspberry Pi project that I never finished – in fact I couldn’t get the thing to work at all.

So I’m delighted to have got it working with a micro:bit! Here’s what I used:

  • 16 x 2 LCD module
  • medium-large breadboard
  • a whole heap of jumper wires, some male-male, some male-female
  • a BBC micro:bit
  • a Kitronik micro:bit breakout board
  • a 5V power supply
  • a 1KΩ resistor

I used this project as the basis, which includes a Python program to drive the display (registration required). You don’t need to download MicroPython to program a micro:bit, you can use the online editorthe beta Python editor will even allow you to flash programs straight to your micro:bit over webUSB if you’re using Chrome.

The wiring diagram isn’t very clear on that website, so here’s a list of all the pins on the LCD display and what you need to connect them to:

LCD pin LCD function connect to
1 GND – 0v GND on micro:bit & -ve 5v
2 5v in +ve 5v
3 Contrast GND via a resistor
4 Register select micro:bit pin 0
5 Read/write GND
6 Enable micro:bit pin 1
7 Data DB0 not connected
8 Data DB1 not connected
9 Data DB2 not connected
10 Data DB3 not connected
11 Data DB4 micro:bit pin 8
12 Data DB5 micro:bit pin 12
13 Data DB6 micro:bit pin 2
14 Data DB7 micro:bit pin 13
15 Backlight +ve +ve 5v
16 Backlight GND -ve 5v

I tried driving the whole thing off the 3v supply on the micro:bit, but it didn’t work – I think you really do need an external 5v power supply as there has to be a bigger difference in voltage between the power in and the contrast pin (although perhaps someone can do something clever with this information?) I didn’t have one to hand, so I chopped an old USB lead in half, and stripped the wires back to get 5v off the red (positive) and black (negative) wires, which I connected to the +ve and -ve (GND) rails on my breadboard.

Normally you’d use a potentiometer to adjust the contrast, but I just used a 1KΩ resistor instead.

You’ll see in the video that I added a little switch as well to turn the backlight on and off and you’ll see below I found an old volume control or something which I’ve pressed into service as a contrast knob on my maximum / minimum temperature display:

Here’s the Python program that does the temperature display (not including the LCD driver code):

InitDisplay()

def showTemp():
    clear()
    showText('Current temp: ' + str(temperature()) + 'C')
    setCursor(0,1)
    showText('Max: ' + str(maxTemp) + '  Min: ' + str(minTemp))

currentTemp = temperature()
maxTemp = currentTemp
minTemp = currentTemp
showTemp()

while True:
    if currentTemp != temperature():
        currentTemp = temperature()
        if currentTemp > maxTemp:
            maxTemp = currentTemp
        if currentTemp < minTemp:
            minTemp = currentTemp
        showTemp()
    sleep(1000)

It would be nice if someone made an adaptor to allow you to plug one of these common LCD modules straight into a micro:bit, with a USB input for 5v display power, maybe back-powering the micro:bit with 3v?

Now what else shall I do with it? Show received radio messages from other micro:bits, make another Little Box of Poems or other random fact dispenser?

Posted in microbit | Tagged , , | Leave a comment