Arduino composite video out using only 3 resistors

It’s supposed to be easy – 2 or 3 resistors and you can get your Arduino micro controller to produce low-resolution black and white composite video. Well, it wasn’t for me. It took me hours to get it working, but I finally cracked it. It’s possible I was unlucky, stupid or possibly both – but I’m writing it up before I forget how I did it and in case someone else has the same problem.

I tried about 4 different projects on different web sites which all broadly worked the same way: you connect a couple of resistors to some pins on the Arduino, wire them up to a phono plug, install the TV Out library in the Arduino IDE, compile and flash the code and off you go.

I could not get any of the sample code to compile in the current version of the Arduino IDE. I lost track of the different error messages I got. I’d make progress then something else would break. Googling error messages kept telling me to install the TV Out libraries – which I had.

Spookily I’d decided to try an old version of the Arduino IDE at the same time that @gadgetoid on Twitter made the same suggestion. In the end I compiled and uploaded working code using version 1.0.1 of the Arduino IDE on an old Lenovo Ideapad running MacOS X 10.6 (Snow Leopard). I could not get any demo code to compile using the current version of the Arduino IDE, nor could I install an old version of the IDE on my MacBook because they required older versions of Java. Thank goodness for the old hackintosh!

I used:

  • An old Arduino UNO (perhaps its age was part of the problem)
  • a phono / RCA plug
  • some jumper wires
  • a small breadboard
  • A 470 ohm resistor
  • A 1K ohm resistor
  • A 75 ohm resistor
  • An old laptop to program the Arduino
  • an old CRT TV with a composite video input (most modern LCD TVs have such inputs – or you can use a SCART adaptor)

Here’s how I wired it up. (In the photo there are 4 resistors not 3 because I lacked a certain value and wired two together in series).

(I am grateful to microcontrollerelectronics.com for hosting the Fritzing diagram of their project – the only difference here is that I had to move the green video wire to pin 7 (not 8) as I am using an Arduino UNO not a Leonardo as they did.)

The version of the TV Out library that I think I used finally (there were so many) was TVOutBeta1.zip – unpacked the zip file and put the 3 folders pollserial, TVout and TVoutfonts in the Arduino/Libraries folder like this:

This is the code that finally worked – I’m afraid I can’t remember where I copied it from now, I had so many snippets of code on the go. If it’s yours I will of course credit and link (or remove it).

Here it is working on a modern LCD TV rather than the retro CRT model at the top of the page:

Now I wonder if I can remember why I wanted to get this working in the first place..?

#include <TVout.h>
#include <fontALL.h>

TVout TV;
unsigned char x,y;

void setup()  {
  x=0;
  y=0;
  TV.begin(PAL);	//for devices with only 1k sram(m168) use TV.begin(_NTSC,128,56)
  TV.select_font(font6x8);
}

void loop() {
  TV.clear_screen();
  x=0;
  y=0;
  for (char i = 32; i < 127; i++) {     TV.print_char(x*6,y*8,i);     x++;     if (x >= TV.char_line()) {
      y++;
      x=0;
    }
  }
  TV.delay(1000);
  TV.clear_screen();
  TV.println("Fill the Screen\nPixel by Pixel");
  TV.delay(1000);
  TV.clear_screen();
  for(x=0;x
Posted in Arduino, computers, hardware | Tagged , , , | 1 Comment

The truth about cats AND/OR dogs

Here’s an activity I think you could do with almost any year group in KS2 or KS3. You could use it to teach about the origins of modern databases in Hollerith’s punch cards used to collect US census and immigration data in the 1890s. You could link it to the Jacquard loom punch cards that Ada Lovelace and Charles Babbage envisioned being used to program the Analytical Engine in the 1830s. You could also use it to teach logic gate concepts like AND and OR.

I got this idea from a school’s TV programme I must have seen over 40 years ago, which I only just remembered when reading about Hollerith’s tabulating machines in Code by Charles Petzold.  I think it was a programme about data processing and computers, and they made a really cool selection tool using a cornflake box, punched cards and pencils. I can’t remember the precise configuration, and it was way cleverer than this project, but it works on a similar principle.

You have a stack of cards about cars, people, or in my case pets. You encode their properties using either a punched hole or a slot. For example my pet is a dog, black and female, so I snip out all the other labels and leave holes for dog, black and female.

I do the same for all the other animal cards and stack them up. The neat thing is that all the property fields are visible no matter what card is on top.

Now if I put a pencil in the black hole and lift it up, it picks up all the black animals in the pack. This is like running a query in a database:

Of course I may want to select more than one property at a time, and I can do that with these cards. Say I want to sort out all the animals that are black AND female. Aha, I know, I’ll put a pencil in the black hole AND the female one. (It doesn’t matter what order the cards are stacked in).

The results, however, may not be quite what you may expect. We seem to get all the animals that are black OR female, shown on the left. (Rejected cards left behind are on the right – note that these are NOT black and NOT female.)

So we need to sort them again. I stack up the 3 cards I picked up on the left and re-select black. (I could also reselect female but they are all already female). Note that we can see that we now have no white, male pets left in this stack.

Finally I’m left with just the pets that are black AND female, Lilith the cat and Ellie the dog on the left:

There’s lots of scope for expanding this. Can you redesign the cards so you can select black AND female in 1 pass rather than black OR female as I do here? You could build a frame with levers or pins in a DT project. And using the blank cards you can make as many cards as you like, or make your own cards with more than 7 properties. Could you encode numbers, either as binary or using the system Hollerith used with number ranges? The possibilities are endless.

You can download PDF files of the cards here.

Please let me know your ideas or if you have used something similar in class!

Posted in computers, education, ICT | Tagged , , , , | Leave a comment

Trash can PC

Here’s how I got a useful, working PC for next to nothing.

SonB and I found a couple of old PC chassis dumped on the street a year or so ago, both missing hard drives but we took the memory from one and put it in the higher spec base unit, an old Evesham PC that had a Soundblaster digital audio card and a Hauppage WinTV DVB-T tuner. Running less /proc/cpuinfo at the command line on it reports the CPU as an AMD Athlon(tm) 64 X2 Dual Core Processor 4600+ running at 1GHz.

We got this PC to boot off a memory stick but never did anything with it, but my interest in it was re-sparked when the Raspberry Pi foundation released a version of its PIXEL operating system that runs on normal PC (and some Mac) hardware. Would the trash PC boot PIXEL off a 2GB memory stick? Yes it would!

I had to experiment a bit with the BIOS settings on the PC to get it to boot off USB, and I found one of the USB sockets was physically damaged, but I soon got it to boot into PIXEL. Booting was slow but it worked – patience paid off. The PC doesn’t have any built-in wifi, so I plugged an ethernet cable in the back and into my broadband router.

The picture shows me simultaneously web-browsing, playing music in Sonic Pi out of the SoundBlaster card, drawing a Matisse snail in Python – and of course doing a screen-shot using scrot. The picture quality on my old Sony TV is amazingly good – luckily I had a DVI to HDMI cable lying around as my TV only has HDMI inputs.

So there we go – discarded PC found on the street, minus hard drive to perfectly perfectly serviceable computer in about 15 minutes! The PC also has 2 Firewire 400 ports which I might be able to make use of – I have a load of old FW devices – plus SPDIF and optical audio ports, an E-SATA socket, multi-format card-readers, a DVD-ROM drive, another DVD-RW drive… plus that Hauppage TV card! I even wrote this blog post on the computer, though the PIXEL OS wouldn’t see the built-in card reader.

The OS would read an audio CD but not play the files, so I installed VLC but couldn’t get it to play the CD – possibly the drive is damaged, or something is missing from the x86 PIXEL version of Raspbian.  A fun foggy afternoon, however, as the Flash player works! Here it is playing Mark Kermode’s video round-up of the worst films of 2016:

It will also play radio programmes on the BBC iPlayer Radio web pages, and – though it took an age to get going – here it is playing a TV show perfectly off the BBC iPlayer web page! One of our home movies, I think…

 

Posted in Linux, Raspberry Pi, thrift | Tagged , | Leave a comment

Microbit wireless data logger with printout

This project uses 2 microbits – one attached to a thermal till-roll printer as before. This time there’s a second microbit which can be battery-powered and put on a car, parachute, dog, whatever takes your fancy. Every 10 seconds it transmits radio data about its temperature, x/y/z data from the accelerometer and its compass heading. This is picked up by the printer’s microbit and logged on paper. (You could also save a file with the data which might be more useful!). Time information is missing of course, but if you know when you started you could always work that out…

Here’s the astonishingly compact Python code for microbit A which stays put and is attached to the printer. It constantly polls for incoming radio messages; when it gets one it prints the message with a few blank lines after. Flash it to the microbit using the Mu editor.

from microbit import *
import radio

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

radio.on()
while True:
    incoming = radio.receive()
    if incoming:
        uart.write(incoming+"\n\n\n")

Here is the Python code for microbit B which flies away and has adventures in temperature and space. Once calibrated by drawing a circle, every 10 seconds it sends its temperature, compass heading and accelerometer data by radio to the printer. You could obviously increase the time or have its transmission triggered instead by another event such as a button press or even falling!

from microbit import *
import radio

compass.calibrate()
# remove next line to save power
display.scroll('transmitting data', wait=False, loop=True)
radio.on()

while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()
    z = accelerometer.get_z()
    tx_message = str(temperature())
         +"c  head "+str(compass.heading())
          +"\nx"+str(x)+" y"+str(y)+" z"+str(z)
    radio.send(tx_message)
    sleep(10000)   # wait 10 seconds
Posted in microbit | Tagged , , | Leave a comment

Connecting a thermal printer to a BBC microbit

UPDATE – I have tidied up the code a lot and put a demo program on Github for you to enjoy – prints out a demo page like the one below:

I have a Sparkfun thermal till-roll printer which I used for making my Little Box of Poems – this was a box that printed out a random poem at the press of a big red shiny button. The original one was powered by an Arduino and then I made a Raspberry Pi version. I also made an internet printer using it, and a box that printed out the weather forecast just before I woke each day.

It had been sitting idle for a while and I started to wonder if I might be able to make a Little Box of Poems (or something similar) using a humble BBC micro:bit. And if making 2-player wireless microbit Pong was harder than I expected, then this was actually much easier than I could have dared hope.

This is what you’ll need:

  • a microbit
  • a thermal till-roll printer as sold by PimoroniSparkfun or Adafruit
  • a 5-9v power supply for the printer that can deliver at least 1.5A, 2A is better.
  • 2 jumper leads to connect the GND and RX pins of your printer to the microbit
  • I also used a Kitronik edge connector / breakout board to make connecting to the microbit easier.
  • Some Python code (below)
  • The Mu editor to flash the Python program to your microbit.

I connected the GND pin on the microbit to the black TTL GND wire on the printer, and I connected pin 8 on the microbit to the yellow TTL RX wire on the printer. I didn’t connect the printer’s TX pin as this is just 1-way communication, microbit to printer.

I had absolutely no idea what I was doing, but armed with an amusingly-translated Chinese instruction manual, some wires and blind faith I found that I could print some text with just a few lines of Python. It was a bit feint and garbled at first, and I seemed to be getting messages from the console! Look carefully and you’ll see my ‘hello world’.

After a bit of jiggery-pokery (and reading the manual) I realised that I needed to send a linefeed (LF) character to get it to print the contents of the buffer. I found that at its simplest I could print different messages with different button presses with just a few lines of Python. I chose pin 8 for transmission as it looked like it wasn’t used for anything else, and the only other wire you need to connect is GND on the microbit to GND on the printer TTL connection. The \x0A at the end of the message is a hex code for line feed – this causes the printer to print the contents of its buffer and feed the paper up 1 line.

import microbit

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

while True:
    if microbit.button_a.is_pressed() and microbit.button_b.is_pressed():
        microbit.uart.write("both buttons pressed\x0A\x0A")
    elif microbit.button_a.is_pressed():
        microbit.uart.write("message A\x0A\x0A")
    elif microbit.button_b.is_pressed():
        microbit.uart.write("message B\x0A\x0A")
    microbit.sleep(100)

I then wrote some slightly more complex code that would allow wide text, inverse text and even print 13 digit EAN-format bar codes. It would be easy to add double-height printing, more challenging but fun to get graphics working!

import microbit

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

def normal_print(msg):
    microbit.uart.write(msg+"\x0A\x0A\x0A\x0A")

def wide_print(msg):
    microbit.uart.write("\x1B\x0E"+msg+"\x0A\x0A\x0A\x0A\x1B\x14")

def bold_print(msg):
    microbit.uart.write("\x1B\x451"+msg+"\x0A\x0A\x0A\x0A\x1B\x450")

def inverse_print(msg):
    microbit.uart.write("\x1D\x421"+msg+"\x0A\x0A\x0A\x0A\x1D\x420")

def print_barcode(msg):
    microbit.uart.write("\x1D\x6B\x43\x0D"+msg+"\x0A\x0A")

# increase printing temperature - increase hex number A0 for darker print
microbit.uart.write("\x1B\x37\x07\xA0\x02")

while True:
    if microbit.button_a.is_pressed() and microbit.button_b.is_pressed():
        microbit.display.scroll("AB")
        wide_print("both buttons")
    elif microbit.button_a.is_pressed():
        normal_print("1234567890123")
        microbit.display.scroll("A")
    elif microbit.button_b.is_pressed():
        microbit.display.scroll("B")
        inverse_print("message B")
    microbit.sleep(100)

I had to do it – a microbit version of the Little Box of Poems. Press button A and a random short poem is printed. You could modify this code with poems of your own, or perhaps make a Christmas cracker joke generator… let me know what ideas you have!

import microbit
import random

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

def normal_print(msg):
    microbit.uart.write(msg+"\x0A\x0A\x0A\x0A")

def wide_print(msg):
    microbit.uart.write("\x1B\x0E"+msg+"\x0A\x0A\x0A\x0A\x1B\x14")

def bold_print(msg):
    microbit.uart.write("\x1B\x451"+msg+"\x0A\x0A\x0A\x0A\x1B\x450")

def inverse_print(msg):
    microbit.uart.write("\x1D\x421"+msg+"\x0A\x0A\x0A\x0A\x1D\x420")

def print_barcode(msg):
    microbit.uart.write("\x1D\x6B\x43\x0D"+msg+"\x0A\x0A")

poemtitle = ['In a station of the Metro', 'The Sick Rose', 'This is just to say', 'Surprise']

poemtext = ['The apparition of these faces in the crowd;\n\
Petals on a wet, black bough.', 'O Rose thou art sick.\n\
The invisible worm,\n\
That flies in the night\n\
In the howling storm:\n\n\
Has found out thy bed\n\
Of crimson joy:\n\
And his dark secret love\n\
Does thy life destroy.', 'I have eaten\n\
the plums\n\
that were in\n\
the icebox\n\n\
and which\n\
you were probably\n\
saving\n\
for breakfast\n\n\
Forgive me\n\
they were delicious\n\
so sweet\n\
and so cold', 'I lift the toilet seat\n\
as if it were the nest of a bird\n\
and i see cat tracks\n\
all around the edge of the bowl.']

poemauthor = ['Ezra Pound\n', 'William Blake\n', 'William Carlos Williams\n', 'Richard Brautigan\n']

# increase printing temperature - increase hex number A0 for darker print
microbit.uart.write("\x1B\x37\x07\xA0\x02")

while True:
    if microbit.button_a.is_pressed():
        poem = random.randrange(0,4)
        microbit.display.scroll(str(poem))
        wide_print(poemtitle[poem])
        normal_print(poemtext[poem])
        inverse_print(poemauthor[poem])
    microbit.sleep(100)

I then built the printer and microbit into an old soap powder box in the time-honoured Box of Poems tradition:

And finally… a festive twist. This version of the code will print a random poem if you press button A, and a really bad Christmas cracker joke if you press button B:

import microbit
import random

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

def normal_print(msg):
    microbit.uart.write(msg+"\x0A\x0A")

def wide_print(msg):
    microbit.uart.write("\x1B\x0E"+msg+"\x0A\x0A\x1B\x14")

def big_print(msg):
    microbit.uart.write("\x1D\x211"+msg+"\x0A\x0A\x1D\x210")

def bold_print(msg):
    microbit.uart.write("\x1B\x451"+msg+"\x0A\x0A\x1B\x450")

def inverse_print(msg):
    microbit.uart.write("\x1D\x421"+msg+"\x0A\x0A\x1D\x420")

def print_barcode(msg):
    microbit.uart.write("\x1D\x6B\x43\x0D"+msg+"\x0A\x0A")

poemtitle = ['In a station of the Metro', 'The Sick Rose', 'This is just to say', 'Surprise']

poemtext = ['The apparition of these faces in the crowd;\n\
Petals on a wet, black bough.', 'O Rose thou art sick.\n\
The invisible worm,\n\
That flies in the night\n\
In the howling storm:\n\n\
Has found out thy bed\n\
Of crimson joy:\n\
And his dark secret love\n\
Does thy life destroy.', 'I have eaten\n\
the plums\n\
that were in\n\
the icebox\n\n\
and which\n\
you were probably\n\
saving\n\
for breakfast\n\n\
Forgive me\n\
they were delicious\n\
so sweet\n\
and so cold', 'I lift the toilet seat\n\
as if it were the nest of a bird\n\
and i see cat tracks\n\
all around the edge of the bowl.']

poemauthor = ['Ezra Pound\n', 'William Blake\n',
'William Carlos Williams\n', 'Richard Brautigan\n']

joke_question = ['Why are Christmas trees so bad\nat sewing?\n',
'Why did the turkey join\nthe band?\n',
'What song do you sing at a\nsnowman\'s birthday party?\n',
'Which famous playwright was\nterrified of Christmas?\n']

joke_answer = ['They always drop their needles!\n',
'Because it had\nthe drumsticks\n',
'Freeze a jolly\ngood fellow\n',
'Noel Coward!\n']

# increase printing temperature - increase hex number A0 for darker print
microbit.uart.write("\x1B\x37\x07\xFF\xFF")

microbit.display.scroll("Press A for poem, B for joke",
delay=150, wait=False, loop=True, monospace=False)

while True:
    if microbit.button_a.is_pressed():
        poem = random.randrange(0,4)
        wide_print(poemtitle[poem])
        normal_print(poemtext[poem])
        inverse_print(poemauthor[poem])
    elif microbit.button_b.is_pressed():
        joke = random.randrange(0,4)
        wide_print("I say, I say, I say...")
        normal_print(joke_question[joke])
        wide_print(joke_answer[joke])
    microbit.sleep(200)
Posted in computers, microbit | Tagged , , , , | Leave a comment