Tuesday, 12 February 2019

Painting with Light

You can see the basic idea on the right there - you have a string of individually addressable LED pixels and you use them to display a bitmap image, one column at a time whilst you take a long-exposure photograph.  The result is an image that seems to hang in the air.  You can just see the white stick in my hand which has a string of 144 APA102 LED pixels..

What's an LED pixel?  it's actually 3 LEDs, a red one, a green one and a blue one plus a clever chip which can control the brightness of each one independently, based on a digital data stream.  This usually works something like this; each LED can have one of 256 brightness levels, i.e. brightness is represented by an 8-bit number, so the controller chip needs 24 bits of information to set the pixel's colour.  The chip has two sets of registers, one to store the colour being displayed and a second to temporarily store incoming data.  Once the chip has received its 24 bits, it simply passes any more data on to the next chip, until data stops being sent.  When data stops being sent, the incoming data is copied to the display registers and the pixel changes colour.  That way, all the pixels in a string can change colour at the same time.

A variety of LED pixels are available on the market, some easier to drive than others.  The very inexpensive ones (e.g. WS2812) need data to be sent at a very precise rate with no interruptions and so are very difficult to drive.  The APA102, which I've used here is much easier to drive - data can be sent at at any speed from a few KHz up to several MHz.  The data stream is deemed to have finished if nothing is sent for about half a millisecond, which is quite a long time for a 32-bit microcomputer clocked at around 1GHz.


The box, and what's inside

So, how are these pixels being controlled?  Over on the right you can see the bottom end of the light-painting stick. The yellow box contains a battery, a Raspberry Pi Zero (would have used a Pi Zero W, but they weren't around yet when I built this), a WiFi adapter and a circuit board that shuts off the power when the Pi has been shutdown.  There are also two LEDs and two small button switches.  The white button switches on the power and the red one shuts down the Pi which then switches off the power.  There's a red LED that lights up when the Pi is shutting down and a green one which flashes when it's booted up and ready to go.  There is a third switch on the handle, which triggers the image display.  Press and release that, and an image is displayed once, keep it pressed and the image will display repeatedly.




In this picture you can see a few of the LED pixels.  APA102 combines the three LEDs and the controller into one 5x5mm chip, so the pixels can be packed quite closely.  There is a version in a 2x2mm package, but they're a bit harder to find, plus, at that density it gets quite expensive producing a decent sized pixel string.  You can also see the battery, comprising three 18650 lithium ion cells connected in series.







Here's a close-up of the circuit board.  Raspberry Pi uses 3.3V logic levels but the APA102 needs 5V logic levels.  The little black chip on the right of this bit of stripboard provides the necessary level shifting.

In the centre of the board are two of the MP1584 DC-DC converters I mentioned in my previous post about the PiFI.  These step down the battery voltage from the 12.6V  of the fully charged lithium cells to 5V. One of them supplies the Pi Zero and the level shifter, the other powers the LED string. I used two of them to spread the load a bit and to isolate the Pi's power supply from voltage fluctuations due to the LED string changing brightness.  144 pixels can cause quite a large current drain at 30mA per pixel.  Fortunately, full white on all the pixels doesn't happen often, or I'd be worried about the survivability of the converter.  


Circuit description

The components on the left of the stripboard control the switching off and on, the schematic is shown  here.  Here's how that works.  S1 is the white button - press that and Q1 will be biased into conduction which will pull the gate of M1, a p-channel MOSFET, down below the supply rail voltage making it conduct. This will supply power to the external circuit (our Pi Zero) and also maintain the base-emitter voltage of Q1 so that the circuit latches on.

Pressing S2 sends a signal to one of the Pi's GPIO pins.  That pin is monitored by a python script running in the background, when it goes high, the script sends another GPIO pin, connected to the "shutdown now" input high then it issues a shutdown command.  When the "shutdown now" input goes high, that momentarily increases the base voltage of Q1, which is already conducting so this doesn't make any difference now.  C3 will now discharge through R4 and the base of Q1, returning the base voltage to normal.  When the Pi has finished shutting down, all its GPIO pins will go low, allowing C3 to pull the base voltage of Q1 low enough to shut it off.  When Q1 shuts off, the voltage at the gate of M1 will rise to the point where M1 will shut off, disconnecting the Pi from the battery and preventing the circuit from latching on again - in other words, the Pi will have switched itself off.

Software

The Pi Zero is running Raspbian Jessie Lite.  It is configured to act as a WiFi access point using hostapd with dnsmasq performing the twin duties of DHCP server and DNS.  There is also a Samba share.  You can't connect to the internet this way, and that was never my intention. This simply provides a way to upload pictures to display and to change which one will be displayed.  The Samba share allows file management from a mobile device, WiFi connection allows a command line via SSH.

Two python scripts are run at startup - one runs in the background and checks for the shutdown button being pressed, the other handles the displaying of images.

Displaying images

The program for displaying images  is a python script which will only ever display one image - an image called "showthis.png".   The program loads this when it starts, then waits for the trigger button to be pressed before displaying it one column at a time  on the pixel string.  Once it has been displayed it will be re-loaded.  This provides a simple way to  change the image to be displayed - rename or copy the one you want to display to "showthis.png" and press the trigger once to 'flush out' the old image, and load the new one.

Generating Images

Not a lot of point having this if you have nothing to display.  I use a vector drawing program such as Inkscape on Windows or Linux machines, Draw+ or Vector on RISCOS systems and then either screen grab the bit I want or export the drawing as a bitmap.  In either case, the result is a bitmap image that can then be resized so that it is 144 pixels high (because that's how many pixels are in the LED string) and uploaded to the Pi Zero via WiFi.



Sunday, 10 February 2019

PiFi gets a Pi-3

Having noted the speed improvement upgrading from a Pi B to a Pi 2, it seemed worthwhile to see what improvement a Pi 3 would make.   The performance of this was quite usable with a Pi model B and much quicker with a Pi 2. Now, with a Pi3, the response is very slick indeed.  I can't tell if the slight delay in updating the server's web page is down to the server or the browser. 

At the same time, I thought I'd see how another DAC board behaved, since they're hardly prohibitively expensive.  This time, I've gone back to HiFiBerry, with their DAC+ PRO.  Sounds excellent to my ears, and a bargain at £32 or so, but then so did the IQ Audio Pi DAC+ previously used so I can heartily recommend either of those to anyone wanting to build a Raspberry Pi based audio player. 

Final change is the addition of a small DC-DC converter so that it can be run from a 12V supply.  It's the little board you can see nearly edge-on to the right of the green button.  I have lots of 12V supplies, so this is a good way to put one of them to good use.   The converter is a little board based on the MP1584 chip.  It works at 1.5MHz so can be very tiny and still have very little ripple, and claims to be good for 3A, though I wouldn't want to run it at that current for long. I took a gamble of buying a lot of 10 of these, (which worked out at <50p each!) from China.  I've used several of them now, and so far they have all worked. They seem to be a good option for powering a Raspberry Pi Zero from batteries - I'll be posting something about that soon. No, really, I made a new-year resolution to update this blog more often.




Monday, 16 March 2015

RiscPiC - a bit late, but here it is.

2014 saw the 20th anniversary of the launch of Acorn's RiscPC, and to celebrate the event I fired one up.  It still worked, and still didn't thrash its hard disc just because I asked it to open a menu, which I was able to do without moving the mouse pointer all the way to the top of the screen somewhere.  A subseqent conversation with other members of the Bristol RISCOS user group, in the pub, naturally, led to an idea - why not put a Raspberry Pi, running RISCOS into a case that looks like a little RiscPC?.  Both myself and Vince Hudd decided to tackle the project,  You can buy Vince's professionally 3d-printed designs at most RISCOS shows, and soon from riscpic.co.uk.  I opted to use my own, rather basic "glorified glue gun" type printer and try for something more like a scale model of a RiscPC.  Here's my first attempt, sat atop a real one.  The scale is 2:5


The case is in 2 parts plus lid and front flap. The front overhanging bit slots vertically into the main case.  It seemed easier to do it that way rather than printing the whole thing with huge amounds of support material to remove.  Sadly the fitting turned out to be not quite right and the sideways pressure has split the main case - you can see that on the left-hand side.  Also, although the front flap does open, it doesn't open all the way.  Inside is a Raspberry Pi, SATA-USB adaper, 120GB SSD, USB hub with added power from which the sockets have been removed and remounted on the front and rear (2 behind the front flap) and a power distribution circuit board.

I'm now on the 3rd prototype.  I'm not sure I can regard it as finished, but I'm unlikely now to make another.  This time I did do the box in one piece and had great fun (!) removing the support material. The front flap now opens fully,
Flap open.  Note the SD card slot on the side.
Flap closed



Here's what's inside.  It all works rather well.  Raspberry Pi (model B) is on the left, SSD on the right, power distribution circuit (with the fuseholder) at the top.  USB hub is above the Pi's network port.  Network cable goes in through a big hole in the case rear then has to bend rather sharply to get into the socket.  That's not ideal, but prevents the need for another big hole in the side.

Sunday, 15 March 2015

More PiFi Updates.


With the recent launch of the Raspberry Pi 2, with it's 4-core processor and more memory, I had to see if it would make Logitech Media Server run a bit more snappily.  Using it with the PiFi also meant getting a new DAC board, since the original HiFiBerry that I have won't fit the pins of the new Pi2 layout (which is the same as the B+).  I decided to try the IQaudIO Pi-DAC+ just to be different.  I've no complaints about the HiFiBerry I've been using, and I've no reason to believe that the latest offerings from them are inferior to the IQaudIO device (they use the same chip at least), I just thought it was worth trying the competition since the cost of the thing wasn't exactly going to leave me starving if I hated it and had to buy something else.  I don't hate it, it sounds lovely.  Better than the original HiFiBerry (which uses a different chip to the current ones, so don't conclude anything from that) and better than my CD player.

I've also done a new cradle for the Pi2 and SSD - in tasteful mauve.  This also meant printing another back for the glowing badge - there it is at the top of this post..


The two buttons are also new.  The green one switches the power on, the red one asks the Pi2 to shutdown nicely.  When the shutdown finishes, the power goes off automatically.

You can see the circuit board a bit better in this photo.  It communicates with the Pi2 by means of a couple of GPIO pins.  The shutdown request generated by the red button goes to a pin which is monitored every 5 seconds by a little Python script.  If that pin is high then it sends another pin high, and issues a "sudo shutdown -h now".  The power control circuit ignores that event, but, when shutdown has finished the Pi2 sends all the GPIO pins low and the falling edge is used to shut off the power.  If any of my legions of followers request it, I'll post some more detail about that.  Might just do it anyway.


Meanwhile, the back panel has also changed - I've used these two connectors from Maplin.  I'm considering splashing out on some Lumberg 0332 series DIN connectors, but for now these are much better (IMO) than the standard coaxial power connectors (that's the 2-pin one on the right) and those ghastly RCA phonos.  You noticed, perhaps, that I removed them from the DAC board?  Audio out is now from a right-angle PCB header underneath the board to the 3-pin socket on the left in this photo.

The photo below shows a side view, and you can see the two thin wires (red & white) which connect the power control board to the GPIO pins.  Rather than mess with more header pins, I simply soldered them on and used a PCB header on the power control board to allow me to disconnect them.

On the software front, it's much the same.   The major difference, and a vast improvement, is that the SSD now has 2 partitions - its still mostly a big NTFS partition to store the music files, but there's now a small (4GB) ext4 partition for the Linux rootFS - i.e. the operating system.  Once the basic kernel is loaded from the SD card, the rest of the boot sequence is done from the rootFS partition on the SSD.   The NTFS partition is the first one, so the USB lead to the SSD can be plugged straight into a Windows PC for rapid file transfer.  Thanks to Sam Hobbs for the tutorial on that subject.  I've moved the DHCP and DNS duties for my home network onto another Pi (well, I had a spare now). There is still a DHCP server and DNS on the PiFi which now only looks at the WiFi interface as it is needed to connect wireless devices to control the PiFi when no wired network is available.

Finally, to answer my initial query, yes, the Pi2 is a very worthwhile improvement.  Not only does it boot up much quicker and scan the library a lot quicker, it responds noticeably more snappily to navigation requests, especially when using folder navigation.



Sunday, 2 March 2014

Pi-Server is dead, long live Pi-Fi

With the addition of a HiFiBerry DAC board from Crazy Audio and a USB WiFi adapter, my Pi-Server is now a stand-alone Hi Fi audio source which provides it's own wireless network so that it can be controlled by anything with  WiFi and a web browser.  I've also replaced the USB flash drives with an SSD.

I've written the whole thing up in a bit more detail, although there's nothing about the software set-up process (might add that if anyone asks).  It's on my ancient blueyonder personal web page here, which also shows an intermediate stage with a USB hub and 4 flash drives that I never got around to mentioning before:

Erm, edit: Blueyonder (i.e. Virgin Media) have scrapped their users' personal web sites, so this no longer exists.  -I.N. July 2020.

http://www.ian-nic.pwp.blueyonder.co.uk/PiFi

Oh, and it now has a new, glowing badge.


Monday, 27 August 2012

Pi-Server has a case. Sort of


I happened to have some transparent plastic around, salvaged from a water bath that was no longer needed, so I made this.  It's not the neatest job in the world, but it keeps the dust off and is well ventilated.  I did intend from the outset to leave a small gap at the top, it's not evidence of poor measuring leading to insufficient room for the USB flash drives..

Thursday, 23 August 2012

Pi-Server Update

Still haven't made a cover for it, but I did  buy a couple of Sandisk Cruzer Slice USB flash drives - 64GB each.  With their outer covers removed they will fit into the USB ports of the Raspberry Pi, and are able to store all my music between them with a few Gigs left over - plus whatever's free on the SD card.  No moving parts, no laser modules to fail and all controllable from a handy little remote, a tablet PC, smartphone or anything that will run a web browser.