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.
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.
Welcome to Arduino!
The first step in learning Arduino is to download the software. This can be found at arduino.cc. There are the step by step instructions in the images above.
It is recommended to make sure that the install has worked by opening the arduino app on your computer. In our next tutorial, we will go through how to use this interface and connect it to your physical Arduino device.
NOTE
If you have a chromebook, you will not be able to use this method and instead will need to create an account on Arduino Create to gain access to the web editor version.
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:
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.
If done correctly this completes the circuit and will make our next step, coding the Arduino, our final step.
If you have used the NuVu Platform before AND you remember your password, then skip to STEP 4 and 5. If this is your first time using the Platform or if you have forgotten your password, then follow all of the steps below.
STEP 1: In the top-right corner, click the word "login."
STEP 2: When the black menu appears, click "reset password," and enter your school email address, and then click "Send Password Reset Link."
STEP 3: Check your school email account for an email from NuVu. Be sure to check your junk email folder as well if you don't see the email in your inbox. Follow the instructions in the email to reset your password.
STEP 4: You should now see your name in the top right corner. Success! (your screen will look slightly different than mine). Under "Studios" click on our current studio, "Cyborg Enhancements". You can also find this studio by clicking on your name in the top right corner.
STEP 5: You are now on the landing page for our studio for the rest of the term! To let me know that you have succeeded in making it this far, make a celebratory comment under the post "comment here!" This post will only appear if you have logged in successfully.
To report a bug or see release notes, visit the platform page.