Open Arduino IDE, Connect, Blink

PR0 Challenge 5.3 [30pts]

Adapted from Adafruit Industries Tutorials


Let’s start with the very basics of Arduino programming. We’ll use the Arduino “Integrated Development Environment” (IDE) software to connect with the Arduino and send your customized code to it.

The first code we’ll develop will be to blink a Light Emitting Diode (LED) located on the Arduino board.


STEP 1: The 'L' LED

The Arduino has rows of connectors along both sides that are used to connect to electronic devices and plug-in 'shields' that allow the Arduino to do more.

However, the Arduino also has a single LED that you can control from your sketches. This LED is built onto the Arduino board and is often referred to as the 'L' LED as this is how it is labelled on the board.

The position of this LED is circled in red on the pictures of the Arduino Uno and Leonardo below.

image of Arduino board with LED highlighted

STEP 2: Loading & Saving the Blink Example

You may find that your Arduino board's 'L' LED already blinks when you connect it to a USB plug. This is because Arduino boards are generally shipped with the 'Blink' sketch pre-installed.

In this lesson, we will reprogram the Arduino with our own Blink sketch and then change the rate at which it blinks.

In Challenge 5.2, you setup your Arduino IDE and made sure that you could find the right serial port for it to connect to your Arduino board. The time has now come to put that connection to the test and program your Arduino board.

The Arduino IDE includes a large collection of example sketches that you can load up and use. This includes an example sketch for making the 'L' LED blink.

Load the 'Blink' sketch that you will find in the IDE's menu system under File → Examples → 01.Basics

Arduino IDE 01.Basics menu

When the sketch window opens, enlarge it so that you can see the whole of the sketch in the window.

Arduino IDE 01.Basics Blink

From the File menu on the Arduino IDE select the option 'Save As..' and then save the sketch with the name 'MyBlink'.

You have saved your copy of 'Blink' in your sketchbook. This means that if you ever want to find it again, you can just open it using the File → Sketchbook menu option.

Arduino IDE sketchbook menu

STEP 3: Uploading Blink to the Board

Attach your Arduino board to your computer with the USB cable and check that the 'Board Type' and 'Serial Port' are set correctly. You may need to refer back to Challenge 5.2.

The Arduino IDE will show you the current settings for board at the bottom of the window.

Arduino IDE board status

Click on the 'Upload' button. The second button from the left on the toolbar.

Arduino IDE upload button

If you watch the status area of the IDE, you will see a progress bar and a series of messages. At first it will say 'Compiling Sketch..'. This converts the sketch into a format suitable for uploading to the board.

Arduino IDE compiling

Next, the status will change to 'Uploading'. At this point, the LEDs on the Arduino should start to flicker as the sketch is transferred.

Arduino IDE Uploading

Finally, the status will change to 'Done'.

Arduino IDE done

The other message tells us that the sketch is using 1,084 bytes of the 32,256 bytes available.After the 'Compiling Sketch..' stage you could get the following error message:

Arduino IDE error

The clue is at the top here, it probably means that your board is not connected at all, or the drivers have not been installed (if necessary) or that the wrong serial port is selected.

If you get this, go back to Challenge 5.2 and check your installation.

Once the upload has completed, the board should restart and start blinking.

STEP 4: How Blink Works

Here is the code for the Blink sketch.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

The first thing to note is that quite a lot of this sketch is what is called 'comments'. Comments are not actual program instructions, they are just comments about how the program works. They are there for out benefit, so that there is some explanation to accompany the sketch.

Everything between /* and */ at the top of the sketch is a block comment, that explains what the sketch is for.

There are also single line comments that start with // and everything up intil the end of the line counts as being a comment.

The first actual line of code is: int led = 13;

As the comment above explains, this is giving a name to the pin that the LED is attached to. This is 13 on most Arduinos, including the Uno and Leonardo.

Next, we have the 'setup' function. Again, as the comment says, this is run when the reset button is pressed. It is also run whenever the board resets for any reason, such as power first being applied to it, or after a sketch has been uploaded.

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

Every Arduino sketch must have a 'setup' function, and the part of it where you might want to add instructions of your own is between the { and the }.

In this case, there is just one command there, which, as the comment states tells the Arduino board that we are going to use the LED pin as an output.

It is also mandatory for a sketch to have a 'loop' function. Unlike the 'setup' function that only runs once, after a reset, the 'loop' function will, after it has finished running its commands, immediately start again.

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Inside the loop function, the commands first of all turn the LED pin on (HIGH), then 'delay' for 1000 milliseconds (1 second), then turn the LED pin off and pause for another second.

STEP 5: Blinking Faster & Patterns

You are now going to make your LED blink faster. As you might have guessed, the key to this lies in changing the parameter in () for the 'delay' command.

This delay period is in milliseconds, and so if you want the LED to blink twice as fast, change the value of 1000 to 500. This would then pause for half a second each delay rather than a whole second.

Upload the sketch again and you should see the LED start to flash more quickly.

Can you make the LED blink in an interesting pattern?

Try copying and pasting the write/delay code (below) to repeat it several times in the loop. What happens when you adjust the multiple delay times so they are not all the same?

  
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

STEP 6: Share Your Work

Take a video of your blinking Arduino and post it to Edmodo. Add your video, photos or the code to your portfolio page.

Hint: you can make code appear formatted as code (just like in the examples above) by using either the <code&rt; or <pre&rt; tags to wrap the lines of code. The <code&rt; tag is best used inside of a paragraph. The <pre&rt; tag works in place of a paragraph.