This page is part of my Model Remodel series of articles.
DISCLAIMER: If you choose to attempt any of these modifications, you assume all risks thereof. I just wanted to share my experiences here. Neither Fanhome, nor myself, are responsible for any damages that may occur.
Buying an Arduino is the easy part. Programming an Arduino is something else entirely. Many builders have no coding/programming experience and this may keep them from trying it out. I am hoping these articles will persuade both novices and more advanced builders alike to give it a shot. We will start with a simple Arduino program (called a sketch) first and then work our way up to my final result.
The Arduino IDE
The Arduino IDE (Integrated Development Environment) is a computer-based application that allows us to write, test, and upload sketches to an Arduino. Arduino sketches are based on the C++ programming language and the IDE helps us manage our code before it is ‘compiled’ and written to the Arduino. Compilation is the process where the IDE converts the human-readable code on our computer screen to the machine language the Arduino needs.
If you have a reliable Internet connection, the recommended version of the IDE is the web-based Arduino Editor. This online version is always kept up to date by Arduino and it also saves/backs up your sketches in the cloud to be able to access them on any device. It is free for basic use, but you may have to upgrade to a paid plan in order to compile larger sketches. This IDE works on the Chrome, Safari, Edge, and Firefox browsers, but Chrome is the preferred option. I will be basing the rest of my articles on this online version.
NOTE: If you so desire, Arduino also provides a free standalone IDE program you can install locally on your computer for offline use. These installers can be downloaded for Windows, Linux, and Mac at the Arduino Software page. These standalone IDEs need to be updated manually and do not save to the cloud, however, they also do not have any compilation time limits. I have used both both versions of the IDE and they each have their merits. Ultimately, choose the editor that suits you best.
Once you log into the online Arduino Editor, you are presented with a quick tour of the interface. I highly recommend going through this part to learn how the interface is laid out:
Next, we need to download and install a local program on our computer that this IDE can use to talk your Arduino over USB. This program is called an Agent. If this is your first time running the Arduino Editor, click this LEARN MORE button in the yellow banner across the top of the screen.
NOTE: If you do not see this yellow banner, you can also download the Agent by clicking on the Help menu on the left side of the Arduino Editor and scroll down the Arduino Create Agent section under the Info tab.
A window should pop up that asks if the Agent is running correctly or not. Most likely, you will need to INSTALL THE AGENT. Click this link to receive instructions on how to download and install the agent on your computer:
When the Agent is successfully installed and running on Windows (what I use), you should see a small white Arduino logo running in your System Tray. Linux and Mac users will see this agent running in a different manner, but I am not familiar with those operating systems:
At this point, we can connect our Arduino to the computer via a USB A to USB B cable:
Returning to the Arduino Editor screen, we can now select our Arduino from the top dropdown menu. The Agent program allows the editor to detect the connected Arduino type and communication port automatically. In my case, it saw my Arduino Uno on COM4. Upon making the appropriate selection, the editor will link the current sketch to this specific Arduino. This may be helpful if you use multiple Arduinos:
That’s it – we are ready to begin writing our first sketch!
Writing Our First Sketch
A quick personal note: I am not a software programmer by any means, so are likely better ways to write Arduino code. However, I wanted to share what limited knowledge I have learned doing this project in the hope it helps others!
Before we program an Arduino to do something for us, we need to have an idea of what we want it to do. I thought we could start small and create a simple program and wire a circuit that will be used to light up a single red LED.
With the Arduino connected and the Arduino Editor open, we can take a moment to look at the default code provided when you create a new sketch. There are three sections here: a comments area, the setup() function, and the loop() function. Functions are small chunks of code between a { and a } that can be called (triggered to run) by other parts of the program. The reason that the setup() and loop() functions are there by default is because these two functions are required in any Arduino program. The Arduino itself calls these functions as it operates.
NOTE: The ‘void’ declaration before the setup() and loop() functions just means that these functions does not ‘return’ anything. There are other function types, but we do not need to get into those here.
/* */ void setup() { } void loop() { }
Using Comments
Any text in your code between a /* and a */ is considered a comment block and is ignored by the program. Comments are entirely optional, but they can be very useful for describing what the program does, adding author information and contact details, version numbers, etc.
TIP: You can also use // anywhere in your code to add a single line comment. Anything after the // and until the end of the line will be a comment. I will frequently use comments throughout my examples to help explain what the code is doing.
NOTE: While sketches themselves ignores text colors, I will use red text on these code snippets to highlight any ‘new/updated’ code from now on:
/* This is the comment area of our first program. A comment 'block' like this can contain multiple lines. */ void setup() { // We can put single-line comments anywhere we want // This is a comment too! } void loop() { }
The setup() Function
The setup() function is run once at startup when the Arduino first turns on. Inside this function (between the { and } ) is where we put any code that will only need to execute (run) once at the beginning and never again.
In our first sketch, we will use the setup() function to tell the Arduino which pin will be used to connect to our LED. To do this, we use the pinmode command. The pinmode command requires two parameters to work correctly: the pin number and the operating mode. Let’s use DIGITAL Pin 3 for our LED and configure the mode as OUTPUT. The OUTPUT mode means Pin 3 will output 5 VDC when turned on.
NOTE: Almost all lines in an Arduino program need to be ended (terminated) with a semi-colon (;). One benefit of using the Arduino IDE is that it will tell you when these are missing from the required locations.
/* Our first test sketch that will light a LED */ void setup() { // Set DIGITAL Pin 3 as an output pin pinMode(3, OUTPUT); } void loop() { }
You may notice that I indented the comments and pinmode command by a few spaces. This style of formatting is not strictly required, but it generally makes your code easier to read. I indent the commands inside a function/statement so I can quickly see where the function/statement begins and ends.
TIP: The Arduino Editor has a built-in Auto-indent function that can do this for you by clicking this button on the right side of the screen:
The loop() Function
The loop() function is typically where the main body of your sketch will live. The Arduino will call this loop() function after the setup() function. It will then keep calling loop() over and over again until the Arduino is powered off, hence the loop name – it is looping this function.
This loop() function is where we will tell the Arduino to turn our LED on. Since we are using DIGITAL Pin 3 for our LED, we are going to utilize use the digitalWrite command. Similar to the pinmode command, digitalWrite requires two parameters to work correctly: the pin number and a value. This value can be HIGH or LOW. The HIGH value turns the pin ‘on’ (supplying it with 5 Volts DC) while LOW value turns the pin ‘off’ (grounds the pin to zero volts). Remember to use a semi-colon at the end of this command!
/* Our first test sketch that will light a LED */ void setup() { // Set DIGITAL Pin 3 as an output pin pinMode(3,OUTPUT); } void loop() { // Turn Pin 3 on digitalWrite(3,HIGH); }
Download my Sketch
To help others, I have shared this sketch via the Arduino Editor. Simply click the Open Code or Download buttons!
Verifying the Sketch
We can check our sketch for errors clicking on the ‘Verify & Save’ checkmark button in the Arduino Editor:
If the verification passes, you will see a green Success banner at the bottom of the screen. Nice job! Below this banner, you will see some additional data about how much space your sketch takes up and what the maximums are for your specific Arduino:
If your verification fails, a red banner will be displayed and the offending line in the code will be highlighted. The Arduino Editor will also tell you what is wrong, so hopefully it is an easy fix! To demonstrate a failed verification, I deleted the semi-colon from the end of the digitalWrite command and ran the verification again. You can see the IDE fails to verify the sketch because it is expecting to see a ; before the closing } of the function.
That’s it – we have created our first Arduino sketch!
Uploading the Sketch
First, make sure your Arduino is connected to the computer via the USB cable and the small ‘ON’ light is lit on the Arduino board. Then, we can upload this sketch to the Arduino by clicking on the ‘Upload & Save’ arrow button in the Arduino Editor:
NOTE: Uploading a sketch always replaces/overwrites any existing sketch stored on your Arduino.
After a few seconds, we should receive a green Success banner indicating a good upload! With this upload complete, the Arduino will reset and begin running our sketch automatically.
TIP: If your sketch does not upload, check your cable connections, make sure the Arduino is powered, and verify the Arduino Agent is running on your computer. By the way, there have been times when a sketch failed to upload and all it took was uploading it again to make it complete successfully.
Next Section
OUR FIRST CIRCUIT – Bringing our first sketch to life!