{"id":153,"date":"2013-08-20T13:43:55","date_gmt":"2013-08-20T13:43:55","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/pi\/?page_id=153"},"modified":"2013-08-23T16:46:15","modified_gmt":"2013-08-23T16:46:15","slug":"ledborg-ultra-bright-rgb-led-add-on-board","status":"publish","type":"page","link":"https:\/\/blog.soton.ac.uk\/pi\/ledborg-ultra-bright-rgb-led-add-on-board\/","title":{"rendered":"LedBorg \u2013 Ultra Bright RGB LED add-on board"},"content":{"rendered":"<p><!--\nPRE.ctl { font-family: \"Lohit Hindi\",monospace; }P { margin-bottom: 0.21cm; }CODE.ctl { font-family: \"Lohit Hindi\",monospace; }A:link {  }\n--><span style=\"font-family: Times New Roman,serif\">The easiest way to get started with the installation and initialising the RGB LED is to follow the instructions on this page: <a href=\"http:\/\/www.piborg.com\/ledborg\/install\">http:\/\/www.piborg.com\/ledborg\/install<\/a>. <b>(!!!) <\/b>Please make sure you attach the LedBorg onto the Raspberry Pi when it is not running, because otherwise the Pi will reset and this might be harmful for the device. The reason why it`s resetting is the presence of a decoupling capacitor between +5V and GND, which initially charges from the 5V power supply and causes the voltage to drop slightly, but enough to make the Pi not draw enough power which makes the device reset. <\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\">I found the light coming from the LED quite strong to watch with direct eye, instead cover the LED with something thin which will ensure the colour can still be seen (like a piece of paper\/plastic) but will diminish the light intensity. Note that, even though at the end of the installation you get a message saying \u201cLedBorg should now be green\u201d, you might get a different colour or even no colour on your LED (I got no colour) depending on the value stored in the \u201cledborg_bootcolour\u201d found in your home directory. You can manually change this by typing <b>echo \u201cRGB\u201d &gt; \/home\/pi\/ledborg_bootcolour <\/b>and replacing <b>RGB <\/b>with your desired lev<span style=\"font-family: Times New Roman,serif\">el. The way setting the <\/span><span style=\"font-family: Times New Roman,serif\"><b>RGB <\/b><\/span><span style=\"font-family: Times New Roman,serif\">level works is explained at the link above, in the <\/span><span style=\"font-family: Times New Roman,serif\"><b>Usage <\/b><\/span><span style=\"font-family: Times New Roman,serif\">section. <\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><b>Examples<\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\">There are few simple examples on the piborg website which can get you started with a sequence of lights or let you adjust the light from the keyboard directly. The three examples we will look at can be run with the following commands (in terminal):<\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><b>cd ~<\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><b><code><span style=\"font-family: Times New Roman,serif\">wget -O *.py http:\/\/www.piborg.org\/downloads\/ledborg\/*.txt<\/span><\/code><span style=\"font-family: Times New Roman,serif\"><br \/>\n<\/span><code><span style=\"font-family: Times New Roman,serif\">chmod +x *.py<\/span><\/code><span style=\"font-family: Times New Roman,serif\"><br \/>\n<\/span><code><span style=\"font-family: Times New Roman,serif\">~\/*.py<\/span><\/code><\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">making sure to replace \u201c*\u201d with the following:<\/span><\/code><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">1) <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">random-colours <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\"> (this will open up a script that picks random colours at regular intervals and displays them on the LED)<\/span><\/code><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">2) <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">sequence <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\">(script to run a preset sequence of colours at a regular interval)<\/span><\/code><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">3) <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">user-colours <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\">(script providing a user menu in which the user can manually adjust the light that is displayed on the LED).<\/span><\/code><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><b><code><span style=\"font-family: Times New Roman,serif\">Pick up colour using a Camera<\/span><\/code><\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">The Raspberry Pi Camera Board described in an earlier post on this blog can be used to capture images which will then be used to <\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">change the colour of the LedBorg accordingly. The code loads the images into the program, focuses on the center of the image and uses some image processing tools to convert the colour into LedBorg code and set the LedBorg to that colour. <\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">The program called ChameleonPi can be found on the piborg website <\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">at<\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">: <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">piborg.org\/chameleonpi<\/span><\/code><code><\/code><\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">To get this program working, first install the python imaging libraries: <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">sudo apt-get instal<\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">l<\/span><\/code><code><span style=\"font-family: Times New Roman,serif\"> python-imaging. <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\">Now create a new blank python script with the command <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">sudo nano ChameleonPi.py, <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\">and copy paste the following code in that script:<\/span><\/code><\/span><\/p>\n<pre><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">#!\/usr\/bin\/env python<\/span><\/code><\/span>\r\n# coding: latin-1\r\n\r\n# Import library functions we need\r\nimport time\r\nimport os\r\nimport threading\r\nfrom PIL import Image\r\n\r\n# Setup for ChameleonPi\r\nimageFile = '\/tmp\/chameleon-image.jpg'              # Image file to read in for processing\r\nimageCommand = 'raspistill -t 0 -o ' + imageFile    # Command used to create image file\r\ninterval = 1                                        # Seconds to wait between updates\r\n\r\n# Set the LedBorg colour by LedBorg code\r\ndef SetColour(colour):\r\n    LedBorg=open('\/dev\/ledborg','w')\r\n    LedBorg.write(colour)\r\n    LedBorg.close()\r\n\r\n# Convert a single 0-255 channel into an LedBorg 0-2 code\r\ndef ChannelToCode(c):\r\n    if c &lt; 85:\r\n        return '0'\r\n    elif c &lt; 171:\r\n        return '1'\r\n    else:\r\n        return '2'\r\n\r\n# Set the LedBorg colour by 0-255 RGB values\r\ndef SetRgb(r, g, b):\r\n    ledCode = ChannelToCode(r) + ChannelToCode(g) + ChannelToCode(b)\r\n    SetColour(ledCode)\r\n\r\n# Class for the user interaction thread\r\nclass WaitForUser(threading.Thread):\r\n    # The code which will be run when the thread is started\r\n    def run(self):\r\n        global waiting\r\n        # Wait for the user to press enter\r\n        tempString = raw_input()\r\n        # Set the running flag and finish\r\n        print 'Please wait...'\r\n        waiting = False\r\n\r\n# Setup status flags\r\nglobal waiting\r\nwaiting = True\r\n\r\ntry:\r\n    # Setup a thread to wait for user input\r\n    print 'Press ENTER to end the program'\r\n    WaitForUser().start()\r\n    # Run until interrupted\r\n    while waiting:\r\n        # Call the external command to generate a photo\r\n        os.system(imageCommand)\r\n        # Read in the photo\r\n        photo = Image.open(imageFile)\r\n        # Crop out the center section\r\n        width, height = photo.size\r\n        x1 = int(width * 0.4)\r\n        x2 = int(width * 0.6)\r\n        y1 = int(height * 0.4)\r\n        y2 = int(height * 0.6)\r\n        cropPhoto = photo.crop((x1, y1, x2, y2))\r\n        # Average the cropped image down to a single colour\r\n        tinyPhoto = cropPhoto.resize((1,1), Image.ANTIALIAS)\r\n        r, g, b = tinyPhoto.getpixel((0,0))\r\n        # Scale RGB such that the highest channel is fully on\r\n        scale = 255.0 \/ max(r, g, b)\r\n        r *= scale\r\n        g *= scale\r\n        b *= scale\r\n        # set the LedBorg colour\r\n        SetRgb(r, g, b)\r\n        # Wait a while\r\n        if waiting:\r\n            time.sleep(interval)\r\n    print 'All done :)'\r\n    SetColour('000')\r\nexcept KeyboardInterrupt:\r\n    # CTRL+C exit, turn off the LedBorg\r\n    print 'Terminated'\r\n    SetColour('000')<\/pre>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">Save the python script with <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">Ctrl+x<\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\"> without changing the name and make sure you give the right permission by making the file executable, typing the following command: <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">sudo chmod +x ChameleonPi.py<\/span><\/code><\/b><\/span><\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><code><span style=\"font-family: Times New Roman,serif\">Make sure you have the LedBorg connected and working and also the camera board connected and working (see <a href=\"http:\/\/blog.soton.ac.uk\/pi\/modules-available\/raspberry-pi-camera-board\/\">http:\/\/blog.soton.ac.uk\/pi\/modules-available\/raspberry-pi-camera-board\/<\/a>). Now open up the program by typing <\/span><\/code><b><code><span style=\"font-family: Times New Roman,serif\">.\/ChameleonPi.py <\/span><\/code><\/b><code><span style=\"font-family: Times New Roman,serif\">and see how it works by pointing the camera close to different objects of different colours. The video from the piborg website above uses a Rubick`s cube but you can use any <\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">slightly uniformly coloured <\/span><\/code><code><span style=\"font-family: Times New Roman,serif\">object to give good results. <\/span><\/code><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easiest way to get started with the installation and initialising the RGB LED is to follow the instructions on this page: http:\/\/www.piborg.com\/ledborg\/install. (!!!) Please make sure you attach the LedBorg onto the Raspberry Pi when it is not running, because otherwise the Pi will reset and this might be harmful for the device. The &hellip; <\/p>\n<p><a class=\"more-link block-button\" href=\"https:\/\/blog.soton.ac.uk\/pi\/ledborg-ultra-bright-rgb-led-add-on-board\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":84718,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-153","page","type-page","status-publish","hentry","nodate"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/users\/84718"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":3,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/153\/revisions"}],"predecessor-version":[{"id":167,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/153\/revisions\/167"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/media?parent=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}