Board and IDE Connections

Objectives and Overview

This lesson reviews the code used in the example Blink sketch. Additionally, this lesson provides some tips such as making sure that line numbers are displayed in the Arduino IDE.

Lesson Objectives

  • Review the code used in the Blink sketch.
  • Familiarize yourself with some basic Arduino functions used in the Blink sketch, such as digitalWrite and delay.
  • Make sure that your Arduino IDE displays line numbers.

Board and IDE Connections

Now you know how to set up the Arduino board and upload code from the IDE. Right now, you just uploaded the starter Blink code. Now let’s look at that code and break down some of the key parts in order to develop an understanding of what each line does, and what can be changed.

Let’s start by examining the Blink sketch that you just uploaded:

// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(13, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Code language: JavaScript (javascript)

The code comments (you’ll learn more about this important concept later in the camp) in the void setup() function have some important information about connecting the software and hardware of the Arduino:

  • pinMode(13, OUTPUT);
    • pinMode tells the Arduino some important information. The first number, 13, tells the Arduino that the code is going to interact with something that’s on pin 13.
    • Recall that you attached an LED to pin 13. This line of code tells the Arduino that there is something on this pin.
    • The “OUTPUT” sets the pin to output. In this example, the output is the blinking LED.
  • Important Troubleshooting: If you’re having trouble with your LED not blinking, make sure that the pinMode number corresponds to the pin holding the LED!

Next, let’s examine some of the key code in the void loop() function:

  • digitalWrite(13,HIGH); and digitalWrite(13,LOW); are the commands that actually turn the LED on and off, resulting in the blinking.
    • Note that the number included in this must match the one in the pinMode() in the setup.
    • HIGH sends an on message to the LED whereas LOW sends an off message.
  • delay(1000); results in a pause of 1 second. The values that delay takes are in milliseconds(ms) so 1000 is equal to 1 second.

Important Concepts

The comments in the code, as well as the explanations above, shed some light on the importance of making sure that your code correlates and matches the physical connections with your board.
When troubleshooting Arduino projects, and you’ll likely have to do some troubleshooting in this camp, you have to check both the software and the hardware. If you build solid habits from the beginning, such as making sure that anything that references specific pins in the board has the correct number, you’ll have a much easier time isolating and fixing issues!

Pro-Tip: Displaying Line Numbers in the IDE

Here is a quick tip to help you when you are using the Arduino IDE. You want to be able to see the line numbers in the Arduino IDE to help track down bugs in error messages. To do so, just follow these steps:

First, launch the Arduino IDE. Once it opens, click on the File menu and select Preferences.

Arduino IDE File menu with Preferences highlighted

Next, click on the checkbox next to Display line numbers. This will now display line numbers in your code! Here’s a screenshot of where this is inside the Preferences menu:

"Display line numbers" checkbox selected inside the Arduino IDE Preferences menu.

Click the OK button to save your changes and close the window.

Without this option selected, the code in the IDE looks like this:

Arduino IDE sketch without line numbers shown

With the line numbers displayed, the code in the IDE looks like this:

Arduino IDE sketch with line numbers shown

Turning on the line numbers makes debugging and fixing your code much easier. Additionally, you’ll be able to check to see if you’re missing any code that you import since your line numbers won’t match.