{"id":223,"date":"2015-01-04T23:54:01","date_gmt":"2015-01-05T04:54:01","guid":{"rendered":"http:\/\/fw.hardijzer.nl\/?p=223"},"modified":"2016-12-06T08:51:51","modified_gmt":"2016-12-06T13:51:51","slug":"96x48-full-color-led-matrix","status":"publish","type":"post","link":"https:\/\/fw.hardijzer.nl\/?p=223","title":{"rendered":"96&#215;48 full-color LED Matrix"},"content":{"rendered":"<h2>LED Panels<\/h2>\n<p><iframe loading=\"lazy\" title=\"LEDMatrixHUB75\" width=\"660\" height=\"371\" src=\"https:\/\/www.youtube.com\/embed\/4_Fv_DULgeM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>A few years ago I built a red-only 32 pixels high, 96 pixels wide LED Matrix, and due to all the positive responses I sought out to do it again the year after with a bigger better matrix. I did some research into affordable solutions, and as usual ended up with Chinese vendors. I got my hands on about 10 32&#215;16 RGB LED panels with a 1cm pixel pitch, and a HUB75 connection, quite similar to the <a title=\"ADAFruit LED Matrix\" href=\"https:\/\/learn.adafruit.com\/32x16-32x32-rgb-led-matrix\/\">ADAFruit 32&#215;16 matrix<\/a>. ADAFruit had a bunch of information on them, and there are several other places where they&#8217;re being used, so I figured I&#8217;d give it a shot. I even bought a Digilent Basys 2 FPGA development board, as these boards are apparently best driven by an FPGA, and I was willing to pick that up.<\/p>\n<p>However, when I started working on it initially, I found out that the Basys 2\u00a0did not even have enough memory for 24-bit color, and with my microcontroller of choice at that moment (TI Stellaris) I did not get more than 16-bit color without flickering. While the panels looked fun, with 16-bit color everything still looked very basic, and very limited. Definitely not the neat full-color panels I had in mind. I figured I&#8217;d either have to shell out for a much more expensive FPGA, or buy one of the purpose-built LED drivers from china, both of which were out of my budget for this project. The panels disappeared in a box somewhere, only to be stumbled upon at least 2 year later.<br \/>\n<!--more--><\/p>\n<h2>Project revival<\/h2>\n<p>Seeing as STM32 chips are much more powerful than Arduino&#8217;s or AVRs, they&#8217;ve been appearing on loads of Chinese development boards, and are relatively inexpensive, I&#8217;ve collected quite a few STM32 boards in my &#8220;to play with sometime&#8221; box of electronic gadgets.\u00a0I&#8217;d gotten my first introduction to ARM chips on the TI Stellaris Launchpad, and seeing the rise in popularity of STM32 chips, I was itching to get my feet wet. I&#8217;d been writing some simple LED blinking on an STM32F1 discovery board, played with the DMA, and it was time for something bigger. Going through the box of electronic playthings, I stumbled upon the LED panels, and figured I&#8217;d give them one more try.<\/p>\n<p>Either I&#8217;d get them working this time, or would get rid of them as they were quite big and no use like this anyway.<\/p>\n<h2>Basic driving of a HUB75 LED panel<\/h2>\n<p>MY HUB75 consists of basically two identical parts stacked on top of each other. I&#8217;ll describe one part of 32&#215;8, and you should be able to extrapolate to the 32&#215;16 by yourself \ud83d\ude09<\/p>\n<p>The connection of HUB75 has 6 data pins (R1, G1, B1, R2, G2, B2), 4 row select pins (A,B,C,D), and 3 control pins (CLK, STB\/LAT, OE).<\/p>\n<p>Of these the OE, or Output Enable pin is the easiest, it simply controls if anything lights up at all. If you pull this to ground, LEDs might light up, if you leave it high, nothing wil light up.<\/p>\n<p>Second easiest are the A, B, C, D pins. In my panels, which are described as 1\/8th scan, only the first three of these are used. These pins select which row is active. If for example A, B, and C are all low, the first row might display something, and all other rows are off. If A is high, and B and C are low, the second row might display something, whereas the rest will again be off. You&#8217;re supposed to switch between all 8 rows so fast that to the human eye it seems like they are all on at once.<\/p>\n<p>Next up are the data pins, and the CLK and STB\/LAT pins. Inside the panels are, for each channel, two 16-bit shift registers.Everytime the CLK pin goes high a bit is shifted in, and once the STB\/LAT pin goes high, anything in the shift register is displayed on the LEDs. So let&#8217;s say you shift in 100&#8230;000 on the R1 line, and set ABC to be 000, the top-left LED will light up RED. If you then change ABC to be 100, the top-left LED will turn off again the the one below it will light up.<\/p>\n<p>So, basically, you clock in data, disable the output, latch in the data, set the row selectors, enable the output, display this data for a while, and then do the next row. Ideally you&#8217;d clock in the data for the next row while you&#8217;re displaying the former, but let&#8217;s not get ahead of ourselves.<\/p>\n<p>Back to the bigger 32&#215;16 panel: R1,G1,B1 are for the top half, R2, G2, B2 for the bottom half. Also, the panels have a HUB75 output on the other end that allows you to chain things. So if you chain 2 panels, you&#8217;d have to shift in 64 bits each time.<\/p>\n<h3>More colours: PWM<\/h3>\n<p>At this point you might be wondering &#8220;But hey, you mentioned full-colour, but up until now I&#8217;ve only seen you discuss on or off?&#8221;. That&#8217;s right, to get different brightness values, we should use <a title=\"PWM\" href=\"http:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\">pulse-width modulation<\/a>. So at this point not only should we drive this thing fast enough so you don&#8217;t notice there&#8217;s only one row on at a time, we should do it so fast that we can vary how often a pixel is on, without having the human eye notice.<\/p>\n<h3>More on HUB75 driving<\/h3>\n<p>For more information on how precisely to drive a HUB75 display, the following articles greatly helped me understand it:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.rayslogic.com\/propeller\/programming\/AdafruitRGB\/AdafruitRGB.htm\">Adafruit RGB LED Matrix at Rayslogic.com<\/a><\/li>\n<li><a href=\"http:\/\/bikerglen.com\/projects\/lighting\/led-panel-1up\/\">RGB LED Panel Driver Tutorial<\/a> by Glen Akins<\/li>\n<\/ul>\n<h2>Actually getting it to work: different approaches<\/h2>\n<h3>Bit-banging with naive PWM<\/h3>\n<p>My first goal was to get the panels to display anything.\u00a0A simple loop to just read each pixels R, G, and B values, do a comparison against a looping value from 1 to 255, and shift out a bit. Initially on the STM32F1 Discovery this worked, but I could only get up to 4\u00a0bit grayscale on a single panel without noticeable flickering, while the goal was to get something at least 24bpp on a lot more panels. Switching to the STM32F4 Discovery, and with it increasing the clockspeed from 24mhz to 168mhz, gave me some extra leeway, but at just a single panel 8-bit grayscale\u00a0already gave visible flickering on my phone&#8217;s camera.<\/p>\n<p>There were a couple of problems with this approach:<\/p>\n<ul>\n<li>There was some major ghosting going on between rows. Apparently this is a common problem with these kinds of matrices, and is easily solved by limiting the number of row-switches to a minimum.<\/li>\n<li>6-bit grayscale is nowhere near full-colour.<\/li>\n<li>The processor has a full-time job driving this display, which means that there are no clock cycles left to actually do some animating.<\/li>\n<\/ul>\n<h3>Binary code modulation, precalculating<\/h3>\n<p>The first fix is to switch from naive PWM to binary code modulation. With normal PWM I refresh the display 255 times, and if a pixel was supposed to be 127\/255 lit, I&#8217;d turn it on for the first 127 times, and leave it off for the rest. Binary code modulation is a better fit, instead of 255 equally long display periods, I use just 8. One of let&#8217;s say x, one of 2x, one of 4x, and so on. For a 127\/255 lit LED, we&#8217;d split up the 127 in it&#8217;s binary counterparts: 1 + 2 + 4 + 8 + 16 + 32 + 64, and turn it on in the periods corresponding to those, and off during the others. The LED ends up being lit for the same amount of time, yet with binary code modulation I only have to shift out data 8 times, and switch rows 8 times.<\/p>\n<p>Furthermore, seeing as now for each row there are only 8 different streams being pushed out, I could precompute them, thus cutting the inner loop of shifting the data outback from about 20 clockcycles per bit to 5.<\/p>\n<p>This finally pushed me near 8-bit grayscale, but 9-bit unfortunately still gave flickering on my phone camera (but barely noticeable to my eye).<\/p>\n<p>Thoughts on this method:<\/p>\n<ul>\n<li>The ghosting wasn&#8217;t nearly as bad, but still visible. Something that I can live with, but I&#8217;d rather get rid of it at some point.<\/li>\n<li>While 8-bit grayscale sounds good, seeing as your monitor is 8-bit grayscale too, in reality it isn&#8217;t at all. The human eye responds to brightness logarithmically, and normally your monitor adjusts for that. Our duty-cycle algorithms don&#8217;t, which means we&#8217;ve got only a few different intensities in the lower end, and a lot in the higher end. We should aim for at least 9-bit grayscale, to get something resembling full colour.<\/li>\n<li>Due to the timing, there are now chunks of time where the processor isn&#8217;t doing anything, but as it&#8217;s scattered all over the place, it&#8217;s hard to do anything useful with it.<\/li>\n<li>The time of the lowest significant bit was at this point limited by the time it took to shift out an entire row.<\/li>\n<\/ul>\n<h3>FSMC + DMA + Output Enable<\/h3>\n<p>To free up the processor, it was time to use Direct Memory Access: a peripheral that can pump a block of memory to a peripheral, without the processor having to do anything. There&#8217;d be a DMA interrupt (whenever it&#8217;s ready copying the data), and a timer interrupt, and once these both have been fired the processor would do a little bit of work to set up the next bit of data to shift out. Apart from that, it was free to do anything it wanted, like create a nice animation.<\/p>\n<p>I could use the DMA to write directly to the GPIO, but that would mean embedding the clock signal in the data, and doubling the memory required. Luckily the STM32F4 has an FSMC , or Flexible Static Memory Controller , peripheral. Most memory chips have a few address lines, some data lines, and a write and read strobe. Whenever you toggle the read strobe, it&#8217;ll read whatever is at the address pointed to by the address lines, and put it on the data lines. Whenever you toggle the write strobe, it&#8217;ll write whatever is on the data lines to the address on the address lines. The FSMC was made specifically to drive memory like this, by mapping an address range to it. The STM32&#8217;s main processor can just write or read from an address, and the FSMC will talk to the memory for it. For our matrix, we need a few data lines, and a clock pulse, which maps nicely on the data lines and the write strobe. So we can set up the FSMC, write a couple of bytes to the address range for it, and the FSMC will shift it out for us and take care of the clock signal. Nice!<\/p>\n<p>Another little trick I thought up is to actually use the Output Enable pin for something. Shifting out the data takes a set amount of time, and initially I thought that with the binary code modulation the least significant bit couldn&#8217;t be shorter than the time\u00a0that shifting out took. However,\u00a0what if we could turn on the row for only a fraction of the time it takes to shift out the data. Yes, the display won&#8217;t be at absolute maximum brightness, but it would allow us to make the time for the LSB much smaller, and as such for the entire refresh rate to be much smaller. It wasn&#8217;t difficult to wire\u00a0the timer counter-compare pin to the output enable pin on the HUB75, and the results were stunning. I could finally do 10-bit grayscale!<\/p>\n<p>Some setbacks, though:<\/p>\n<ul>\n<li>The FSMC was very very fickle. The write strobe was idle-high, while the clock was supposed to be idle-low. Usually it worked, but whenever I would do anything over any other pins, like let&#8217;s say receive data over UART, there would be major glitches appearing.<\/li>\n<li>While I could theoretically now set the LSB-duration really small and allow for more bit grayscale, the lower I set it, the more the ghosting reappeared.<\/li>\n<\/ul>\n<h3>Final method: DMA + GPIO + Loop fix-up<\/h3>\n<p>While in theory the FSMC was perfect for this, and maybe with a little inverter on the clock pin it still might, I still opted to ditch it in favour of just DMA&#8217;ing to the GPIO pins. This doubled the memory consumption, but seeing as it&#8217;s much much more stable, I think that&#8217;s worth the trade-off.<\/p>\n<p>Finally I also re-organized the loop. Initially for each bit in the bit code modulation, I would loop through all rows, each time introducing a teeny tiny bit of ghosting on the next row. Instead, I opted to loop over the rows, and then do all the bits at once. This cut the number of times I looped over the rows drastically, and the ghosting is now no longer visible with the naked eye. I&#8217;m sure it&#8217;s still there, but at least now it&#8217;s so minor it no longer bothers me.<\/p>\n<h2>Displaying something useful<\/h2>\n<p>At this point the display would function fine as a sort of digital photo frame, seeing as I could display an image and keep it that way, but actually displaying video was not really possible. Seeing as almost everything at this point is done in DMA, there are plenty of clock cycles to generate images, but that&#8217;s no what I wanted: I wanted to be able to drive it over the network.<\/p>\n<h3>Raspberry Pi + SPI<\/h3>\n<p>The first approach was using a raspberry pi. I set up a simple double-buffer on the STM32F4, and set up DMA to copy anything it received over SPI to the buffer, and once it was filled to actually display that buffer. The raspberry pi would then do all the precalculation, and simply spit it out over SPI. This worked great, but there was one setback: it was wired. Why would I want to run an extra ethernet cable to my LED matrix, when we live in a world with WiFi?<\/p>\n<h3>OpenWRT on TP-Link WR703N + USB<\/h3>\n<p>Luckily I had a cheap TP-Link WR703N router available which would be up to the task. It was a bit slower than the raspberry pi though, so it probably wouldn&#8217;t be able to do all the precalculation too. Fortunately the STM32F4 has lots and lots of clock-cycles left over to do that for us. Also the TP-Link WR703N has a USB port, and the STM32F4 discovery board has a nice USB port directly to the microcontroller. It&#8217;s only Full-speed (USB1.1), but that should be enough right?<\/p>\n<p>Nope, apparently it isn&#8217;t. There is apparently way too much overhead on the USB port to get a halfway decent framerate, and it required a USB2.0 USB hub inbetween as the TP-Link&#8217;s USB port was horrible at driving USB1.1 devices. Oops<\/p>\n<h3>Uart output on the TP-Link WR703N<\/h3>\n<p>There is another way to pipe out data on the TP-Link though: UART! It&#8217;s hidden inside, so you&#8217;ll have to manually solder to it, but it should be up to the task.<\/p>\n<p>After some soldering I could get data out, but it was limited to 3megabaud. Not quite good enough for fluid framerates \ud83d\ude41 The weird thing is that apparently the serial clock on this thing runs at 25mhz, so I had no clue why it would crap out above 3mbaud. After some searching I found that in the drivers it was artificially limited, probably because noone ever uses a serial port over 3megabaud, and it might not be able to reproduce baudrates above that accurately. After a quickly patching <a href=\"https:\/\/github.com\/opennetworklinux\/linux-3.8.13\/blob\/master\/drivers\/tty\/serial\/ar933x_uart.c\">the driver<\/a> and recompiling OpenWRT, I could get it to work on higher baudrates. Seeing as the serial clock was running at 25mhz, I figured 5megabaud would be easy for it to generate, and indeed the signal is rock-solid!<\/p>\n<p>I wrote a quick little program that would get Art-Net data over UDP, and wired up <a href=\"http:\/\/www.solderlab.de\/index.php\/software\/glediator\">GLEDiator <\/a>to pump out some data. It worked like a charm!<\/p>\n<h2>Final hardware<\/h2>\n<p>The final hardware consists of:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.st.com\/web\/catalog\/tools\/FM116\/SC959\/SS1532\/PF252419\">STM32F4 Discovery board<\/a>, about\u00a0\u20ac15<\/li>\n<li><a href=\"http:\/\/www.dx.com\/p\/tp-link-tl-wr703n-mini-3g-2-4ghz-802-11b-g-n-150mbps-wireless-router-blue-158552#.VKndeCvF8Ys\">TP-Link WR703N<\/a>, about\u00a0\u20ac20<\/li>\n<li><a href=\"http:\/\/www.aliexpress.com\/item\/P10-Indoor-1-8-Scan-SMD3528-3in1-RGB-Full-color-LED-display-module-320-160mm-32\/1615047851.html\">9 HUB75 32&#215;16 RGB 10mm pitch panels<\/a>, about\u00a0\u20ac18 each (incl shipping)<br \/>\n(note: This is not the seller I bought them from. I was unable to find the seller that originally sold them to me)<\/li>\n<li><a href=\"http:\/\/www.dx.com\/p\/5v-20a-iron-case-power-supply-silver-ac-110-220v-124499\">A cheap\u00a05v 20A power supply<\/a>, about\u00a0\u20ac17<br \/>\n(note: I did not double-check if this is enough current for the entire display to light at once!)<\/li>\n<\/ul>\n<p>Compared to the <a href=\"http:\/\/www.adafruit.com\/product\/1453\">dedicated hardware offered by adafruit<\/a> for $300, I think my solution for less than a fifth of that (\u20ac45) is pretty good \ud83d\ude42<\/p>\n<p>Furthermore any computer or laptop with a network interface can drive this without needing extra\u00a0PCI cards or DVI ports.<\/p>\n<h2>Final code<\/h2>\n<p>All code can be found on my <a href=\"https:\/\/github.com\/Frans-Willem\/LEDMatrixHUB75\">Github repository<\/a>, with instructions on how to <a href=\"https:\/\/github.com\/Frans-Willem\/LEDMatrixHUB75\/tree\/master\/Documentation\">compile and wire everything<\/a>. Furthermore in the utils directory there is a file to generate a <a href=\"http:\/\/www.solderlab.de\/index.php\/software\/glediator\">GLEDiator<\/a> Art-net patch file, and a file to adjust the matrix parameters (brightness and gamma).<\/p>\n<p>I&#8217;ve licensed all code with an MIT license, so feel free to reuse it in it&#8217;s entirety or in parts for your own projects. Do note that I would highly appreciate it if you sent me a small e-mail about your project if you do, though!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LED Panels A few years ago I built a red-only 32 pixels high, 96 pixels wide LED Matrix, and due to all the positive responses I sought out to do it again the year after with a bigger better matrix. I did some research into affordable solutions, and as usual ended up with Chinese vendors. &hellip; <a href=\"https:\/\/fw.hardijzer.nl\/?p=223\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">96&#215;48 full-color LED Matrix<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-electronics"],"_links":{"self":[{"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":10,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fw.hardijzer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}