In this tutorial you will learn how to connect and control an LED on an Arduino Uno development board.
What Is Needed
- Linux, macOS, Or Windows Based Computer With A USB Port
- Arduino IDE
- Arduino Uno (available on Arduino and SparkFun) With Compatible USB Cable
- Solderless Breadboard (available on Adafruit and SparkFun)
- 2 x Male-Male Jumper Wires (available on Adafruit and Arrow)
- Standard 5mm Red LED (available on SparkFun and Mouser)
- 330 Ω Resistor (available on SparkFun and Amazon)
A light emitting diode (LED) is a semiconductor device that emits light when energized and comes in an assortment of different colors. It is similar to a light bulb, but is much smaller and consumes much less power. LEDs are typically used as indicator lights in consumer electronics.
A solderless breadboard will be used to assemble the electronic circuit. These breadboards allow hobbyists to construct circuits without having to permanently (for the most part) attach your electronic components or having to deal with soldering them together. If you are not familiar with solderless breadboards, please refer to How to Use a Breadboard and Build a LED Circuit for additional information.
Although the Arduino platform has many Arduino development boards of different sizes and purposes available, we will be using the Arduino Uno that is by far the most popular Arduino board in use today. My board is an Arduino Uno R3. If you are using a different model, the vast majority of this tutorial should still apply, however, some minor changes may be necessary.
The Arduino IDE (Integrated Development Environment) is the application used to develop and upload programs to an Arduino development board. The programming language used by the IDE is Sketch which is based on C++ and allows for the use of standard C and C++ library routines. A file containing a Sketch program is called a sketch and has a .ino extension. It is my understanding the Arduino IDE works the same across all platforms, however, I am using the Arduino Desktop IDE on macOS for this tutorial so you may see some slight differences.
The Arduino website is a great resource for hobbyists looking to purchase and experiment with Arduino development boards. The Getting Started page is a great place to start if you are new to the Arduino platform. They also provide tutorials, a Sketch language reference, and many more resources. I urge you to check it out if you haven’t already.
Building The Circuit
Attach the red LED to the solderless breadboard by connecting one end of the LED to one of the breadboard’s terminal strips and the other end to an adjacent terminal strip. The terminal strips are the horizontal rows of terminals grouped together in sets of 5 in the center of the board. The anode (positive terminal) of an LED is longer than the cathode (negative terminal). Connect one end of the 330 Ω resistor to the terminal strip containing the LED’s anode and the other end of the resistor to an empty adjacent terminal strip.
Next we will connect the LED and resistor circuit to the Arduino Uno board. Attach one end of a jumper wire to the breadboard’s terminal strip containing the open end of the resistor. Attach the other end of the jumper wire to pin D2 on the Arduino Uno DIGITAL pins header. This is the 3rd pin up from the bottom.
Attach another jumper wire from the terminal strip containing the open end (cathode) of the LED on the breadboard to the ground (GND) pin just above D13 on the Arduino Uno DIGITAL pins header. Once completed, your circuit should look like the one shown below.
The circuit is complete so you can now connect the Arduino Uno to your computer via the USB cable.
Writing The Software
If you have not done so already, install and configure the Arduino IDE for your particular operating system according to the instructions on Arduino’s website.
Once the Arduino IDE is configured, select File > Examples > 01.Basics > Blink from the main menu to open the example sketch we will use in this project. This example sketch already contains the software to blink an LED, albeit one already built onto the board. We will modify the code to use our LED circuit later.
Peruse through the code and look at the various functions defined and used. The
setup() function runs only once and is where we place all initialization code, like setting the pin mode in this example. The
loop() function runs repeatedly over and over after the
setup() function has completed. This is where we put code that is always in action. In this case, the
loop() function repeatedly turns on and off the built in LED with 1 second delays in between. The
delay() function’s argument is 1000 in this case because it is expecting milliseconds.
Let’s run the example sketch without modification and watch it in action. To upload and run the sketch on the Arduino board, select Sketch > Upload from the main menu. The status bar just below the code area will display
Compiling sketch…, then
Done Uploading. and the message area below the status bar will display information about your uploaded sketch. Once the sketch has been uploaded to the Arduino development board, it will begin running automatically on the board. You should now see the built in LED blinking.
You can also accomplish this task by clicking on the right arrow button (Upload) located on the left hand side of the toolbar at the top of the Blink sketch window. This toolbar provides a convenient way to run commands without always having to go through the main menu.
Next, let’s change the code to use our LED circuit instead of the built in LED. Change line 28 of the Blink sketch that sets the specified Arduino pin to be an output from
2 refers to pin D2 we used previously to connect our circuit to the Arduino Uno DIGITAL header. Now let’s change line 33 from
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
and line 35 from
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
for turning on and off the LED respectively.
Next, let’s verify and save our updated sketch program. To verify the code compiles correctly, select Sketch > Verify/Compile from the main menu or click the check mark button (Verify) located at the far left of the toolbar and fix any syntax errors that are displayed. Once that is done, save the sketch by selecting File > Save As…, entering the name and location of the new sketch, and finally clicking the Save button. The Arduino IDE will create a new directory with the same name as the one you entered that contains a file with the same entered name and an .ino extension.
Click the Upload button, located just to the right of the Verify button we clicked earlier, to run our updated sketch. You should now see the LED blinking in the circuit you built. Congratulations, we now have a blinking LED!
Before closing the Arduino IDE and disconnecting the board from your computer, it is always a good idea to run the BareMinimum sketch to reset all pins back to their default state in order to avoid accidental damage to the Arduino board. It never hurts to be extra cautious, right? This sketch is located at File > Examples > 01.Basics > BareMinimum from the main menu and contains only empty
loop() functions. After loading the sketch, click the Upload button to run the BareMinimum sketch.
You can now safely exit the Arduino IDE application and disconnect your Arduino Uno from your computer.
In this tutorial we learned how to construct an LED circuit, attach it to an Arduino Uno development board, write a Sketch program in the Arduino IDE, and run the sketch on the board to blink an LED.
The final sketch is located on GitHub for your reference.
Thank you for joining me in this journey and I hope you enjoyed the experience. Please feel free to share your thoughts in the comments section below.