Tutorials

Getting Started with Arduino: led blinking on Arduino Micro Tutorial

Introduction

LEDs are probably the best thing that ever happened in electronics, they go well with absolutely everything (like raisins, pretty much) and as it turns out, they’re pretty simple to work with. That’s why they’re just perfect for any beginner tutorial on electronics.

This tutorial will show you the very basics of working with a simple microcontroller board (an Arduino micro) and using it to control an LED.

If you are looking for a little introduction on electronics before getting started with actual practice, have a look at my Noob’s crash-course on electronics.

What you are going to need for this tutorial

  • Breadboard (I’m using a mini breadboard here)
  • Arduino Micro
  • LED (any regular LED will do)
  • 220 ohm resistor
  • 2 breadboard jumper wires

Understanding the Components

BreadBoard

A Breadboard is used to prototype new projects without the need for soldering. You just need to plug / connect the components to create your circuit. Pretty much like a “development environment” you might be used to, it’s ideal to start and develop a project. It’s also a great way  for just playing around with no strings attached.

Learn More

Arduino Micro

The Arduino Micro is a very compact microcontroller board based on the ATmega32U4 microcontroller.

Learn More

LED

LED comes from  “light-emitting diode”. LEDs have two legs, one positve (anode) which is usually the longer one, and a negative (cathode) which is usually the shorter leg. You will normally connect the positive leg of an LED to a resistor (around 220 ohm or higher up to 1k) and the negative leg to GND.

Learn More

Resistor

Resistors are a fundamental electronic component. They are used to limit current flowing through a circuit. Resistors can have many different pre-defined values, measured in ohms. The bigger the value, the bigger the resistance, which means less electrons passing through.

Learn More

Jumper Wires

Jumper wires are simple reusable wires that can be easily connected to the breadboard and components for prototyping. 

Learn More

Getting the environment / IDE ready

First things first: “plug” the Arduino board to the breadboard, making sure to position each side of pins in a different side of the breadboard, so they don’t short-circuit. It should look similar to this:

You can now connect the USB cable to the board in order to turn it on.

Go to the Arduino website and download the Arduino IDE.  Once you get it installed, select the right board in the menu “Tools ->  Board”. For the Arduino Micro, you should select “Arduino / Genuino Micro”.  You might need to also select the port for communication, under the menu “Tools -> Board -> Port”. The port selection works only when the board is connected.

If you turn the board on by plugging the USB cable, you should be able to get info about it by going to the menu “Tools -> Get Board Info”.

The Board Pinout

It’s always interesting to have at hand the board pinout. It will show you details about the board, the available pins and other information.  You can find pinouts for all the most popular boards easily with a Google search – you can even go to “images” directly to get what you need quickly.

This is how a simple pinout looks like for the Arduino micro:

Making Things Happen

First example: using the built-in led

Now that you’re all set up, it’s time to upload your first sketch to the board. Open the basic blink example on the IDE through “File -> Examples -> 01. Basic -> Blink”.  Have a look at the code to get a basic idea of how it works:

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

You’ll notice that the program has two functions, a “setup” and a “loop”.  They are quite self-explanatory. This example uses the built-in LED from the Arduino board, which is defined by the constant “LED_BUILTIN”.  As you’re gonna see in the next example, we can change this to a different pin in case we are using an external LED.

When you feel ready, hit the “upload” button (right arrow) to send the code to the board. In a few seconds, you should see the built-in LED in the board starting to blink.

Second example: using an external LED

Well, that was exciting, but way too simple. What if we want to use an external (and way more exciting) LED, instead the boring built-in one?

Well, to do that we are going to build our very first circuit.

Circuits are, basically, paths in which electricity can run.

You can think of a circuit as a program. It has a very specific flow of execution (electricity) that can be altered by controls: function or method calls, conditionals and such. In a circuit, such controls are implemented in “bare metal” through the use of components like LEDs, resistors, capacitors, buttons, etc.

Our circuit is very basic. We are going to connect an LED to the pin 6 of the Arduino micro board. To connect the LED, we’ll need a 220 ohm resistor (values up to 1k can still light up the led).

Without further ado, let’s check the diagram representing the circuit we want to build:

As you can see from the diagram, the positive leg of the LED (anode, right leg in this diagram) is connected to the resistor, which is then connected to the pin number 6 on the Arduino board. If you don’t quite understand how they are connected, have a look in our Whatsit page explaining how breadboards work.

You might be wondering what the black wires are doing there.  They are connecting the negative leg of the LED (cathode, left leg in this diagram) to the GND pin on the Arduino board.

Here’s how my circuit looks like IRL:

Now, we are going to make a slight change to the previous “Blink” example, so it uses pin 6 instead of the built-in led pin it was using before. To make the code more versatile, we’ll create a variable with the pin value so it’s easier to change it, in case we need to.

Here’s the updated code:

int led = 6; // we are using pin 6 for the led

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin led as an output.
  pinMode(led, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

And this is how it looks like, when it works 😉

Third example: using PWM

PWM stands for “Pulse Width Modulation”, which is basically a type of digital signal that emulates an analog signal. While a digital signal can only be HIGH (5v) or LOW (0v), PWM implements a way for translating digital signals into analog waves (of sorts) so we can use a graduating range of values through a pin – for this example, we are going to use PWM to make our LED fade in and out, instead of simply blink.

Now, if you have a look at the Arduino micro pinout image, you’ll notice that some of the pins have the word “PWM” close to the pin number. This means that that pin supports a PWM output, and that’s what we need in order to make our LED fade.

As you’ll notice, the pin we chose for the previous example (pin 6) has PWM support, so we actually won’t need to change anything in our circuit. Only the code needs to be changed.

Let’s open the “Fade” example. On the Arduino IDE, go to the menu “File -> Examples -> 01. Basic -> Fade”. Now change the pin number from 9 to 6 and you should be ready to upload the new example.

Here’s the code (including the pin change and stripped from comments):

int led = 6; // our led uses pin 6
int brightness = 0;
int fadeAmount = 5; 

void setup() {
 pinMode(led, OUTPUT);
}

void loop() {
 analogWrite(led, brightness);

 brightness = brightness + fadeAmount;

 if (brightness <= 0 || brightness >= 255) {
   fadeAmount = -fadeAmount;
 }

 delay(30);
}

This is how it looks like in action:

It’s awesome, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *