{"id":131227,"date":"2023-01-16T19:23:11","date_gmt":"2023-01-16T19:23:11","guid":{"rendered":"https:\/\/appleinsider.com\/inside\/macos-ventura\/tips\/how-to-get-started-coding-for-arduino-on-macos-ventura?utm_medium=rss"},"modified":"2023-01-16T19:23:11","modified_gmt":"2023-01-16T19:23:11","slug":"how-to-get-started-coding-for-arduino-on-macos-ventura","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/16\/how-to-get-started-coding-for-arduino-on-macos-ventura\/","title":{"rendered":"How to get started coding for Arduino on macOS Ventura"},"content":{"rendered":"<div class=\"col-sm-12\" id=\"article-hero\" aria-labelledby=\"hero-cap\" role=\"figure\"> <a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura.jpg\"> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura.jpg\" alt> <\/a> <\/div>\n<div class=\"col-sm-12\">\n<p> <em> AppleInsider may earn an affiliate commission on purchases made through links on our site. <\/em> <\/p>\n<\/div>\n<p class=\"col-sm-12 article-lead\">In a previous article, we looked at connecting various kinds of Arduino hardware to your Mac. Here&#8217;s how to get started programming on them to create your own projects.\n<\/p>\n<p><h2 data-anchor=\"getting-started\" id=\"getting-started\">Getting started<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>In order to program your Arduino, you need the Arduino IDE (Integrated Development Environment) from the <a href=\"https:\/\/www.arduino.cc\">arduino.cc<\/a> website. We mentioned how to download and install the IDE in the previous article but we&#8217;ll reiterate it here:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>On your Mac, go to <a href=\"https:\/\/arduino.cc\">arduino.cc<\/a>, click on <a href=\"https:\/\/www.arduino.cc\/en\/software\">&#8220;Software&#8221;<\/a>, then under the Download Options section, click the link for the Mac version for either Intel or Apple Silicon. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Note that unless you want to explore the current development version, you <em>don&#8217;t<\/em> want the link under the section &#8220;Nightly Builds&#8221;. Nightly builds may be unstable and contain bugs.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Once you&#8217;ve downloaded the IDE, refer to the previous article for details about how to connect and set up your Arduino on your Mac in the IDE. You need a connection before you can upload code from the IDE to your Arduino.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"basics\" id=\"basics\">Basics<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>You only need rudimentary programming skills to program your Arduino. You can use Arduino&#8217;s C-like programming language, or <a href=\"https:\/\/www.python.org\/\">Python<\/a>, but we&#8217;ll only use <a href=\"https:\/\/en.wikipedia.org\/wiki\/C_(programming_language)\">C<\/a> in the examples below.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In Arduino programs, called <em>Sketches<\/em>, you essentially write code to set up your Arduino for a specific tasks or tasks, then run a continuous loop which gets called repeatedly by the Arduino microcontroller once your Sketch is uploaded to your Arduino device. The loop usually waits for input from sensors or users, and sends control signals back to sensors and devices to display some sort of output. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The loop code can also connect to, and interact with, the internet.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can also install third-party libraries which support various sensor devices and shields using the Library Manager in the IDE. To access the Library Manager, create or open a sketch window, and click the icon on the left side which looks like a set of books:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<figure><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-1.jpg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-1.jpg\" alt height=\"761\" loading=\"lazy\" class=\"img-responsive article-image\"><\/a><\/figure>\n<\/div>\n<div class=\"col-sm-12\">\n<p>A pane will appear and at the top, you can browse available libraries by Type and Topic by clicking either of the two popup menus:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<figure><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-2.jpg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-2.jpg\" alt height=\"780\" loading=\"lazy\" class=\"img-responsive article-image\"><\/a><\/figure>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can also check for installed library updates by selecting <strong>&#8220;Updatable&#8221;<\/strong> from the <strong>&#8220;Type:&#8221;<\/strong> menu.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The Library Manager pane lets you download official and third-party libraries. Libraries are code bundles that add a specific functionality or device support to the IDE. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>For example, if you use a particular brand of a non-generic sensor, you&#8217;ll need to download its library and install it first.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can view different categories of libraries by clicking the &#8220;Topic&#8221; popup menu at the top of the Library Manager window.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Most Arduino Sketches are simple and short and are stored in a &#8220;Sketchbook&#8221; folder specified in the Arduino IDE <strong>Settings<\/strong> window. You can change where the IDE stores sketches from here by clicking the <strong>Browse<\/strong> button next to &#8220;Sketchbook location&#8221;:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<figure><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-3.jpg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-3.jpg\" alt height=\"768\" loading=\"lazy\" class=\"img-responsive article-image\"><\/a><\/figure>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Sketches have a file extension of .ino, which stands for &#8220;innovation&#8221;.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can also change text editor and compiler settings here. If you have an unsupported Arduino, you can add its board support file or URL by clicking the small icon in the lower right of the window, one which looks like a stack of documents.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Click on the <strong>&#8220;Click for a list of unofficial board support URLs&#8221;<\/strong> text in the Additional Boards Manager URLs window to view the <a href=\"https:\/\/github.com\/arduino\/Arduino\/wiki\/Unofficial-list-of-3rd-party-boards-support-urls\">vast array of boards<\/a> supported on Arduino&#8217;s GitHub.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The Arduino IDE provides a sample sketch with a simple program outline in a text window when you first open it.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>There are also a vast array of code samples under the <strong>File-&gt;Examples<\/strong> submenu item. Samples are organized by &#8220;Built-In&#8221; and &#8220;UNO Examples&#8221; in the menu. Custom-installed libraries may also provide examples.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"a-first-example-sketch\" id=\"a-first-example-sketch\">A first example sketch<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>In our first example, we&#8217;ll use the built-in <em><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/BuiltInExamples\/Blink\">Blink<\/a><\/em> example. To open it, select <strong>File-&gt;Examples-&gt;0.1Basics-&gt;Blink<\/strong> submenu item. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>After a few seconds, a new editor window will open with the Blink example. Make sure your Arduino board and port are selected from the connection popup menu at the top of the IDE&#8217;s editor window.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Blink does one thing \u2014 it blinks a built-in LED on the Arduino. There&#8217;s a description of the sample in the comment at the top of the editor window and online.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In code, <em>comments<\/em> are notes programmers leave to describe what code is doing. Comments are ignored during compilation.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The IDE editor window uses C-style comments: the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Compiler\">compiler<\/a> will ignore anything bracketed inside of \/* and *\/. Single-line comments start with \/\/ but must be on one line only. Anything after the \/\/ is ignored.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>When you click the large <strong>Verify<\/strong> button with the checkmark icon in the editor&#8217;s upper left corner, the IDE will compile the code in that window. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>During compilation, an Output pane will appear at the bottom of the editor window in black showing progress. If there are no errors, you&#8217;ll see messages such as:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>\"Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes. <br \/>Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.\"<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Or something similar.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>If there are errors, they will appear in red text and you&#8217;ll have to fix your code until there are no more errors. Some errors are not critical, and your sketch will still run with them, but other errors can prevent your sketch from running at all.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can clear the Output messages by clicking the small icon in the upper right corner of the Output pane. You can hide the Output pane entirely by clicking the small square icon in the lower right corner of the editor window.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>When you click the <strong>Upload<\/strong> button (the one with a large right arrow icon) next to the <strong>Verify<\/strong> button, the IDE uploads the compiled binary program into the Arduino on the port you specified. The Arduino microcontroller takes over from there and executes your code on the Arduino.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>During upload, if your Arduino has an RX (Receive) LED built-in, you should see it flash rapidly as it receives the sketch data.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>If your Sketch couldn&#8217;t be uploaded for any reason, the Output pane will list a description and why.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"a-few-words-about-cbased-languages\" id=\"a-few-words-about-cbased-languages\">A few words about C-based languages<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>C is the language of operating systems. Some early Mac apps in the late 1980s and the 1990s were written in C or one of its later variants: <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\">C++<\/a>.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The Arduino IDE programming language is based on C-like syntax.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In most C-like languages, all code lines end with a &#8220;;&#8221; \u2014 without the semicolon, the code won&#8217;t compile and you&#8217;ll get an error.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Most C-based languages also use predefined code text files called <em>headers<\/em>, which usually have a &#8220;.h&#8221; file extension.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Think of a .h file as a predefined set of code that describes how functions are to be accessed, called prototypes. Each prototype defines a function name, parameters to be passed to the function (inside parenthesis), and a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Return_type\">return type<\/a> that is sent back from the function when it exits.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>If you call (access) any library or built-in functions in your Sketch code, how you call each function must match its prototype defined in a .h file somewhere. Libraries work the same way.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>For example, if a prototype says a function must take two input parameters (in the parenthesis), and a certain type of return value (listed before the function name), then you must call it in precisely the same way. Anything else will throw an error during compilation.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Header files can also contain a C-style construct called a &#8216;define.&#8217; A define creates a label as another code expression, such as a number, text (a <em>string<\/em> in C), a calculation, or some other function. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>To create a define, you use the #define C <a href=\"https:\/\/en.wikipedia.org\/wiki\/Preprocessor\">preprocessor<\/a> directive. For example:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>#define FALLING 2<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>This code defines the label &#8216;FALLING&#8217; as the value of 2. Anywhere you use FALLING in your code, the number 2 will be substituted at compile time. #defines can get quite complex but can make your code shorter and more readable.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The Arduino IDE uses #defines to define things like I\/O pin numbers, modes, and other things.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can also create your own headers and #defines.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Headers can be included in other files &#8211; in other .h files, or in Sketches themselves. The contents each included .h file get inserted at compile time into the top of any files they are included in. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>To insert a header into another file, use the #include C directive. For example at the top of Arduino.h you&#8217;ll see:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>#include \"binary.h\"<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Which includes another header file called &#8220;binary.h&#8221; into the top of Arduino.h at compile time.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>If you look at the screenshot shown above you&#8217;ll see two included .h files in the example Sketch:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>EEPROM.h <br \/>Ethernet.h<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>All this may seem confusing at first, but it&#8217;s actually quite simple: you put function prototypes and #defines in .h files so they can be used in many other files. Then you #include them in other files and the compiler inserts them where indicated during compilation. Easy. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Organizing definitions into seperate headers enables <a href=\"https:\/\/en.wikipedia.org\/wiki\/Code_reuse\">code reuse<\/a>.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Just think of .h files as definitions, and your Sketch files as programs that use them.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Modern programming languages such as Apple&#8217;s <a href=\"https:\/\/appleinsider.com\/inside\/macos-ventura\/tips\/Swift.org\">Swift<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/\">Microsoft&#8217;s C#<\/a> have done away with header files, in the interest of simplicity.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"back-to-the-blink-example\" id=\"back-to-the-blink-example\">Back to the Blink example<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>In the Blink sample&#8217;s setup() function, there is one line of code:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>pinMode(LED_BUILTIN, OUTPUT);<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Both LED_BUILTIN and OUTPUT are #defines defined by Arduino headers. The main Arduino header file is named Arduino.h, as we saw above.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can hold down the Command key on your Mac keyboard and double-click any #define in an editor window to jump to its definition in the corresponding .h file where it&#8217;s defined. A new tab will open in the same editor window displaying the matching .h file.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>LED_BUILTIN is defined as &#8220;13&#8221; in pins_arduino.h:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>#define LED_BUILTIN 13<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>This indicates digital pin 13 on the Arduino&#8217;s pins connector (also called a header).\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>But at the same time LED_BUILTIN tells the Arduino to use the actual built-in LED on the Arduino circuit board itself. If you connect a <a href=\"https:\/\/www.amazon.com\/s?k=jumper+wires&amp;crid=2ICWEG3PDZ7KN&amp;sprefix=jumper+wire%2Caps%2C208&amp;ref=nb_sb_noss_1?tag=reality\" rel=\"sponsored\">jumper wire<\/a> to pin D13 on the Arduino header, then connect it to an LED on a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Breadboard\">breadboard<\/a>, it will blink that LED also.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>pinMode&#8217; is a built-in Arduino function that sets how a given I\/O pin on the Arduino behaves &#8211; either input or output. In this case, we&#8217;re telling the Arduino to use pin LED_BUILTIN (13) as an output pin.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Since pinMode()&#8217;s prototype in the wiring_digital.h header has a return type of &#8216;void&#8217;, the function doesn&#8217;t return any value. &#8216;void&#8217; is a C data type meaning &#8216;nothing&#8217;. All C functions having a &#8216;void&#8217; return type return nothing.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>When the compiled Blink sketch runs on your Arduino, it runs setup() first, setting up the hardware, then it runs loop() over and over forever. In the Blink example, loop() simply turns the pin on and off with a delay of one second between each:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>digitalWrite(LED_BUILTIN, HIGH); \/\/ turn the LED on (HIGH is the voltage level) <br \/>delay(1000); <br \/>digitalWrite(LED_BUILTIN, LOW); \/\/ turn the LED off by making the voltage LOW <br \/>delay(1000);<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>&#8220;High&#8221; and &#8220;Low in electrical engineering terms simply mean &#8220;on&#8221; and &#8220;off&#8221;.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>digitalWrite() is a built-in Arduino function that simply turns the digital U\/O pin specified on or off &#8211; in this case whatever is attached Arduino pin D13, or LED_BUILTIN.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>delay() is a delay function that halts further processing until the specified interval has elapsed. delay() takes a single time value, in milliseconds, with a value of &#8216;1000&#8217; being one second.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Go ahead and click the <strong>Upload<\/strong> button in the Blink Sketch IDE window.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>That&#8217;s it. You&#8217;ve now compiled and run your first sketch. If everything worked, you will see the built-in LED on your Arduino blink on and off. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Most <a href=\"https:\/\/store.arduino.cc\/collections\/shields\">shields<\/a> also have a built-in LED on them which does the same thing as the built-in one on the Arduino.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"a-breadboard-example\" id=\"a-breadboard-example\">A breadboard example<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>Now that you&#8217;ve seen the Blink example in action, we&#8217;ll do the same thing &#8211; but this time we&#8217;ll make an external LED on a breadboard blink at the same time. For this example you&#8217;ll need:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<ol>\n<li>A breadboard<\/li>\n<li>2 jumper wires<\/li>\n<li>1 3.3V LED<\/li>\n<li>1 220 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ohm\">Ohm<\/a> <a href=\"https:\/\/en.wikipedia.org\/wiki\/Resistor\">resistor<\/a><\/li>\n<\/ol>\n<\/div>\n<div class=\"col-sm-12\">\n<p>First, plug two jumper wires into the &#8220;D13&#8221; or &#8220;13&#8221; and &#8220;GND&#8221; holes on the corresponding Arduino headers.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Plug the other end of the &#8220;D13&#8221; wire anywhere into row one in the interior of the breadboard (any position except in the &#8220;+&#8221; and &#8220;-&#8221; power rails on the outer edges).\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Next, install the LED onto the breadboard so the long (+) leg of the LED is in the same <em>horizontal<\/em> row as the &#8220;D13&#8221; wire you just installed.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Turn the LED sideways so the short leg is inserted into a hole towards the long end of the breadboard about three holes away.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Next, insert a 220 Ohm resistor into the same horizontal row as the short LED&#8217;s leg, but towards the power rail on the opposite side of the board. Across the center of the board works perfectly.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Insert the other end of the resistor into another hole in the same direction about three holes away.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You should now have the &#8220;D13&#8221; wire, LED, and resistor in a pattern somewhat in the shape of a stretched-out &#8220;Z&#8221; (see photo below).\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>At the other side of the breadboard in the last or second to last hole before the power rail, insert the other end of the &#8220;GND&#8221; jumper wire. The final assembly should look something like this:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<figure><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-4.jpg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-4.jpg\" alt height=\"833\" loading=\"lazy\" class=\"img-responsive article-image\"><\/a><\/figure>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Remember there is a grid of metal rails under all the holes on the breadboard. No matter which way components are oriented on the breadboard, they must always form connections between components, the Arduino, and with jumper wires to complete the circuit.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>If you already uploaded the Blink example to your Arduino, you should see the LED start to blink. Both the LED on the Ardunio and the one on the breadboard should blink in unison. If not, go back and check all your connections again.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Note that some electronic components such as LEDs are <em>polar<\/em>: current can only flow through them correctly in one direction. Other components such as most resistors are non-polar: current can flow the same through them in either direction.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can play around with the timing value passed to the delay() function to speed up or slow down the blink rate of the LEDs. Try huge and tiny values and watch what happens.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>By adding more digitalWrite() and delay() statements to your Sketch you can alter the blink pattern: for example, you can make the LEDs blink <a href=\"https:\/\/en.wikipedia.org\/wiki\/Morse_code\">Morse Code<\/a>.\n<\/p>\n<\/div>\n<p><h2 data-anchor=\"final-example-a-traffic-light-simulator\" id=\"final-example-a-traffic-light-simulator\">Final example: a traffic light simulator<\/h2>\n<\/p>\n<div class=\"col-sm-12\">\n<p>Now that you&#8217;ve seen how to blink your Arduino&#8217;s LED, we&#8217;ll use one final, slightly more complex example: we&#8217;ll use a third-party UNO breakout board and an external traffic light sensor board to simulate a three-color traffic stop light.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Alternately, you can find a <a href=\"https:\/\/www.pcbway.com\/project\/shareproject\/Temporized_Traffic_Light.html\">temporized one<\/a> with a countdown timer LCD also on PCBWay.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In our example, we&#8217;ll use the simplest three-light sensor and make it change colors at brief intervals, just like a real traffic light.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>These traffic light boards usually have three or four pins: one for each colored LED, and one GND. The code is smilier to Blink, except that you turn all the lights off except one, wait using delay(), then turn that one off, and the next one on, in sequence.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>First, we&#8217;ll connect our traffic light sensor to a generic breakout shield we have installed on our Arduino UNO: breakout shields are shields containing banks of analog and digital pins, GND pins, Bluetooth, and serial cable connectors.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Our traffic light sensor has four pins: R, G, Y, and GND. We&#8217;ll connect digital pins 9, 10, and 11 to R, G, and Y respectively, and the fourth pin, GND to a GND pin on our shield.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Now open the Arduino IDE and start a new Sketch. First, we&#8217;ll define some things we&#8217;ll need at the top of the Sketch above setup().\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>First, we define how many milliseconds are in a second so we can specify how many seconds to pass to delay();\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>#define kMillisecondsInSec 1000<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Next, we define how many seconds we want the yellow light to be on for:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>#define kSecondsForYellow ( kMillisecondsInSec * 4 )<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Then we define which three pins we want to use for red, yellow, and green on the Arduino. Instead of using defines, we assign the pin values each to a <em>global variable<\/em>, in this case variables of type int (which is defined by C):\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>int RED = 9; <br \/>int YELLOW = 10; <br \/>int GREEN = 11;<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Think of a <em>variable<\/em> as a named container whose contents (value) you can change whenever you want. Variables also have a <em>type<\/em> in order to specify what kind of values they can hold. Global variables can be accessed from anywhere in a program. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Variables declared inside of functions are called <em>local<\/em> variables and can only be used inside one function. This is called <a href=\"https:\/\/www.w3schools.in\/c-programming\/variable-scope\">variable scope<\/a>.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Next, we define global variables and assign calculations to them to simplify how we calculate seconds and milliseconds, and delay values for the green, red, and yellow lights in seconds. In this case we use a variable type called &#8216;unsigned long int&#8217;, which are like ints, but can hold larger values:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>unsigned long int millisecondsInMinute = ( kMillisecondsInSec * 12 );\t\/\/ # of milliseconds in 1 min. <br \/>unsigned long int minutesForGreenAndRed = ( 1 * millisecondsInMinute ); \/\/ number of minutes to leave green, red lights on. <br \/>unsigned long int yellowDelay = kSecondsForYellow; \/\/ Time to leave yellow light on. Yellow delay is shorter than red\/green.<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Now in setup() we define the pin modes for pins we defined above, one each for red, yellow, and green:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>void setup( void ) <br \/>{ <br \/>pinMode( RED, OUTPUT ); <br \/>pinMode( YELLOW, OUTPUT ); <br \/>pinMode( GREEN, OUTPUT ); <br \/>}<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>This tells the Arduino we will use those three pins for output values (in this case on or off).\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In loop(), we start by turning the red\/yellow lights off, the green light on, then we wait for onDelay seconds:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>void loop( void ) <br \/>{ <br \/>\/\/ Green - Start <br \/>digitalWrite( RED, LOW ); <br \/>digitalWrite( YELLOW, LOW ); <br \/>digitalWrite( GREEN, HIGH ); <br \/>delay( onDelay );<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>After onDelay, we turn the green light off, the yellow light on, then wait for yellowDelay seconds:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code> digitalWrite( GREEN, LOW ); <br \/>digitalWrite( YELLOW, HIGH ); <br \/>delay( yellowDelay );<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>After yellowDelay, we turn the yellow light off, the red light on, and wait for onDelay seconds:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code> \/\/ Red <br \/>digitalWrite( YELLOW, LOW ); <br \/>digitalWrite( RED, HIGH ); <br \/>delay( onDelay );<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Finally, after onDelay elapses, we turn the red light off, effectively resetting the simulation:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p><code>digitalWrite( RED, LOW ); <br \/>}<\/code>\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>The next time the loop runs, the same sequence repeats, starting with the green light. This loop will run forever until stopped. We now have a working traffic light simulator:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<figure><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-5.jpg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2023\/01\/how-to-get-started-coding-for-arduino-on-macos-ventura-5.jpg\" alt height=\"802\" loading=\"lazy\" class=\"img-responsive article-image\"><\/a><\/figure>\n<\/div>\n<div class=\"col-sm-12\">\n<p>Once you&#8217;ve uploaded the Sketch to your Arduino, if you plug in an external power supply to your Arduino&#8217;s DC barrel jack, and unplug its USB cable, the simulation will continue to run. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>This is one of the benefits of Arduino: once you program the microcontroller, it can run a program independently of a host computer. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>You can make Arduinos do just about anything &#8211; act as sensors, display info, wait for input, make sounds, use cameras and send images back to other devices, monitor conditions, drive motors, and so on.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>I&#8217;ve posted the complete traffic light Sketch. You can download it and upload it to your Arduino.\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>There are a few introductory books worth reading for learning Arduino&#8217;s C-like language:\n<\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>These simple examples should get you started programming Arduino. As you build your programming skills and gain confidence, you can expand to bigger examples. <\/p>\n<\/div>\n<div class=\"col-sm-12\">\n<p>In future articles, we&#8217;ll explore programming Arduino devices, shields, and busses such as I2C and SPI, and creating complex device environments.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>AppleInsider may earn an affiliate commission on purchases made through links on our site. In a previous article, we looked at connecting various kinds of Arduino hardware to your Mac. Here&#8217;s how to get started programming on them to create your own projects. Getting started In order to program your Arduino, you need the Arduino [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":131228,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"class_list":["post-131227","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple-insider"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131227"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131227\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/131228"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}