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.

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

3 Responses to Surreal random micro:bit story printer

  1. Thank you for this! I’ve always wanted to make something like this and successfully followed your directions. However, I’ve reached the word limit and would like to significantly expand it. Do you know of any workarounds for the micro:bit? Also, can you tell me, or direct me to a source, whether this Python script could work with Raspberry Pi and what wiring diagram I might need?

    • blogmywiki says:

      Hi Howard! I tried doing a ‘choose you’re own adventure’ too but am pretty sure I will run out of storage as well. A twitter friend suggested using a serial connection to a computer to send new text, but if you need a separate computer then you might as well go down the Raspberry Pi route. I made printer Pi-based printer projects ages ago, though the instructions are probably very out-dated now. The Pi would allow you to use a USB thermal printer which would be easier to wire up, if not code. If you took the code on this page and replaced the micro:bit-specific bits (button presses, uart.write etc) with the relevant commands for the Pi you certainly could do the same thing with much, much more storage at your disposal, it would make a great project! Let me know if I can be of any more help and good luck! (This may be of use, but again it’s very old: https://github.com/luopio/py-thermal-printer )

  2. Thank you! I’m pretty much a beginner, so forgive me for easy questions. First, this is the printer I’m using with the micro:bit: https://www.adafruit.com/product/597

    Would that work? I would use the same wiring as your random poetry printer, but modify the code from your story printer to work with the Pi?

    Thank you! This has been a dream of mine, and overcoming the hurdles, doing the learning and debugging necessary to make this work with a much larger vocabulary, is an important part of my learning.

Leave a Reply to Howard Rheingold Cancel reply