Start Here - Lesson 1

Getting Started with Arduino

>> Jump to: Get the Arduino Software
>> Jump to: Connecting the Arduino
>> Jump to: The Arduino Sketch
>> Jump to: Variables
>> Jump to: Functions
>> Jump to: setup() and loop()
>> Jump to: The "Blink" Sketch
>> Jump to: Modifying the "Blink" Sketch
Please review the Official Kit List to ensure you have all components used in the following lessons.

Please review the  FAQ for important assembly instructions.

RoboGames 2016 is the largest, most diverse event we have ever attempted to date. Over 40 teams across the region will compete for the title of RoboGames Champions. Whether you are a first time novice, or a multi-year attendee who has competed before, you will have a great time learning about building a robot and meeting other people who share your passion.

Please take the time to review these lessons, links and code prepared by Nelson Tech Club President, Brad Pommen which will be updated weekly. The purpose of this site is not to recreate everything already out on the internet about building an Arduino robot, but to instead help kickstart your Arduino skills which will lead to the most successful experience.

Our official base kit contains the starting materials of what you can use, the rest is up to you. The code examples and tutorials provided will ensure that all the hardware included works together and gets rid of guess work when hooking it up. Teams will decide what components they want to use, what their robot can do and how it looks. Safety is our primary concern, so if in doubt, send an email and ask if your idea is allowed. Updates to the FAQ section will be happening frequently to keep you up-to-date.

Get the Arduino SoftwareBack to Top

Arduino is at the heart of the RoboGames robots!

Ensure that you have the Arduino software already installed before proceeding.

The Arduino software is sometimes referred to as the Arduino “IDE”. We recommend installing version 1.6 or newer from the Arduino website.

Download Arduino


Download the Software Here

The open-source Arduino Software makes it easy to write code and upload it to the board. It runs on Windows, Mac OS X, and Linux. This software can be used with any Arduino board. Refer to the Getting Started page for Installation instructions.

Arduino is based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a some other trigger - and turn it into an output - activating a motor, turning on an LED, creating noise. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language and the Arduino Software together to accomplish this.


Windows (Installer vs ZIP file)

The Windows version of Arduino is offered in two options: an installer or a zip file. Depending on the computer you are trying to use, the installer is the easier of the two options, just download that, and run the executable file to begin installation. If you’re prompted to install a driver during installation, select “Don’t Install”. Don’t forget which directory it installs to (defaults to “Program Files/Arduino”).

If, instead, you choose to download the zip file version of Arduino, you’ll need to extract the files yourself. Don’t forget which folder you extract the files into! We’ll need to reference that directory when we install drivers. This is the preferred method for running the Arduino software if you are having issues with the installer or computer privileges.

Additional Help

Sparkfun Windows Instructions


Mac OS X

The Mac download of Arduino is only offered in a zip file version. After the download is finished, simply double-click the .zip file to unzip it. Following that, you’ll need to copy the Arduino application into your applications folder to complete installation. You will need to run the Arduino application from the folder (or Desktop) directly since you may not have the correct priveledges to make changes, install or otherwise use the program fully.


Linux

Instructions for Linux here.

Connecting the ArduinoBack to Top


Grab the RoboRED “Arduino” microcontroller and USB cable.

Check that the switch on the Arduino board is set to “5v”. This means it will run on 5 volts.

Plug the the Arduino board to your computer with the USB cable.

Your computer may ask you to install a “driver” for your device. You will want to follow those instructions and when asked for a “location”, choose the install location of your Arduino software and it will find the files it needs.

Next, open the Arduino software.
A default, blank program will be loaded automatically.


We need to make sure that the software and Arduino board are properly configured.
Click the TOOLS menu, then choose Board -> Arduino/Genuine UNO
Click the TOOLS menu, then choose Port, then the serial port of the Arduino

Unless you already know which port to use, typically it will be the highest number listed because it was the last device to be plugged in.

Now it’s time to to upload your first program, also referred to as an Arduino “sketch”.


Click on the “Upload” button.


If you see any error messages, it is most likely caused by incorrectly setting the “Board” or the “Port” value. You might have to try another “Port” in the list if there is more than one until you are successful. If you are still without success, you may not have the correct “driver” installed.

If you see the message, “Upload Successful”, your Arduino board and software are properly configured and you are ready to move on.

The Arduino SketchBack to Top

A sketch is the name that Arduino uses for a program. It's the unit of code that is uploaded to and run on an Arduino board.

Uploading the “Blink” sketch.

Click the FILE menu, then choose Examples -> Basics -> Blink to open the “Blink” sketch.
Review your “Board” and “Port” Settings to ensure they match the initial settings from the previous step if you encounter any issues.

Click on the “Upload” button to send the program to the Arduino board.

If the uploading was successful, a light on the Arduino board should now be flashing on and off every 1 second. This is one of the simplest programs to use and understand in the Arduino examples.


The first few lines of the Blink sketch are a comment.
Everything between the /* and */ is ignored.

/*
This type of comment is for long sentences.
It is also for comments that need multiple lines.
That go on
and on
and on...
*/


If a line starts with // , thats is also a comment for the entire line.

// This comment is for the whole line, but only one line
// You can make another comment by using the // again


These types of comments can start // anywhere in the line and continue to the end of the line.

int ledPin = 13;  // Here is a comment after some code


Comments are very important to explain what a program does. It helps you remember important details and improve readability of your code for yourself and others. Think of this as instructions for humans. If you write code today, you might not remember what it does in a couple months, so it is important to document your code with comments that explains what and how it is doing.

Everything else in the sketch is computer “code”, which simply means “instructions for computers”. Since computers can only do what you tell them to do in a way they can understand, you must pay attention carefully and write simple, clear commands. Capitalization, correct spelling and proper punctuation are all extremely important to check.

NOTE: You will need to end nearly every line in your program with a SEMICOLON.


  ;



Think of this like a PERIOD at the end of a sentence. The software needs to know when your commands is done, otherwise it looks like a  “run on sentence” in your code and it won’t understand what to do.

VariablesBack to Top

A variable is a place for storing a piece of data. It has a name, a type, and a value.
For example, the line from the Blink sketch below declares a variable with the name ledPin, the type int, and an initial value of 13. It's being used to indicate which Arduino pin the LED is connected to.

int ledPin = 13;


Variables can be changed in the program, others stay the same. You need tell the software that a variable exists before you can use it. You will name it, declare what type of data it will be and then what the initial value will be.

FunctionsBack to Top

A function is a special piece of code that can be used multiple times in a sketch. It is a container to defines a small repeatable process and you can make as many as you need.

The “void” keyword that precedes the function name is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called. Functions have the ability to return data such as an integer or a string, if nothing will be returned, it is void.

void DoSomething()
{
  // put the code to “do something” between the brackets
}


You can then use the code DoSomething(); as often as you need. Once you have defined what the function is supposed to do, it is useful for tasks you do over and over and over and over.

For a robot, that might look like:

Move forward, turn left, move forward, turn right, move forward, move forward, stop.

If you had to write the code for “move” every single time your robot had to in the above example, it would be time consuming and increase your chances of making a mistake. Being able to reuse code is an important lesson to learn and you should do it regularly.

A function for “MoveForward” might look like:

void MoveForward() {
  // make the robot go forward
  // wait 1 second
  // stop the robot
}


Notice the 3 “comments” in the function above. There is no actual commands to make the robot move included yet, but we have described the steps required to move forward. This is a great way to help identify each step that is required in our function without getting lost learning the code to do it. This “logic” and will be the basis of good programming habits. Figure out what your function’s purpose is, then include a comment that explains the steps required. If you can’t put your step into a couple words, how will you tell your robot what it is supposed to do?

If something is too complicated to put into a short description, you might have to break it down further into smaller, simpler steps also known as a subroutines. This would be functions, calling other functions.

setup() and loop()Back to Top

There are two special functions that are a part of EVERY Arduino sketch, the setup() and loop().

The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and over and is heart of most sketches. You need to include both functions in your sketch, even if you think don't need them for anything.

setup = do this once at the beginning

loop = do this over and over again


Below is the setup and loop functions from the Arduino “Blink” example.

The first line provides information about the function, like its name, "setup". The text before and after the name specify its return type and parameters: these will be explained later. The code between the { and } is called the body of the function, it tells you what the function does. The brackets indicate where the function starts and ends.

The loop function runs over and over again - forever. In the code example below, the LED will turn on and off every 1000 milliseconds or “1 second”. It will continue to do this over and over.

void setup() {
  pinMode(13, OUTPUT); // pin 13 as an output
}

void loop() {
  digitalWrite(13, HIGH); // turn the LED on
  delay(1000); // wait for 1 second
  digitalWrite(13, LOW); // turn the LED off
  delay(1000); // wait for 1 second
}


The command “delay” is a pre-defined Arduino function used to pause the program for an amount of time, specified in milliseconds.

The "Blink" SketchBack to Top

The Blink Program Using Built-In LED on Pin 13



void setup() {
  pinMode(13, OUTPUT);
}

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
}

Modifying the "Blink" SketchBack to Top

Changing the code so that the LED is on for 1500 milliseconds and off for 500 looks like this.

void setup() {
  pinMode(13, OUTPUT); // pin 13 as an output
}

void loop() {
  digitalWrite(13, HIGH); // turn the LED on
  delay(1500); // wait for 1.5 seconds
  digitalWrite(13, LOW); // turn the LED off
  delay(500); // wait for 1/2 a second
}


Changing the code so that the LED turns on when the sketch starts and stays on. It makes use of the setup function only, but you still need to include the “empty” loop function.

void setup() {
  pinMode(13, OUTPUT); // pin 13 as an output
  delay(50); // wait for 50 milliseconds
  digitalWrite(13, HIGH); // turn the LED on
}

void loop() {
  //no other commands, so the light stays on forever
}


Changing the code so that the LED turns on when the sketch starts and then starts to blink on and off after a short pause at the start.

void setup() {
  pinMode(13, OUTPUT); // pin 13 as an output
  delay(50); // wait for 50 milliseconds
  digitalWrite(13, HIGH); // turn the LED on
  delay(1500); // wait for 1.5 seconds
}

void loop() {
  digitalWrite(13, HIGH); // turn the LED on
  delay(500); // wait for 1/2 a second
  digitalWrite(13, LOW); // turn the LED off
  delay(500); // wait for 1/2 a second
}


Try changing the values further on your own.