{"id":2567,"date":"2014-11-23T16:09:20","date_gmt":"2014-11-23T16:09:20","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2567"},"modified":"2014-11-23T16:09:20","modified_gmt":"2014-11-23T16:09:20","slug":"getting-started-with-the-arduino","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/getting-started-with-the-arduino\/","title":{"rendered":"Getting started with the Arduino"},"content":{"rendered":"<p>Let&#8217;s get started with some basics using the Arduino. <\/p>\n<p><em>Note: I am using the Arduino Uno, so references to the specification of the Arduino should be taken as specific to the Uno.<\/em><\/p>\n<p>The Arduino is programmed using C\/C++ and includes libraries which make programming simple and programming the Arduino even simpler. Arduino programs are known as sketches.<\/p>\n<p>My aim in this post in not to repeat everything in the excellent Arduino documentation but to just show code snippets (Arduino sketches) to simply demonstrate the principles of getting started programming the Arduino. <\/p>\n<p><strong>The Basics<\/strong><\/p>\n<p>Whether using the Arduino IDE\/application or the Visual Micro for Visual Studio (or any other editor) and Arduino application requires at the bare minimum a <em>setup<\/em> function and a <em>loop<\/em> function, which are simply written as<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid setup()\r\n{\r\n   \/\/ your setup goes here \r\n}\r\n\r\nvoid loop()\r\n{\r\n   \/\/ your code goes here \r\n}\r\n<\/pre>\n<p>The <em>setup<\/em> as the name suggests, is used to initialize variables or set pin modes (discussed later) etc. The Arduino calls the <em>setup<\/em> and then the <em>loop<\/em>.<\/p>\n<p>An Arduino is single threaded, the <em>loop<\/em> is where we add our &#8220;main&#8221; code. Of course we can create further functions that are called from the <em>loop<\/em>. Just remember when the loop function exits, the Arduino will call the function again and so on.<\/p>\n<p><strong>Input\/Output<\/strong><\/p>\n<p>The Arduino microcontroller has digital input\/outputs and analog input pins. There are no analog output pins but we can simulate such output using <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/SecretsOfArduinoPWM\" title=\"PWM\" target=\"_blank\">PWM<\/a>. <\/p>\n<p><strong>Digital pins<\/strong><\/p>\n<p><a href=\"http:\/\/arduino.cc\/en\/Tutorial\/DigitalPins\" title=\"Digital pins\" target=\"_blank\">Digital pins<\/a> can be configured as either inputs or outputs. Digital pins 1 to 13 are available for input\/output with digital pins marked with the tilde ~ on the Uno being pins capable of PWM and therefore simulating analog output.<\/p>\n<p>By default, digital pins are inputs, but within the <em>setup<\/em> we can change them to outputs. Let&#8217;s take a look at how we do this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst int inputPin = 1;\r\nconst int outputPin = 3;\r\n\r\nvoid setup()\r\n{\r\n   pinMode(inputPin, INPUT); \/\/ setting a digital pin for INPUT\r\n   pinMode(outputPin, OUTPUT); \/\/ setting a digital pin for OUTPUT\r\n}\r\n<\/pre>\n<p>Once the pins have been setup for I\/O we need a way to read\/write data from\/to those pins. <\/p>\n<p><em>Note: I will discuss using digital pins for PWM in the Analog section<\/em><\/p>\n<p>Let&#8217;s look at the code to read and write to the digital pins<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint value;\r\n\r\nvoid loop()\r\n{\r\n   value = digitalRead(inputPin);\r\n   digitalWrite(outputPin, value);\r\n}\r\n<\/pre>\n<p>Digital values are HIGH and LOW (think ON and OFF), so a digitalRead will return either HIGH or LOW and likewise the digitalWrite can write a HIGH or LOW.<\/p>\n<p>One thing to note is that when the pins are used for input they&#8217;re in a &#8220;high impedance state&#8221;, for us programmers this basically means that small changes can change the pin from LOW to HIGH or vice versa, and more importantly it means that if they have nothng connected to them and we&#8217;re reading their state we will get random changes in the pin state as they pick up &#8220;noise&#8221;. See <a href=\"http:\/\/digitalmedia.risd.edu\/pbadger\/PhysComp\/pmwiki\/pmwiki.php?n=Devices.GPIO\" title=\"Arduino General Purpose Input &#038; Output (GPIO) Pins Explained\" target=\"_blank\">Arduino General Purpose Input &#038; Output (GPIO) Pins Explained<\/a> for more on this subject.<\/p>\n<p><strong>Analog<\/strong><\/p>\n<p>As stated previous, there are no analog output pins, but we can use the digital pins as output pins and using PWM simulate analog output.<\/p>\n<p>First off let&#8217;s take a look at the Arduino Uno and you&#8217;ll see 6 Analog input pins, marked A0 to A5. To read from the analog pins we simple use the <a href=\"http:\/\/arduino.cc\/en\/Reference\/analogRead\" title=\"analogRead\" target=\"_blank\">analogRead<\/a> method, for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint value;\r\n\r\nvoid setup()\r\n{\r\n   value = analogRead(0);\r\n}\r\n<\/pre>\n<p>Analog input voltages range from 0 to 5V which results in an integer value from 0 to 1023. However when outputting analog values we can only write values 0 to 255.<\/p>\n<p>We can use the <em>map<\/em> function to scale the values from input to output if we need to, i.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint value = map(inputValue, 0, 1023, 0, 255); \r\n<\/pre>\n<p>As mentioned, we cannot write analog data to analog pins but we can write data to digital PWM pins. The Arduino library kindly gives us a function <a href=\"http:\/\/arduino.cc\/en\/Reference\/analogWrite\" title=\"analogWrite\" target=\"_blank\">analogWrite<\/a> which handles everything for us, we just need to specify one of the PWM pins for output, for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst int outputPin = 3;\r\n\r\nvoid setup()\r\n{\r\n   pinMode(outputPin, OUTPUT);\r\n}\r\n\r\nint value = 0;\r\n\r\nvoid loop()\r\n{\r\n   if (value &gt; 255)\r\n      value = 0;\r\n\r\n   analogWrite(outputPin, value++);\r\n   delay(1000);\r\n}\r\n<\/pre>\n<p><strong>Serial I\/O<\/strong><\/p>\n<p>We can use the UART via the <a href=\"http:\/\/arduino.cc\/en\/reference\/serial\" title=\"Serial\" target=\"_blank\">Serial<\/a> functions to communicate with other devices, for example intercommunication between the Arduino and a connected PC. <\/p>\n<p>In such a case you might use the Serial functions to write debug information and within the Arduino IDE you can monitor the connected COM port and view the debug output.<\/p>\n<p>To setup and use the Serial port we can use the following code (from the Arduino)<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid setup()\r\n{\r\n   Serial.begin(9600);\r\n}\r\n\r\nvoid loop()\r\n{\r\n   if (value &gt; 255)\r\n      value = 0;\r\n\r\n   Serial.println(value);\r\n   value++;\r\n\r\n   delay(1000);\r\n}\r\n<\/pre>\n<p>With the above code, if we view the output in the serial port monitor we&#8217;ll simply see the value variable slowly (with a 1 second delay between each iteration) increase until it reaches 1024 and then it&#8217;s reset to 0.<\/p>\n<p>So, we can output to the COM port but the Arduino can also read input from the COM port and thus we can create two way communications between the Arduino and another device.<\/p>\n<p>For example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst int outputPin = 3;\r\n\r\nvoid setup()\r\n{\r\n   Serial.begin(57600);\r\n}\r\n\r\nint value = 0;\r\n\r\nvoid loop()\r\n{\r\n   if (Serial.available() &gt; 0)\r\n   {\r\n      value = Serial.parseInt();\r\n      if (value &lt; 0)\r\n         value = 0;\r\n   }\r\n   Serial.println(value);\r\n\r\n   analogWrite(outputPin, value);\r\n}\r\n<\/pre>\n<p>In the above code we use the parseInt method instead of the read method. The parseInt method simply takes the input and converts to an integer, so for example if using the serial port monitor within the Arduino IDE, sending 0 will result in the ASCII value (or 49) being received by the Arduino &#8211; parseInt converts this to an integer for us.<\/p>\n<p><strong>References<\/strong><\/p>\n<p>There are many excellent resources on the Arduino, some of which I have referenced for this post, others I will add here because they may be of use.<\/p>\n<p><a href=\"http:\/\/arduino.cc\/en\/uploads\/Main\/ArduinoUno_R3_Front.jpg\" title=\"Arduino Uno\" target=\"_blank\">The Arduino Uno<\/a><br \/>\n<a href=\"http:\/\/arduino.cc\/en\/reference\/board\" title=\"Introduction to the Arduino Board\" target=\"_blank\">Introduction to the Arduino Board<\/a><br \/>\n<a href=\"http:\/\/playground.arduino.cc\/Learning\/Pins\" title=\"Arduino Quick Reference\" target=\"_blank\">Arduino Quick Reference<\/a><br \/>\n<a href=\"http:\/\/arduino.cc\/en\/uploads\/Main\/arduino-uno-schematic.pdf\" title=\"Arduino Uno schematic\" target=\"_blank\">Arduino Uno Schematic<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s get started with some basics using the Arduino. Note: I am using the Arduino Uno, so references to the specification of the Arduino should be taken as specific to the Uno. The Arduino is programmed using C\/C++ and includes libraries which make programming simple and programming the Arduino even simpler. Arduino programs are known [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[88],"tags":[],"class_list":["post-2567","post","type-post","status-publish","format-standard","hentry","category-arduino"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2567"}],"version-history":[{"count":27,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2567\/revisions"}],"predecessor-version":[{"id":2629,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2567\/revisions\/2629"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}