Blinky – Digital IO tutorial

Normally when you start to use a new programing language the first thing that people will often do is write a simple little program called “Hello World”. In basic programming language this would simply be PRINT “Hello World” however the Arduino has no included display unless we connect one. So it is often convenient to upload a digital IO (input/outout) equivalent of the Hello World and this is where we come to our blinky program.

Before we start I will quickly explain that a digital IO is marked on the Arduino Uno/Nano as D0 to D13, however as D0 and D1 are used for serial transmissions TX and RX it is best to not use those pins in a digital IO project. When LOW the pin will be 0 volts and when high will be 5 volts. The other special pin that concerns us is pin 13 which is connected to the internal LED. Which brings us to a quick explanation of an LED.

A typical LED has 2 legs and a colored body. LEDs come in many different colors including: red, greed, blue, white, yellow, orange, purple, infra red (invisible to us), and in many different sizes and shapes. The main characteristic of a LED is that it acts like a diode. as depicted by it’s symbol: The only difference to a diode symbol is that it has two arrows depicting that light is emitted from the diode symbol.

Just like a Diode a LED has an Annode and Cathode, on an LED the Annode is the positive and longer leg and Cathode is the negative and shorter leg.

In the quick Blinky tutorial I first used the internal led which is already soldered on the Nano board on pin 13, then later I used an external blue LED, and to limit the current through the LED we need to use a resistor in series. For that tutorial I used a 220 Ohm resistor and wired it to the Arduino nano using a data pin 4 as shown:

Once connecting the Arduino Nano to the USB lead and plugging it into the computer, open up the Arduino Ide. You will find the example “Blinky” program in the menu under: File -> Examples -> 01.Basics -> Blink. The included Blink code will blink the onboard LED (or pin 13). Select your board, port and settings and upload the code into the micro controller. If the upload was successful the Nano will reset and the onboard LED will blink.

In order to get the LED on pin 4 to blink we need to alter the code slightly and upload it again. We add a declaration #define LEDpin 4 which creates a constant variable called LEDpin with the value of 4 (which is our pin number the LED is connected to). Next we altered the existing pin pointer which was the constant LED_BUILTIN (pin 13) and swapped it for our LEDpin variable which equals 4, representing pin 4.

Either enter the copy the following code or modify the Blink example.

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

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

Once this code is uploaded the Nano will reset and the LED will start flashing.

As a side note Analog pins A0 to A5 can be used for digital outputs too. for example if we want to use A1 as a digital output we change the above line: #define LEDpin 4 by simply changing the 4 to be A1 so it will become #define LEDpin A1

Leave a Reply

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