HomeSTEM HQ Getting Started with Arduino Arduino Lesson 1

 

Lesson 1: My First Arduino sketch - Blink an LED

Arduino Reference page

     
           
     

Connecting Computer and Arduino Board

So, you have your Arduino board, and you have downloaded and installed the Arduino IDE and the required drivers for your system (if required). Open the IDE and connect the board via USB to your computer (I will be showing screenshots from my MacBook, but the main features should be readily transferable across platforms.)

Go to the Tools menu and check under "Board" and "Serial Port". You should see something similar to the following:

You will need to ensure that you select the correct board (there are many varieties, so make sure that you have the right one!). Then check that your serial port is active - if not, try unplugging and replugging the board, or quitting and restarting the software. Occasionally, I have needed to reboot my computer with the device plugged in. Normally, though, it should work automatically - there is good trouble-shooting support at the Arduino site.

 

Back to Top

 

My First Sketch

From the File menu, select 'New' and you should see something similar to the images above. The sketch is named with the date (you should rename it to something meaningful when you save it!), and consists of two parts: 'setup' and 'loop'. As the names imply, the first section is used to initialise variables and other housekeeping, the loop is where things happen - over and over! (The "void" means that the software does not expect any result returned from these functions, unlike most functions which return a result).

Before we try creating our own sketches, it is highly useful to learn from what others have done before us! Go to the File menu once again, and choose 'Examples'. You should see many sub-menu options, with the first ten being excellent tutorials. Begin with Basics, and choose 'Blink'.

As noted in the text, this is the essential starting sketch - doesn't get much simpler. Most of the text you will see (everything in green) is just comment to explain what is going on). In fact, if you copy and paste the non-green text, what you get is very simple:

Back to Top

#define LED LED_BUILTIN

void setup()
{

pinMode(LED, OUTPUT);
}

void loop()
{

digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}

Back to Top

You can copy the text from here and paste it into your 'new' sketch, or just use the example sketch provided by the software.

Look closely at each line of the sketch above. It begins by defining a variable (called 'LED') which represents a special term - 'LED_BUILTIN' (on most boards, this corresponds to PIN 13, so the first line of code could have been #define LED 13. However, for the boards that use a different pin for the LED, the term LED_BUILTIN is clearly a safer option for a generic sketch). The code is case-sensitive, so be careful - well worth a browse of the Arduino Reference page. Also make sure you end each command line with a semi-colon (';') - my most common cause of errors!

In the setup, we tell the board that the in-built led will be used as an output. Then we write to that component - 'HIGH' will switch it on, 'LOW' will turn it off, and in between there will be a 1 second delay (yes - the delay is measured in milliseconds).

Back to Top

 
 

Compile Your Sketch (check for errors)

It all looks OK, so first we check our code by 'compiling' it. Click on the check mark in the circle at the top left of the window and watch the bottom of the window - lots of text flows past as it verbosely reports everything that is happening, and then (hopefully) it will report 'Done compiling'. If not, then go back and check that there is a semi-colon where required, or have a look at the error message in the report.

Back to Top

 
 

Upload Your Sketch (send to the board)

If all is correct, then you can proceed to 'upload' the sketch to the board using the 'upload' button (round right-pointing arrow next to the Compile button at the top left of the window.) Uploading may take a little while, even for a short sketch like this (mine takes around 30 seconds). Once you finish and get the "Success" message, you should see the LED flashing on and off on your board.

Back to Top

 
 

Congratulations! You have run your first sketch!

Now go back and make some changes to your sketch to see the results. Then try a few more examples from the examples menu! Much fun awaits!

Back to Top

 
 

HomeSTEM HQ Getting Started with Arduino Arduino Lesson 1