Self-Guided Activity: Introduction and Hardware

Max Vanatta

Purpose: This introduction to Arduino is meant as a first step in writing code and using electronics to enhance your project's behaviors.

While there are many tutorials available across the internet, and you will inevitably use a wide variety of these in your project work, this one is meant to ground you in the concepts of Arduino and highlight that no matter what level of computation background you have, you already have the skills to succeed.

The Arduino can be thoughts of as a rather simple object.  It can behave as simply as a person sitting in a room flipping light switches based on a set of instructions, and we are going to treat it with as low a level of apprehension.  

The Board

Imagine you have a friend in a room with multiple switches, but your friend is wearing a blind fold.  If you were to ask your friend to turn on a light, they would have to first know where the light is.  

For the Arduino, the location of the lights is actually the location of 'pins'.  Pins are the electrical connectors which line the sides of the Arduino and look somewhat like a one-prong outlet. There are three types of these pins:

  • Power Pins: These pins can provide power or a ground to any number of sensors. The pins which provide power are available in both 3.3 Volts and 5 Volts, please check your sensor for what it needs.
    The ground, labelled GND, is important because to make almost every single sensor or device work, you need to complete a circuitor complete electrical loop. This means you must connect a positive voltage, such as the 5V pin to a device, and then the other side of the device to the GND pin.  This would keep the device at a constant 5V, and not be controlled by the Arduino code.
  • Analog Pins: These pins can be seen as the eyes and ears of the board and allow high resolution inputs to the board in values ranging from 0-255.
  • Digital Pins: These pins can be both input and output for the board and there are two types. Most of these are only able to input and output High and Low, similar to on and off, or the stereotypical 0 or 1.
    Those labelled with a "~" are called PWM, Pulse Width Modulation, and can output information from 0-255.
    Additionally, these pins can provide up to 5V, meaning that they can be used to complete a circuit with the GND pin.

Adding Components

The Arduino board itself will do very little without adding additional components, but the choice of other components will depend on the goal of the project, so let's make a goal...

We need to blink an LED on and off every second continuously.

What you will need: Arduino, Breadboard, 3x m-m wires, 220ohm Resistor, 1x LED (don't know what all these are?  No worries, we will introduce them)

What is an LED anyways?  An LED, or Light Emitting Diode, is a device which converts energy(electricity) into light.  The word diode in the name also means that the electricity can only flow one direction through the device.  We can know which way due to the length of the electrical pins, longer is positive, or by a small flat spot on the LED itself, flat is negative.

Since we know that the LED will be receiving information from the Arduino (It needs to be told to turn on and off) we know that it will be at a digital pin.  Since these pins can be considered positive(5V), we also know that we will need to use a GND pin.

If we were to connect the LED directly between pin 13 and GND, this LED would receive the full 5V.  This is a problem because many LEDs are not made to convert that much voltage into light to the point where the LED would likely break. Let's avoid this!

To avoid this LED from being overwhelmed, we need to reduce the amount of voltage going through the LED, and this is done with a resistor.  A resistor is a device which converts energy to heat.  While this might seem like a waste of energy, we actually need this to not burn out our LED.

If we now look at the components needed to connect our circuit, there are too many to simply plug it into the Arduino.  This means we need a breadboard which is a board with many holes like the pins on the Arduino but with hidden connections between holes.  Orienting the board portrait style, the two columns on either side of the board are connected and are usually used to create a positive voltage area(red) and a GND area(black or blue).  The rows on either side of the middle line are connected as rows rather than the edges which are columns.

No we have all of our pieces and understanding of the board itself.  Let's build it.

  1. Connect a wire from the GND pin to the black/blue column of your breadboard.  
  2. Connect a second wire from the black/blue column to a row of the breadboard.
  3. Now we plug in the LED.  The short/negative lead of the LED should be in the same row as our previous wire.  The long lead should be in a different row
  4. The resistor should be plugged in so one end is in the same row as the long/positive side of the LED and the other is in a third row.  
  5. Finally connect a wire from this third row to Pin 13 of your Arduino.  

If done correctly this completes the circuit and will make our next step, coding the Arduino, our final step.  


Self-Guided Activity Part 2: From Speech to Code

Max Vanatta

Welcome back for the second part of our introduction to Arduino!

In this section, we will discuss the process of coding, and while there are faster ways to get the code ready for our goal of blinking the LED, this process will be useful in your long career of more complex code projects.

Talk it out

When we start to code our project it is useful to first get our ideas down on paper.  Try to begin by saying what someone else would need to know to perform any tasks that are to come.  An example is: There is an LED connected to the board which we will be turning on and off.

Then go through and describe what you would like to happen.  This is the general behavior of your device such as: Turn the LED on for a second and then off for a second.

Pseudo-code it

Now that we have our talked out version:

There is an LED connected to the board which we will be turning on and off.

Turn the LED on for a second and then off for a second.

We are going to do our first layer of translation called pseudo-coding.  This is a term which refers to a simplified and clearer directions which are closer to coded language.  Here is where you will first begin to think like a computer.  So let's go through a few questions which can help us to define our pseudocode.

  1. What needs to be set up?  For each device:
    1. Where is this device? (Pin Location)
    2. What type of interaction is this? (Output or Input)
    3. What type of pin is being used and how? (Analog, Digital, Digital PWM)

      LED at Pin 13, Output
  2. What happens when you run the code? Some things to consider:
    1. The Arduino only performs one task at a time, so order matters a lot.
    2. The Arduino will loop through your code as fast as it processes the information meaning if you need to have something happen for a certain time, tell it.
    3. Are there decisions which need to be made during the loop? This could be important when you include sensors into your devices.

      LED on
      Wait a second
      LED off
      Wait a second
      Repeat

Putting it in Arduino

Now that we have our pseudocode written out, we will open up the Arduino interface and take our first step into the digital world.

When you first open Arduino, you will see and nearly empty sketch.  This is the name given to Arduino files.  It should look like this:

void setup() {

  // put your setup code here, to run once:


}


void loop() {

  // put your main code here, to run repeatedly:


}


Before moving forward we are going to figure out what we see here.  Just like when we talked through our code, there are two sections: Setup and Loop.  Setup runs once at the beginning of the program and Loop continues repeatedly until stopped.

These sections are bound by curly brackets, { } , which are very important.  These mark out the beginning and end of these two sections.  For example, anything which you would like to run repeatedly must be within the brackets of Loop.

There are also some words which are just word phrases.  These are commented out by putting // in front of the line.  This means the computer will not read these lines, but humans which are helping trouble shoot code or even you after not looking at the work in progress code can read these and know what you are planning.

So now, we will put our pseudocode into the Arduino sketch using the commenting function.  This turns into: 

void setup() {

  // LED at Pin 13, Output  

}


void loop() {

// LED on
// Wait a second
// LED off
// Wait a second
 // Repeat (this one is already included in the section title)

}

Coding it!

Now that we have our pseudo code outlining exactly what we are doing, we will start to translate from the spoken language that we are using to the Arduino coding language.

For out Setup, we will take LED at Pin 13, Output  and translate this to pinMode(13, OUTPUT);  Let's break this down a bit.  'pinMode' is what tells the Arduino what to expect to do and where, in this case it will OUTPUT at pin 13.  At the end of this phrase we put a semi-colon which is similar to a period at the end of a sentence.  Missing this will cause the most errors.

For our loop, LED on  turns into digitalWrite(13,HIGH); which again needs some explaining. 'digitalWrite' is telling a digital pin to do something.  In this case it is pin 13, and the output should be the high value.  Think back to our description of the pins and how the digital pins often are only dealing with High and Low.  In this case, high is on.

Wait a second becomes delay(1000); which is just telling the Arduino to delay the next process by 1000 milliseconds (1 sec).

NOTE: As some of you have probably noticed, the capitalization seems to be different than we use in English, and this is something that we should be careful of.  If for example, you were to type High instead of HIGH, the Arduino would not successfully run your code.

After our translating the new code will look like this: 

void setup() {

  // LED at Pin 13, Output
 pinMode(13, OUTPUT);

}


void loop() {

  // LED on
  digitalWrite(13, HIGH);
  // Wait a second

  delay(1000);
  // LED off

  digitalWrite(13, LOW);
  // Wait a second

  delay(1000);

}

Uploading the Code

Now that we have our code written, we should test it.  This is done using the verify button at the top which looks like a check-mark.  If this finds an error, it will alert you to the error at the bottom of the window in red text.  

If there was no error, then you should click the arrow directly next to the check-mark.  This is the upload button and will send the code to the board itself. 

NOTE: It is necessary to go to the drop-down menu tools, then port, and select your board from that list (which is often only a single option).  Many times you will only have to do this the first time you plug your Arduino in, but some computers and some boards need more selections.  If there is an upload error, and not a code error, this could be why.

If you have followed through to this point, your light should be blinking!  

Congratulations! 

There are more resources which will help you grow here: NuVu Toolbox 

There are also great tutorials through Arduino.cc and generally available through googling "Arduino code" with the name of your sensor or device.