GoganWare

All about tech.

post banner

Blink a LED with an Arduino

4 min read

Controlling electronic components like sensors, buttons, LEDs, or even motors has been made easy using an Arduino. The most basic example is the ability to blink an LED. Once your skills and knowledge evolve so will your projects. You could use the builtin LED that is located on the Arduino board but for this example we will add our own LED. For this example I am using an Arduino UNO R3 but any standard Arduino should work. The pictures you see here were made using TinkerCAD, a free to use software to practice using the Arduino without having any of the hardware!

What you will need

  • Computer to write code
  • Arduino
  • breadboard
  • 2 male to male jumper cables (not pictured here)
  • LED
  • 220 ohm resistor

Building the circuit

Luckily the circuit we are making is extremely simple with very little room for errors. We will be hooking our components up to the breadboard. As current can only flow in one direction through the LED, you will need to ensure your wiring is correct or the LED will not work at all. The longer and sometimes bent leg of the LED is known as the Anode(+). This is where the power source should be connected. The other end which is usually shorter is known as the Cathode (-). This is where you should connect the ground.

We will also be using a 220ohm resistor to connect the LED. The reason being is that an average LED can only handle about 10-20mA. Without the resistor the LED will shine brightly for a couple of seconds and burn out quickly after.

Use the diagram below to connect everything. This complete circuit swill have a signal or output coming from DIGITAL PIN 2, through the LED, the resistor then back to the Arduino ground. We are using the digital pins because we want to control the LED, alternatively you could just turn the LED on by moving the wire from PIN 2 to 5V:

Coding the circuit

Now that our circuit is put together we can move on to writing the code to implement the blinking LED. I will be using TinkerCad but for actual projects it is recommended to create all of your programs though the Arduino IDE. With every program on an Arduino you will find two main functions. The setup function and the loop function. In the setup function you general assign the pins to a mode, usually INPUT or OUTPUT. It is usually a good idea to assign variables in a program. Variables allow you to store information that can be reused in different areas of the code

int ledPin = 2;

int is the identifier that tells the program what kind of value will be stored in the variable ledPin. In our case it is an integer (int). You could name it something other than ledPin like myPin but something more descriptive is common convention. Next we want to assign our ledPin a mode. We will do this in the setup function:

void setup() {
pinMode(ledPin, OUPUT);
}

After the setup is done we will create our loop. A function that literally loops continuously unless power is cut or logic is in place to pause or stop it. In this loop the LED will turn on for one second and then turn off for one second and repeat.

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}

The complete code looks like this:

int ledPin = 2;

void setup() {
pinMode(ledPin, OUPUT);
}

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}

And that's it! Run or upload the code to the Arduino to start blinking the LED! Try manipulating the microseconds for the delay to change how fast the LED blinks!

Youtube video guide coming soon! Thanks for reading!