{"id":45,"date":"2020-05-06T22:36:18","date_gmt":"2020-05-06T22:36:18","guid":{"rendered":"https:\/\/samndave.org.uk\/ourdoc\/?p=45"},"modified":"2025-07-10T16:11:35","modified_gmt":"2025-07-10T16:11:35","slug":"microbit-neopixels","status":"publish","type":"post","link":"https:\/\/samndave.org.uk\/index.php\/2020\/05\/06\/microbit-neopixels\/","title":{"rendered":"Micro:bit Neopixels"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"187\" src=\"http:\/\/samndave.org.uk\/wp-content\/uploads\/2020\/05\/neopixel.jpg\" alt=\"\" class=\"wp-image-46\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">NeoPixels are Adafruit&#8217;s brand of individually addressable RGB LEDs. However the name has become common use to describe most kinds of WS2812 (and similar) LED arrays. Each LED can have it&#8217;s colour set individually using a RGB value. This can lead to interesting light and animation displays. The above image is not an Adafruit branded matrix, it was cheaper and has one &#8216;stuck&#8217; LED. Adafruit&#8217;s offerings are generally considered to be higher quality. For this article, all similar arrays will be referred to as Neopixels.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Neopixels can come in a large range of shapes and sizes, rings, strips, arrays, long strings or as individuals. They come with 3 connections, Vcc (3v), GND and Data\/IN. Data connects to any pin which supports PWM. It is not clear from the <a href=\"https:\/\/microbit-micropython.readthedocs.io\/en\/latest\/pin.html\">docs<\/a> if all pins support PWM, though pins 0-3 certainly do. Most units also have output connections for power and data, allowing you to daisy chain units together for large LED displays. Chaining more units together may require the use of external power supplies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Micropython has a <a href=\"https:\/\/microbit-micropython.readthedocs.io\/en\/latest\/neopixel.html\">Neopixel library<\/a>, which has 2-3 simple steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Clear the display (optional)<\/li><li>Configure colour values for each pixel<\/li><li>&#8216;Show&#8217; the display.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following code clears the display, then sets the first three pixels to be red, green and blue. Like most things in computing, counting starts at zero not one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># NeoPixel Demo\nfrom microbit import *\nimport neopixel\n\n# Init a 64 Neopixel display on Pin0\nnp = neopixel.NeoPixel(pin0, 64)\n\n# Clear the display\nnp.clear()\n\n# Set the first pixel to red\nnp&#91;0] = (255, 0, 0)\n# Set the second pixel to green\nnp&#91;1] = (0, 255, 0)\n# Set the third pixel to blue\nnp&#91;2] = (0, 0, 255)\n\n# Show new display settings\nnp.show()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This version of the neopixel library does not allow the adjustment of brightness. The only supported method is to lower the RGB values. Adafruit does have a library which does allow brightness lowering.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pixel numbering<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Different Neopixel shapes have different numbering schemes to determine how your pixels are laid out, the following code will flash each pixel in turn:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># NeoPixel Demo\nfrom microbit import *\nimport neopixel\n\n# Init a 64 Neopixel display on Pin0\nnp = neopixel.NeoPixel(pin0, 64)\n\nfor i in range(64):\n    # Clear the display\n    np.clear()\n\n    # Set the pixel to red\n    np&#91;i] = (64, 0, 0)\n\n    # Show new display settings\n    np.show()\n    sleep(300)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">From this, I could determine the layout on the left of the below image for my 8&#215;8 matrix.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"359\" src=\"http:\/\/samndave.org.uk\/wp-content\/uploads\/2020\/05\/NeoPixel_layout.jpg\" alt=\"\" class=\"wp-image-51\" srcset=\"https:\/\/samndave.org.uk\/wp-content\/uploads\/2020\/05\/NeoPixel_layout.jpg 710w, https:\/\/samndave.org.uk\/wp-content\/uploads\/2020\/05\/NeoPixel_layout-300x152.jpg 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It would be more useful if we could address the pixels using coordinates as demonstrated on the right, especially if we want to do an animation or move a cursor. The red pixel has the coordinates (4,3), pixel number 28. The green pixel is (1,6), pixel 49. The formula p=8*y + x can be used to calculate the pixel number. If we wrap this up in a function, we can address our pixels using coordinates. The following code displays the red and green pixel in the right image.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># NeoPixel Demo\nfrom microbit import *\nimport neopixel\n\n# Init a 64 Neopixel display on Pin0\nnp = neopixel.NeoPixel(pin0, 64)\n\n# Function neoCoord\ndef neoCoord(n, x, y, col):\n    # Accept a neopixel object, x and y values then set the pixel to be that colour\n    # Calculate pixel number\n    p = y*8 + x\n    \n    # Set the pixel\n    n&#91;p]=col\n    \n# Set red and green pixels by coordinate\n# Clear the display\nnp.clear()\n\nneoCoord(np, 4, 3, (128,0,0))\nneoCoord(np, 1, 6, (0,128,0))\n\n# Show new display settings\nnp.show()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You may need to write your own formula in neoCoord depending on your display. It may be rectangular or you could join two 4&#215;4 displays together to make a 8&#215;4 display.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Displaying Images<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The onboard LEDs can be sent an image to display, either using a pre-defined image or defining your own. See the tutorial <a href=\"https:\/\/microbit-micropython.readthedocs.io\/en\/latest\/tutorials\/images.html\">here<\/a>. Wouldn&#8217;t it be nice if we could do the same? With large displays we could use images smaller than the overall display. We tell it where we want the image to appear, what colour and what image. If we define this is a binary bitmap (using 0 and 1 only) and code correctly, we need not worry if the image goes off the &#8216;screen&#8217;. We can use this to produce nice animation effects and call the function multiple times with different colours to produce multi-colour images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">neoImage takes a Image object and prints it in the colour col, at the coordinates specified. It is happy if that prints part of the image (including the starting point) off the screen, as this allows images to scroll on and off the screen. Use negative coordinates to go off the top or left of the screen. The following code includes the image displaying function neoImage along with a short bit of code to scroll a TIE fighter across the screen twice.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Neopixel Images\nfrom microbit import *\nimport neopixel\n\n# Init a 64 Neopixel display on Pin0\nnp = neopixel.NeoPixel(pin0, 64)\n\n# Set the display width and height. We need this for correctly\n# displaying images\ndwidth = 8\ndheight = 8\n\ntie = Image(\"10001:\"\n            \"10101:\"\n            \"11111:\"\n            \"10101:\"\n            \"10001\")\n            \n# Function neoCoord\ndef neoCoord(n, x, y, col):\n    # Accept a neopixel object, x and y values then set the pixel to be that colour\n    # Calculate pixel number\n    p = y*8 + x\n    \n    # Set the pixel\n    n&#91;p]=col\n\n# Display an image on a neoPixel matrix\ndef neoImage(n, px, py, col, img):\n    # n=NeoPixel object, px &amp; py=top left positional coordinates,\n    # col=colour and img is a binary image object\n    # The position may be negative to allow an image to scroll onto\n    # the screen\n\n    # Find dimensions\n    w = img.width()\n    h = img.height()\n    \n    # Set coordinates to start plotting (x and y)\n    x = px\n    y = py\n    \n    # Run through each row of the image as iy (image y)\n    for iy in range(h):\n        # Run through each column in the image as ix (image x)\n        # Reset x to start of row\n        x = px\n        for ix in range(w):\n            # Find the value of that pixel\n            v = img.get_pixel(ix, iy)\n            if(v > 0):\n                # Check the pixel is in the range of the screen\n                # If not, ignore\n                if(x >= 0 and y >= 0 and x &lt; dwidth and y &lt; dheight):\n                    # Set that pixel\n                    neoCoord(n, x, y, col)\n                \n            # Increase x\n            x = x + 1\n        \n        # Move to the next row\n        y = y + 1\n\n    \n# Scroll the image onto the top left and across the screen\nfor i in range(-5,9):\n    # Clear the display\n    np.clear()\n    \n    # Display the image\n    neoImage(np, i, 0, (0,0,80), tie)\n\n    # Show new display settings\n    np.show()\n    \n    # Pause\n    sleep(200)\n    \n# Scroll back on the from the other side at the bottom\nfor i in range(9,-6,-1):\n    # Clear the display\n    np.clear()\n    \n    # Display the image\n    neoImage(np, i, 3, (0,0,80), tie)\n\n    # Show new display settings\n    np.show()\n    \n    # Pause\n    sleep(200)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>NeoPixels are Adafruit&#8217;s brand of individually addressable RGB LEDs. However the name has become common [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-microbit","category-hardware"],"_links":{"self":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":1,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":1188,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions\/1188"}],"wp:attachment":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}