Arduino is an open-source hardware and software platform. It is commonly used by hobbyists, DIYers, students, and working professionals. It is my personal favorite prototyping platform due to its ease of use and cost. Today, I am going to help you get started using Arduino by walking you through downloading and installing Arduino IDE, writing an Arduino Sketch to blink an LED, and implementing the sketch by uploading it an Arduino board.
Estimated Time to Complete: 30-45 Minutes
Parts and Equipment Needed
Arduino Board (Uno, Nano, Mega, etc.)
Computer w/ Internet connection
Table of Contents
Installing Arduino IDE
Building the Circuit
Writing an Arduino Sketch
Uploading the Arduino Sketch and Flashing the LED
Appendix: Arduino Code as Text
1) Installing Arduino IDE
Arduino IDE is Arduino’s open-source software integrated development environment. An IDE consists of all the necessary tools for software development. To use your Arduino board you will need to download the Arduino IDE and use it to edit your source code and then upload your code to the board. Arduino IDE is available for Windows, Mac, and Linux.
Download and follow the necessary install steps for your machine.
2) Building the Circuit
The circuit we are building is really simple. I am using a breadboard to make the circuit; feel free to solder the components together or to make a shield out of a protoboard for your Arduino. I like to make Fritzing schematics of my circuits before building them (Figure 1: Fritzing Schematic). Fritzing is an open-source schematic capture and PCB routing software. If you wish to download Fritzing, you can find it here: http://fritzing.org/home/.
Figure 1: Fritzing Schematic
The LED and resistor should be connected in series between Digital I/O Pin 3 and a ground pin (Figure 2: Arduino Board Connections). The resistor is there to limit current through the LED and should be sized accordingly depending on your LED to prevent burning it out.
Figure 2: Arduino Board Connections
Figure 3: LED and Resistor on the Breadboard
3) Writing an Arduino Sketch
Once you’ve successfully installed Arduino IDE, it’s time to start coding. The source code files for Arduino are called sketches. The Arduino programming language is based off C/C++ and is very similar. Open Arduino IDE and a new blank sketch will appear on your screen (Figure 4: New Arduino Sketch).
Figure 4: New Arduino Sketch
The sketch is divided into two program parts: a) setup and b) loop. I like to add a header to all my source code, giving the code a title, date, description, and version if necessary (Figure 5: Source Code Header).
Figure 5: Source Code Header
The next step would be to include any necessary libraries, but since our code does not use any libraries we can skip this step.
Next you will write any global variable definitions. This step is not vital. However, when working on more complex code, making these definitions can simplify the code and make it easier to edit. When writing a variable definition, you are assigning a value to a variable. In this case I will define Digital I/O Pin 3 on my Arduino Uno as my LED output pin (Figure 6: LED Pin Definition).
In the future if I want to change the pin that outputs to the LED I only have to change this definition; I will not have to change any other code.
Figure 6: LED Pin Definition
The setup part of your code is where you make necessary hardware and software configurations. This part of the code runs only once. Since we are driving an LED, we configure the digital I/O pin we have our LED tied to as an output pin (Figure 7: Pin Configuration).
Figure 7: Pin Configuration
Now that we have finished setting up the Arduino we can write the main body of the code. This will go under the loop section and will repeat over and over unless otherwise stated or until power is removed from the Arduino.
To flash the LED on and off every second we write the following commands (Figure 8: LED Flash Loop):
1) Turn LED On
2) Wait ½ of a second (500 milliseconds)
3) Turn LED Off
4) Wait ½ of a second
Since the code we write is within the loop function, the Arduino will automatically repeat the code over and over.
Figure 8: LED Flash Loop
4) Uploading an Arduino Sketch
Connect the Arduino board to your computer through USB. Once the Arduino is connected, follow these steps to upload the sketch:
1) Select the target board (Figure 9: Target Board Selection)
The target board selection tells Arduino IDE which Arduino board you are uploading to
Note: If you have a different board then the Arduino Uno, select that board
Figure 9: Target Board Selection
2) Select the serial port the board is connected to (Figure 10: Serial Port Connection)
Note: On Windows this will typically be a COM port
Figure 10: Serial Port Connection
3) Press the “Upload” button to upload the sketch to the Arduino (Figure 11: Upload Button Location)
Figure 11: Upload Button Location
Congratulations! Your LED should now be flashing OFF (Figure 12: LED Off) and ON (Figure 13: LED On) every second. You have just installed Arduino IDE and used it to successfully write and upload your first Arduino sketch.
Figure 12: LED Off
Figure 13: LED On
5) Appendix: Arduino Code as Text
/* Project: LED_Blink
* Written by:Chris Marella
* Date: January 4, 2017
* Description: The following code will flash an LED on and off.
constintLED=3;//define digital pin 3 as LED output
pinMode(LED,OUTPUT);//configure the LED pin as an Output
//Flash a LED on and off every second
digitalWrite(LED,HIGH);//Turn LED on for 1/2 a second
digitalWrite(LED,LOW);//Turn LED off for 1/2 a second