In a previous guide, we introduced the Arduino Uno and the line of programmable boards and shields based on the platform. But the hardware is nothing without the software. This guide will show you how to set up the Arduino IDE, connect your board, and upload your first sketch.
First, let’s define a few terms that you’ll see in this guide and as you work on future projects:
● Integrated Development Environment (IDE): This term refers to software used to develop programs, which includes a code editor as well as debugging tools, software templates, and other tools necessary to edit code. The Arduino IDE is called simply Arduino or the Arduino Software.
● Sketch: Programs designed to run on Arduino boards are called “sketches.” These programs are simple and run continuously on the boards, even after you disconnect them from the computer.
● Arduino language: The Arduino programming language is a set of C/C++ functions, and if you’re used to coding in C/C++, then you’ll find yourself right at home here. You can find reference for Arduino’s specific functions here.
To get started, download the Arduino IDE for your operating system here. For this guide, we’ll use Windows. Run the installer once it’s downloaded and follow the wizard to install the necessary components. During set up, you’ll be asked to install the USB driver as well, which will allow your computer to communicate with your Uno, or any other boards.
Once the installation is complete, launch the Arduino IDE. You’ll be greeted with a screen that looks like the one to the right.
This is where you can write code that is then uploaded to your Arduino. The blank sketch includes two functions that are core parts of every Arduino sketch: setup() and loop(). Code written inside setup() function will run once, when the Arduino is turned on. Everything inside of loop() will run repeatedly for as long as the device is powered.
Before you upload a sketch, you’ll need to make sure the IDE can properly communicate with your board. Under Tools > Board, make sure your specific model of Arduino is selected. For this guide, we’re using “Arduino Uno”. Next, under Tools > Port, select the COM port that is connected to your board. If your drivers were installed properly, this will have the name of your board next to it. If not, follow these instructions to install your board’s drivers manually.
If you’d rather do your coding from the web—particularly helpful if you use multiple computers to develop your Arduino project—you can set up the Arduino Web Editor and install the Arduino Create Plugin so you can write and upload sketches from Google Chrome or Mozilla Firefox.
To get an idea of how sketches work, we’ll run through a basic sketch called Blink. You won’t need any special wiring for this sketch, and it’s a handy way to make sure that your Arduino is working properly.
The Blink sketch comes built in with the Arduino IDE. To open it, navigate to File > Examples > 01.Basics > Blink. This will open the short sketch in a new window. Once uploaded, the sketch will cause an orange LED on your board to blink at regular intervals.
To upload the sketch, plug your Arduino into your computer, then click Sketch > Upload or press Ctrl-U. Once the sketch finishes uploading, the orange LED marked “L” on your Uno will start blinking once every second.
Congratulations! You’ve uploaded your first sketch.
Now that you know how the Blink sketch works, we can go back and take a look at how it works, and even tweak it a bit. First, let’s take a look at the setup() portion of the code:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
This part of the code runs only once and initializes the LED on your Arduino’s board. The pinMode() function allows you to assign a pin as either an input or an output. The Arduino Uno has 14 digital pins that can be used as either inputs or outputs. However, this sketch uses the built in LED, which has its own pin designation. Therefore, this line of code assigns the LED as an output.
Next, let’s look inside the loop() function.
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
This code will run for as long as the Arduino is powered. It consists of four lines. The first uses the digitalWrite() function to assign a pin (in this case, the built-in LED) as either HIGH or LOW. For this sketch, you can think of this as either on or off. When the pin is set to HIGH, the Arduino Uno will direct a 5V current through the LED, causing it to light up. When the pin is set to LOW, the Uno will drop that voltage to 0V, turning it off.
So, the first line inside the loop turns the LED on. The next function is delay(). This function does exactly what it sounds like. It causes the sketch to wait for a set period of time, doing nothing. The delay() function is measured in milliseconds. Since the sketch tells the Uno to delay for 1,000 milliseconds, the LED will stay on for 1 second.
The third and fourth lines repeat this same series in reverse. The third line sets the LED pin to LOW, turning it off, and the fourth line causes the sketch to wait for another 1 second. By looping this program, the LED will blink on for one second, turn off for one second, and start over, continually blinking for as long as it has power.
Now that you understand how this sketch works, you can try modifying the script to get different effects. You could the delay() intervals to 100 milliseconds to create a fast-blinking light. You could also delete the lines turning off the LED, to leave the light on at all times.
The built-in LED is a particularly useful tool when writing sketches, since your Arduino won’t always provide much feedback if your programs fail. You can use the LED to diagnose whether your sketches are working by, for example, lighting up the LED when it’s successful, or blinking under certain failure conditions. While a single blinking LED might not seem like much on its own, it can be very useful in bigger projects.