Hey! This article is really old and probably doesn’t work any more: things have changed a lot in Raspberry Pi world since 2013 â€¦
Update 3: code for the demo video is here.
Update 2: In which I actually post working code.
Update: Eep! This post was featured on the Raspberry Pi blog today. Thanks, Liz!
And now for something completely different:
… a clock that isn’t human readable. You’ll need a QR code reader to be able to tell the time.
This, however, is not the prime purpose of the exercise. I was looking for an excuse to try some direct hardware projects with the GPIO, and I remembered I had a couple of Nokia-style surplus LCDs lying about that could be pressed into service. These LCDs aren’t great: 84Ã—48 pixels, 3V3 logic, driven by SPI via an 8-pin header which includes PWM-controllable LED backlighting. They are cheap, and available almost everywhere: DealExtreme ($5.36), SparkFun ($9.95), Adafruit ($10 â€“ but includes a level shifter, which you really need if you’re using a 5V logic Arduino), Solarbotics ($10) and Creatron (about $12; but you can walk right in and buy one). Despite being quite difficult to use, helpful people have written drivers to make these behave like tiny dot-addressable screens.
I’d been following the discussion on the Raspberry Pi forum about driving the Nokia LCD from a Raspberry Pi. Only when user bgreat posted some compact code that was supposed to run really fast did I dig out the LCD board and jumper wires. Building on bgreat’s nokiaSPI.py class and a few other bits of code, here’s what I built to make this singularly pointless clock:
#!/usr/bin/python # -*- coding: utf-8 -*- # qrclock - The Quite Rubbish Clock for Raspberry Pi - scruss, 2013-01-19 import time # need to use git://github.com/mozillazg/python-qrcode.git import qrcode from PIL import Image import ImageOps # uses bgreat's SPI code; see # raspberrypi.org/phpBB3/viewtopic.php?f=32&amp;amp;t=9814&amp;amp;p=262274&amp;amp;hilit=nokia#p261925 import nokiaSPI noki = nokiaSPI.NokiaSPI() # create display device qr = qrcode.QRCode(version=1, # V.1 QR Code: 21x21 px error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=2, border=1) bg = Image.new('1', (84, 48)) # blank (black) image background while 1: qr.clear() newbg = bg.copy() # copy blank background s = time.strftime('%Y-%m-%d %H:%M:%S') qr.add_data(s) # make QR Code of YYYY-MM-DD HH:MM:SS qr.make() qrim = qr.make_image() # convert qrcode object to PIL image qrim = qrim.convert('L') # make greyscale qrim = ImageOps.invert(qrim) # invert colours: B-&amp;gt;W and W-&amp;gt;B qrim = qrim.convert('1') # convert back to 1-bit newbg.paste(qrim, (18, 0)) # paste QR Code into blank background noki.show_image(newbg) # display code on LCD time.sleep(0.4) # pause before next display
(Convenient archive of all the source: qrclock2.zip, really including bgreat’s nokiaSPI class this time …)
To get all this working on your Raspberry Pi, there’s a fair amount of configuration. The best references are bgreat’s own comments in the thread, but I’ve tried to include everything here.
Enabling the SPI kernel module
As root, edit the kernel module blacklist file:sudo vi /etc/modprobe.d/raspi-blacklist.conf
Comment out the spi-bcm2708 line so it looks like this:#blacklist spi-bcm2708
Save the file so that the module will load on future reboots. To enable the module now, enter:sudo modprobe spi-bcm2708
Now, if you run the
lsmodcommand, you should see something like:ModuleÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SizeÂ Used by spi_bcm2708Â Â Â Â Â Â Â Â Â Â Â Â 4421Â 0
Installing the WiringPi, SPI and other required packages
WiringPi by Gordon is one of the neater Raspberry Pi-specific modules, as it allows relatively easy access to the Raspberry Pi’s GPIO pins. For Raspbian, there are a few other imaging libraries and package management tools you’ll need to install here:sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git sudo pip install spidev sudo pip install wiringpi
Installing the Python QR code library
Finding a library that provided all the right functions was the hardest part here. I ended up using mozillazg‘s fork ofÂ lincolnloop‘s python-qrcode module. mozillazg’s fork lets you use most of the lovely PIL methods, while the original hides most of them. Since I had to do some image compositing and colour remapping to make the image appear correct on the Nokia screen, the new fork was very helpful.
To install it:git clone git://github.com/mozillazg/python-qrcode.git cd python-qrcode/ sudo python ./setup.py install
The tiny 84Ã—48 resolution of the Nokia screen doesn’t give you many options for sizing QR codes. For the time display of the clock, a 21Ã—21 module Version 1 code with two pixels per module and one module margin just fits into 48 pixels. Using a medium level of error correction, you can fit the 19-character message (such as “2013-01-19 18:56:59”) into this tiny screen with a very good chance of it being read by any QR code reader.
(In the video, there’s a much larger QR code that’s a link to this blog post. That’s a Version 7 code [45Ã—45 modules] at one pixel per module and no margin. This doesn’t meet Denso Wave’s readability guidelines, but the Nokia screen has large blank margins which seem to help. It won’t read on every phone, but you’re here at this link now, so you don’t need it …)
Wiring it all up
(Do I really need to say that you’ll be messing around with the inner delicate bits of your Raspberry Pi here, and if you do something wrong, you could end up with a dead Raspberry Pi? No? Okay. Just make sure you take some static precautions and you really should have the thing shut down and powered off.)
You’ll need 8 female-female jumper wires, and also some kind of pin header soldered in (I used right-angled ones). Note that the thick border of the LCD is the top of the screen. These boards are made who-knows-where by who-knows-whom, and there’s a huge variety of labels and layouts on the pins. My one appears to be yet another variant, and is labelled:
This is how I wired it (from comments in bgreat’s code and the GPIO reference):Â LCD PinÂ Â Â Â Â Â FunctionÂ Â Â Â Â Pi GPIO Pin #Â Â Pi Pin Name ============= ============= =============== ============= Â 1 VCCÂ Â Â Â Â Â Â Â VccÂ Â Â Â Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â 3.3 V Â 2 GNDÂ Â Â Â Â Â Â Â GroundÂ Â Â Â Â Â Â 25Â Â Â Â Â Â Â Â Â Â Â Â Â GND Â 3 SCEÂ Â Â Â Â Â Â Â Chip EnableÂ Â 24Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO08 SPI0_CE0_N Â 4 RSTÂ Â Â Â Â Â Â Â ResetÂ Â Â Â Â Â Â Â 11Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO17 Â 5 D/CÂ Â Â Â Â Â Â Â Data/CommandÂ 15Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO22 Â 6 DNK(MOSI)Â Â Data InÂ Â Â Â Â Â 19Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO10 SPI0_MOSI Â 7 SCLKÂ Â Â Â Â Â Â Serial ClockÂ 23Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO11 SPI0_SCLK Â 8 LEDÂ Â Â Â Â Â Â Â BacklightÂ Â Â Â 12Â Â Â Â Â Â Â Â Â Â Â Â Â GPIO18 PWM0
Wire it up, and fire up the program:sudo ./qrclock.py
Yes, code that accesses GPIO needs to be run as root. Pesky, but helps you avoid running code that accidentally scrams the nuclear power station you’re controlling from your Raspberry Pi …