Introduction to Arduino – “Blink”

Tutorial Series Introduction

Have you always wanted to do amazing projects combining the flexibility of software with potential of hardware the  but never knew where to start? This tutorial series will provide an introduction to hardware projects from the perspective of a complete beginner. I will document every meticulous detail and when it is applicable, list multiple methods to accomplish the same task yielding a complete understand of the project. In addition to documentation I will make all source code available and post a video walk through of each tutorial. Video tutorials can be found at the One Stop Programming YouTube channel. The link for this video is listed below. As the tutorial series progresses I will incorporate special topics for specific projects and provide the same level of detail and resources for more advanced topics. Words that appear in Italics with an asterisk(*) are terms that are defined at the bottom of the page.

Advertisement:

Overview

In this first tutorial we will do what I consider the “Hello World” of the Arduino world: Blink. The Blink sketch* is provided within the Arduino IDE and does not strictly require any external circuitry and we will use it to verify that the board is working correctly and is programmable. We will expect to see an LED* on the board blink in sync with the LED that we wire in externally.

Required Hardware/Materials

      • (1) Arduino UNO R3 or Compatible Alternative (A compatible alternative is recommended as they are just as functional/reliable, however, they do not carry the Arduino brand)
      • (1) Breadboard and Jumper Wire Kit
      • (1) LED (Any Color)
      • (1) 200Ω – 300Ω Resistor (This is a recommend range but any resistor above 100Ω should be sufficient)
      • (1) Tactile Button

LEDs and compatible resistors can be purchased as a kit here. All items required for this tutorial can be purchased on Amazon through kjdElectronics. The kjdElectronics Starter Kit for Arduino provides the necessary hardware for this tutorial in one convenient hassle free package. Note that the Arduino/Arduino Compatible board is sold separately. Special Offer: When you purchase the Starter Kit and a Funduino (A Funduino is a low cost Arduino Compatible Board) from kjdElectronics use promo code LRWZNU35 to get $5 off your purchase.

Part 1 – Testing Your Board

For the first part of the test no circuit is required. If you have not yet downloaded the Arduino IDE then do that now. It can be downloaded from the Arduino Website. Download and install the most recent version of the IDE. After installing (or if you had the Arduino IDE previously installed) launch the IDE. Then connect your Arduino (or other compatible board) to the computer. After being connected you should see the power LED and other LEDs light up. If this does not happen you are experiencing some issues. Post on the forum and someone will try to assist you. Next, from the Arduino IDE click the “Sketches” and under basic select “Blink.” The blink sketch will now load in a new code window. Click the “Upload” button and begin uploading the sketch to the Arduino. If necessary select the correct COM port that the Arduino is on. For more information on this process review the You Tube video (Coming Soon) that compliments this tutorial. Verify that the LED labeled as L is blinking on the Arduino. Blink Test

Part 2 - Building the Blink Circuit

Now that we have verified that board is working and have practiced uploading a sketch lets add on by building our first circuit. In this section you will need to get out your breadboard, jumper wires, an LED and a resistor. I would recommend that you first read the diagram below and build the circuit. After you have built the circuit continue reading for a description of the purpose of each component. While understanding the function of each component is very important, the ability to read schematics and diagrams is critical and should be developed early.

Diagrams made with Fritzing 0.8.7.

Blink circuit with external LED.

Above you see that we have made several connections. First, connect the 5V terminal on the Arduino to the red rail on the breadboard. Note that red and blue rails are for power (+5V and 0V GND (Ground)). Each point on the breadboard on these rails is connected horizontally in the image above, however, they are not connected across the board. That is why it is necessary to connect to connect these across the board. The other points on the breadboard are connected in vertical columns broken in the middle. In the picture above the 5 points in the top left (excluding the power and rails) are connected in a vertical column. This allows you to make multiple connections that are electrically at the same node (or point). Next, we insert the LED into the breadboard. Note that the longer leg of the LED is the positive terminal and the shorter leg is the negative (GND). When current flows through the positive terminal of the LED to ground light is produced by the LED. However, it is necessary to limit the amount of current through the LED as to much current will destroy it. This is where the resistor comes in: recall ohms law Voltage=Current * Resistance (V=IR). If we are using 5V and a 200Ω resistor the current through the LED is equal to V/R=I => I=V/R. Therefore I=5/200=0.025A which is safe for our LED. If no current limiting resistor is used the equation becomes I=5/0…which equates to an extremely high current that burns out the LED.  With this in mind we don’t forget to connect the negative terminal of our LED through a resistor to GND. The positive terminal of the LED is connected to pin 13 on the Arduino. Why pin 13? Because in the blink code pin 13 is defined as the default led variable. With that subtle transition let’s move onto the code.

Code

/* 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
} What happens when the code is executed? First, you a variable named “led” is defined and given the value of 13. Next, the setup() function runs one time at the beginning of each Arduino sketch. The setup function sets the pin defined by the led variable to “OUTPUT” mode. This pin setup is required before using any Arduino pin. After the setup function completes the loop() function begins executing. The loop function will loop forever until the Arduino is reset or looses power. Within the loop function we make a digitalWrite to out led variable and specify a value of “HIGH.” HIGH connects the pin specified by the led variable to +5V. HIGH can be thought of as “on,” powered, or +5V. Next the program will sleep for 1000 milliseconds (1 second). Then, another digitalWrite is performed setting the led to “LOW” followed by another delay. Similarly, LOW is “off.” GND, or 0V. The digitalWrite command simply either sets the specified pin high or low. Digital values can only be 0 or 1. In this case 0 is off and 1 is on. When the output is set HIGH the led will be provided with 5V and current will through the LED producing light. When the output is LOW the voltage on the positive terminal of the LED will be zero and the LED will be off. Congratulations! You implemented the blink sketch. This is just the first step in an entire new world of possibilities. To continue play with the length of the delays and move the led to different pins. In the next tutorial we manually control the LED with a button.

Advertisement:

Defined Terms

Sketch: A sketch is an Arduino program/file that contains all of the code necessary to function. This is the “code” portion of the project. LED: Light emitting diode