As a first project, making sure everything works as intended and just to show how simple it is to create a working program, we will make a microcontroller version of “Hello, World!”: A blinking LED light.

Depending on what board you have, you may need an additional LED if one does not come along on your board. We are using an Arduino D in this tutorial here.

Required Hardware
1. Arduino
2. Resistor
3. LED

Blinking LED

Moving onto the code itself, I will explain the various parts necessary for you to understand what you are doing. Be sure to make sure you completely grasp everything before moving on or you will get lost later on your Arduino ventures.
The following code will make the default LED blink once each second (500ms off, 500ms on)

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

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

That 10 line program will be your first step to a great future in embedded systems and similar things. I will now go through the code line by line to explain what it does.

On most Arduino boards, pin 13 is reserved for the built in LED. Here, we will define that pin so we can call it later.

int ledPin = 13; 

When making any Arduino program, you will need a setup() function. This will run only once, during startup. You can define digital pins as inputs or outputs, and much more. Basically these three lines are “Hey, pin 13! You’re an OUTPUT pin this round!”.

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

Now we get to the “loop” function. It runs forever while your board is powered. We use digitalWrite() functions here in order to set the voltage on the pins.

void loop() {
  digitalWrite(ledPin, HIGH);   // turn the LED on
  delay(500);  // wait for 500ms
  digitalWrite(ledPin, LOW);    // turn the LED off
  delay(500);  // wait for 500ms
}

Your Arduino sketch window should now look like this

Blinking LED

If it compiles, make sure your board is connected, and press “Upload”!

Blinking Arduino LED

Leave a Reply

Your email address will not be published.