red led lit up on breadboard

Arduino Tutorial for Beginners – Digital Output

In today’s arduino tutorial for beginners blog post, we explore the digital output of the microcontroller. We build on the first post in the series to output data to a pin.

We use the function digitalwrite and light up an external LED which creates a much more dramatic show than the onboard surface mounted LED.

 


Components Required

The components we need for this tutorial are:

 

  • Arduino Uno (or compatible board)
  • 220 ohm resistor
  • Super bright Red LED
  • Hookup wire
  • Computer with Arduino IDE installed and USB cable

 

Wiring up the project

Before we start writing the code we wire up the circuit which is just a single LED with a current limiting resistor. The LED we are using is a super bright red LED that has an operating voltage of 2V and draws 15mA. The resistor value is 220 ohm which is needed to limit the current to the LED to prevent it from being damaged.

 

Arduino Uno wired up to LED

 

 

A calculation example to work out the resistor value can be found in our blog post “simple LED driver circuit” .

We use an external prototyping board to wire up the circuit like as in the photo above. Remembering the longest lead on the LED connects to the positive side of the circuit and the shorter lead connects to the negative side of the circuit.

 

Coding the Microcontroller

Once wired up we start coding by setting up pin 2 to be an output pin, then later in the code we can make the pin either high or low. We use a loop and blink the LED 4 times in a row using  a delay of 70 milliseconds between the high and low states.

The code for this tutorial is below:

 

void setup() {
  // initialize digital 2 as an output.
  pinMode(2, OUTPUT);

}

void loop() {             //continuously loop the program
  digitalWrite(2, HIGH);   
  delay(70);              
  digitalWrite(2, LOW);
  delay(70);
  digitalWrite(2, HIGH);   
  delay(70);              
  digitalWrite(2, LOW);
  delay(70);
  digitalWrite(2, HIGH);   
  delay(70);              
  digitalWrite(2, LOW);
  delay(70);
  digitalWrite(2, HIGH);   
  delay(70);              
  digitalWrite(2, LOW);
  delay(500);               //long 1/2 second pause before loop starts again
 
}

 

Using the digital output is achieved simply by setting the pin direction to be an output and then using the digitalwrite command to make the output high or low.

We hope this tutorial was helpful to you.

Thank you for reading the post, we really appreciate it !

Before you move on, I would love for you to suggest some topics you would like to see covered on the Smartmicrocontroller.com website this year by leaving a comment in the comments section in the youtube channel, or send us a message through one of the social media links on the about page.