In this tutorial, I’ll show you how to use an Arduino to control LEDs. This is a pretty simple project, but you should learn how to do it early on because lots of other sensors and modules are programmed the exact same way.

First I’ll show you how to turn on and off the Arduino’s on-board LED. Then I’ll show you how to turn on and off an LED connected to one of the Arduino’s digital pins. I’ll explain how to change the LEDs flashing rate, and how to change the pin that powers the LED. At the end I’ll show you how to control multiple LEDs. Before starting, you should have the Arduino IDE software installed on your computer.

Electrical Signals

The Arduino communicates with modules and sensors by switching on and off electrical current. It’s very similar to the one’s and zero’s in binary code. When current is switched on, it’s known as a “HIGH signal”. That’s comparable to the “one” in binary code. When the current is switched off, that’s a “LOW signal”, which is similar to the zero in binary code. The length of time the current stays on or off can be changed from a microsecond up to many minutes.

BONUS: I made a quick start guide for this tutorial that you can download and go back to later if you can’t set this up right now. It covers all of the steps, diagrams, and code you need to get started.

Watch the video for this tutorial:

The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need to learn how to master the Arduino. It includes all of the parts, wiring diagrams, code, and step-by-step instructions for 58 different robotics and internet of things projects that are super fun to build!

Controlling the Arduino’s LED

To turn on an LED, the Arduino needs to send a HIGH signal to one of it’s pins. To turn off the LED, it needs to send a LOW signal to the pin. You can make the LED flash by changing the length of the HIGH and LOW states.

The Arduino has an on-board surface mount LED that’s hard wired to digital pin 13. It’s the one with an “L” next to it:

Getting Started with the Arduino - Arduino Uno LED

To get this LED flashing, upload the “Blink” program to your Arduino:

void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);   
  delay(1000);              
  digitalWrite(13, LOW);   
  delay(1000);             
}

The LED should now be blinking on and off at a rate of 1000 milliseconds (1000 milliseconds = 1 second).

The delay() function on line 6 tells the Arduino to hold the HIGH signal at pin 13 for 1000 ms. The delay() function on line 8 tells it to hold the LOW signal at pin 13 for 1000 ms. You can change the blinking speed by changing the number inside the parentheses of the delay() functions.

Controlling an External LED

An external LED or any other powered module can be controlled in a similar way.

LEDs need to have a resistor placed in series (in-line) with it. Otherwise, the unrestricted current will quickly burn out the LED. The resistor can be any value between 100 Ohms and about 10K Ohms. Lower value resistors will allow more current to flow, which makes the LED brighter. Higher value resistors will restrict the current flow, which makes the LED dimmer.

Also, most LED’s have polarity, which means that they need to be connected the right way around. Usually, the LED’s shortest lead connects to the ground side.

If you connect the LED to pin 13 as shown in the image below, you can use the same code we used above to make the LED flash on and off.

Getting Started with the Arduino - External LED Wiring Diagram

Changing the Pin

If you want to use a different pin to power the LED, it’s easy to change it. For example, say you want to use pin 8 instead of pin 13. First move the signal wire from pin 13 over to pin 8:

Getting Started with the Arduino - Changing the Pin

Now you’ll need to edit a line of code in the program so the Arduino knows which pins to use as output pins. That’s done on line 2 of the code above, where it says:

pinMode(13, OUTPUT);

To use pin 8, you just have to change the 13 to an 8:

pinMode(8, OUTPUT);

Next you’ll need to change the code that tells the Arduino which pins will get the HIGH and LOW output signals. That’s done everywhere there’s a digitalWrite() function. In the program above, there’s one on line 5 and one on line 7:

digitalWrite(13, HIGH);   

digitalWrite(13, LOW);  

To specify that pin 8 should get the HIGH and LOW signals, you just need to change the 13’s to 8’s:

digitalWrite(8, HIGH);  

digitalWrite(8, LOW);

The finished program should look like this:

void setup() {
  pinMode(8, OUTPUT);
}
void loop() {
  digitalWrite(8, HIGH);   
  delay(1000);              
  digitalWrite(8, LOW);   
  delay(1000);             
}

After uploading, the LED should flash just like it did when it was connected to pin 13.

Controlling Multiple LEDs Together

You can control as many LEDs as you want as long as you have enough pins available. Let’s make the external LED flash along side the on-board LED to demonstrate. All we need to do is duplicate the code for pin 8, and change the pin numbers to pin 13.

Here’s an example of that:

void setup() {
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(8, HIGH);   
  delay(1000); 
  digitalWrite(13, HIGH);   
  delay(1000);   
  digitalWrite(8, LOW);   
  delay(1000); 
  digitalWrite(13, LOW);   
  delay(1000);   
}

You should be able to see both LEDs blinking. But they won’t be blinking in sync, they’ll be alternating.

The Arduino executes the instructions in the code from top to bottom. It reads each line and performs the task before moving onto the next line. Once it has read through to the end, it loops back to line 6 and starts over again.

In the program above, the code is executed in this order:

  1. HIGH signal sent to pin 8
  2. Wait for 1000 ms
  3. HIGH signal sent to pin 13
  4. Wait for 1000 ms
  5. LOW signal sent to pin 8
  6. Wait for 1000 ms
  7. LOW signal sent to pin 13
  8. Wait for 1000 ms
  9. Return to step 1

To get both LEDs blinking at the same time, we need to remove the delay between the HIGH and LOW signals of each pin, as shown in this program:

void setup() {
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(8, HIGH);   
  digitalWrite(13, HIGH);   
  delay(1000);   
  digitalWrite(8, LOW);   
  digitalWrite(13, LOW);   
  delay(1000);   
}

Now both LEDs should turn on and off at the same time. The tasks are executed in this order:

  1. HIGH signal sent to pin 8
  2. HIGH signal sent to pin 13
  3. Wait for 1000 ms
  4. LOW signal sent to pin 8
  5. LOW signal sent to pin 13
  6. Wait for 1000 ms
  7. Return to step 1

Now that you’ve seen how to control LEDs with the Arduino, check out part 2 of this series, where I’ll show you how to use a light dependent resistor to control how fast the LED flashes and how to control the pitch of sound output by a speaker.

If you have any questions or have trouble getting this project to work, just leave a comment below and I’ll try help you get it going. And be sure to subscribe! We send out an email each time we publish a new tutorial…