Control an LED light with an Arduino Uno
Arduino Uno tutorials
🕑 This lesson will take about 10 minutes
In this lesson, you will learn how to control an LED (Light Emitting Diode) light using an Arduino Uno board and some code written in C Language. We will connect an LED light to the Arduino and write code to turn the LED on and off.
The Arduino Uno board has two rows of pins. The top row of pins contains Ground and Digital pins (labelled 0 up to 13) and the bottom row has power (3.3V and 5V) and ground pins as well as analog input pins labelled from A0 up to A5. There is also a reset button, a USB cable to connect to a computer, and a power port. We can use a breadboard to attach and lay out components (such as LEDs) that will be connected to the Arduino using jumper wires.
For this project, you will need the following parts.
Required parts
1 x Arduino Uno
1 x Breadboard
1 x LED (any colour)
1 x Resistor (The resistor can be any value between 100 Ohms and ~10K Ohms - this will be explained later. We will use a 220 Ohm resistor in this example.)
2 x male-to-male jumper wires (have pins on both ends)
Wiring circuit
Connecting the LED to the Arduino is quite easy. The LED light has two pins - a short pin and a long pin. The long pin will be connected through a resistor to digital pin 13 on the Arduino and the short pin will be connected to ground (GND) on the Arduino.
Plug the two LED pins into the breadboard (anywhere in the centre). Use one male-to-male jumper wire (any colour will do) to connect from GND on the Arduino to the LED’s short pin (by placing the other end of the jumper wire in an empty slot on the breadboard in front of the short pin, or in the same column). This is indicated by the black wire in the circuit shown below.
Connect a resistor to the breadboard with one end inserted in an empty slot in front of the long wire of the LED, as shown in the circuit diagram below (basically the resistor goes between the long wire of the LED and a jumper wire that will connect to digital pin 13 on the Arduino). The resistor is used to limit the current that goes through to the LED to avoid damaging the LED and the pin on the Arduino board. The resistor you use can be of any value between 100 Ohms and ~10K Ohms. A lower value resistor will allow more current to flow and in turn, will make the LED shine brighter. A higher value resistor will restrict the flow of current, and in turn will make the LED dimmer. In this example, we will use a 220 Ohm resistor but you can use one with a lower or higher value. Learn more about resistors here.
Use another male-to-male jumper wire to connect from the digital pin 13 slot on the Arduino to an empty slot on the breadboard in front of the other end of the resistor (in the same column). This is indicated by the green wire shown in the circuit below. Once everything is connected, you can write and upload the code.
The code
Download and install the free Arduino IDE software. Open the Arduino IDE, and create a new sketch (click File > New). A sketch is a file that contains code for the Arduino. Replace any existing code with the following code.
Upload the code
Click File menu and then select Save. Give your sketch a name and make sure it is saved in your ‘Arduino’ folder (likely in Documents > Arduino). Don’t save it anywhere outside this folder.
Click the Verify button (the ‘tick’ icon) to confirm the code is correct and there are no errors (you should see a message at the bottom of the screen that says “Done compiling” if everything is ok). Compiling the code basically involves checking there are no errors in the code (eg. spelling mistakes, missing brackets or semi-colons) and translating your code into instructions that the Arduino board can understand.
Plug the Arduino board into your computer via a USB cable. Before uploading the code to the Arduino Uno board, click on the Tools menu and make sure Board is set to “Arduino Uno”. Then click on Port from the Tools menu. If you’re using a PC, you should be able to select a COM port that has Arduino Uno next to it (this will be the USB port that the Arduino is connected to eg. COM3). If you’re using a Mac, you will need to select something like “usbmodem”.
Now click the Upload button (the little right-arrow icon). Wait until you see the “Done uploading” message. The LED light connected to the Arduino should now be turning on and off.
There’s actually a built-in LED on the Arduino board connected to pin 13. So if you’ve programmed the Arduino to turn an external LED on/off on pin 13, you should see the LED on the board turn on/off too.