{"id":166,"date":"2014-02-04T17:39:31","date_gmt":"2014-02-04T17:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=166"},"modified":"2018-02-06T15:13:38","modified_gmt":"2018-02-06T23:13:38","slug":"raspberry-led","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/raspberry\/LED\/","title":{"rendered":"Creating a &#8220;Blinking LED&#8221; project for Raspberry PI"},"content":{"rendered":"<p>This tutorial demonstrates how to attach a LED to the expansion connector on your Raspberry PI and to make it blink with a simple C++ program.<\/p>\n<ol>\n<li>First of all, in order to programmatically switch a LED on and off we need to connect it between a general-purpose input\/output pin (GPIO) and the ground. Then we need to change the state of the pin between 1 and 0 to switch the LED on and off. The basic schematics is depicted below:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/schematics.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/schematics.png\" alt=\"schematics\" width=\"319\" height=\"126\" \/><\/a><\/li>\n<li>Raspberry PI has an extension header called P1 that contains 26 pins. Not all of the pins can be controlled programmatically, so we will refer to the <a href=\"http:\/\/www.raspberrypi.org\/wp-content\/uploads\/2012\/10\/Raspberry-Pi-R2.0-Schematics-Issue2.2_027.pdf\"> schematics<\/a> to find out which ones can be controlled:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/find-led.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/find-led.png\" alt=\"find-led\" width=\"700\" height=\"453\" \/><\/a><\/li>\n<li>According to the schematics above, pin #13 corresponds to a software-controllable GPIO27 and pin #14. Here&#8217;s how this looks on the board:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/board.JPG\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/board.JPG\" alt=\"board\" width=\"700\" height=\"400\" \/><\/a><\/li>\n<li>\u00a0We will now connect our external LED with a<br \/>\nresistor to pins 13 and 14 of the connector P1. This<br \/>\nrequires soldering together a LED, a resistor and a pair of<br \/>\nconnectors compatible with P1 pins:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led.jpg\" alt=\"led\" width=\"570\" height=\"220\" \/><\/a><span class=\"warning\">Warning! Mishandling things while or after soldering may burn your Raspberry PI and even your PC connected to it via USB! We assume no liability for any damages related to following or misfollowing this tutorial. If you still feel like experimenting, <strong>do not solder anything on a circuit that is currently powered or connected to a powered device<\/strong>. <strong>Be careful when connecting things, as short-circuiting certain pins will restart (and might even burn) our board and any devices attached to it. Use an expandable USB phone charger instead of your actual computer as the power source.<\/strong><\/span><\/li>\n<li>Turn off your Raspberry PI board and carefully connect the LED to the P1 header. Ensure that you have not short-circuited any adjacent pins. Use a multimeter to be 100% sure. When done all checks, turn it on:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led_off.JPG\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led_off.JPG\" alt=\"led_off\" width=\"700\" height=\"528\" \/><\/a><\/li>\n<li>According to the schematics above, we have connected our LED to GPIO27. In order to turn the LED on programmatically, logon to the Raspberry PI over SSH and run the following commands:\n<pre>sudo su\r\ncd \/sys\/class\/gpio\r\necho 27 &gt; export\r\ncd gpio27\r\necho out &gt; direction\r\necho 1 &gt; value<\/pre>\n<\/li>\n<li>Once you enter the last command you will see the LED go on:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led_on.JPG\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/led_on.JPG\" alt=\"led_on\" width=\"700\" height=\"468\" \/><\/a><\/li>\n<li>You can switch it off again by typing the following command:\n<pre>echo 0 &gt; value<\/pre>\n<\/li>\n<li>Now we will make a simple C++ program that will make the LED blink by automating the commands we used above. Make a basic C++ project for Raspberry PI. Follow <a href=\"\/tutorials\/raspberry\/\">this tutorial<\/a> to set it up with just a few clicks within Visual Studio.<\/li>\n<li>Ensure that you will be running your program as root:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/root.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/root.png\" alt=\"root\" width=\"700\" height=\"703\" \/><\/a><\/li>\n<li>Add the following code to your main() function:\n<pre class=\"\">#include &lt;unistd.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n\r\nclass LinuxFile\r\n{\r\nprivate:\r\n   int m_Handle;\r\n\r\npublic:\r\n    LinuxFile(const char *pFile, int flags = O_RDWR)\r\n    {\r\n        m_Handle = open(pFile, flags);\r\n    }\r\n\r\n    ~LinuxFile()\r\n    {\r\n       if (m_Handle != -1)\r\n            close(m_Handle);\r\n    }\r\n\r\n    size_t Write(const void *pBuffer, size_t size)\r\n    {\r\n        return write(m_Handle, pBuffer, size);\r\n    }\r\n\r\n    size_t Read(void *pBuffer, size_t size)\r\n    {\r\n        return read(m_Handle, pBuffer, size);\r\n    }\r\n\r\n    size_t Write(const char *pText)\r\n    {\r\n        return Write(pText, strlen(pText));\r\n    }\r\n\r\n    size_t Write(int number)\r\n    {\r\n        char szNum[32];\r\n        snprintf(szNum, sizeof(szNum), \"%d\", number);\r\n        return Write(szNum);\r\n    }\r\n};\r\n\r\nclass LinuxGPIOExporter\r\n{\r\nprotected:\r\n   int m_Number;\r\n\r\npublic:\r\n    LinuxGPIOExporter(int number)\r\n        : m_Number(number)\r\n    {\r\n        LinuxFile(\"\/sys\/class\/gpio\/export\", O_WRONLY).Write(number);\r\n    }\r\n\r\n    ~LinuxGPIOExporter()\r\n    {\r\n        LinuxFile(\"\/sys\/class\/gpio\/unexport\", O_WRONLY).Write(m_Number);\r\n    }\r\n};\r\n\r\nclass LinuxGPIO : public LinuxGPIOExporter\r\n{\r\npublic:\r\n    LinuxGPIO(int number)\r\n        : LinuxGPIOExporter(number)\r\n    {\r\n    }\r\n\r\n   void SetValue(bool value)\r\n    {\r\n        char szFN[128];\r\n        snprintf(szFN, sizeof(szFN), \"\/sys\/class\/gpio\/gpio%d\/value\", m_Number);\r\n        LinuxFile(szFN).Write(value ? \"1\" : \"0\");\r\n    }\r\n\r\n   void SetDirection(bool isOutput)\r\n    {\r\n       char szFN[128];\r\n        snprintf(szFN, sizeof(szFN), \r\n           \"\/sys\/class\/gpio\/gpio%d\/direction\", m_Number);\r\n        LinuxFile(szFN).Write(isOutput ? \"out\" : \"in\");\r\n    }\r\n};\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    LinuxGPIO gpio27(27);\r\n    gpio27.SetDirection(true);\r\n    bool on = true;\r\n    for (;;)\r\n    {\r\n        printf(\"Switching %s the LED...\\n\", on ? \"on\" : \"off\");\r\n        gpio27.SetValue(on);\r\n        on = !on;\r\n        sleep(1);\r\n    }\r\n}<\/pre>\n<p>This code is far from being optimal, it opens and closes file handles each time it needs to set the value, it does not check for errors, it does not do many things. However, it abstracts the GPIO interface good enough to start playing around with it. You can later switch the <strong>LinuxGPIO<\/strong> class implementation to a faster one that caches file handles, or something even faster that maps the <strong> BCM2835<\/strong> peripherals into the process memory and controls them directly.<\/li>\n<li>Build your project and run it. You will see the LED blinking. If you encounter any problems, use the debugger to step through the code and analyze it further:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/stepthrough.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/raspberry\/LED\/stepthrough.png\" alt=\"stepthrough\" width=\"701\" height=\"729\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial demonstrates how to attach a LED to the expansion connector on your Raspberry PI and to make it<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[64,33,85],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/166"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":1954,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/166\/revisions\/1954"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}