Arduino - Hello world



UpdatedUpdatedJune 09 - 2017June 09 - 2017

Blinking LED
If you are familiar with any programming language, you already know what Hello World means. In the world of electronics, specially for Arduino, sending an ON/OFF (in binary 1/0) signal or blinking an LED means Hello World.


Prelude

Before proceeding further, let's dive into some basic Arduino programming structures.

In any Arduino programming the two functions void setup() { } and void loop() { } have to be included. Without them, the Arduino IDE compiler will fail to compile the code. So even when you have nothing to write inside the curly braces { } of those two functions, you have to add them!
In Arduino world, a program that is uploaded to and run on an Arduino board is called a sketch.
Every time you power up the Arduino, upload a new sketch or push the reset button, the function which runs at the very beginning is void setup() { }. And it runs only for one time. Inside this function, we will define variables, select modes of pins (input or output) or will initialize serial communication.
void loop() { } runs continuously. All the codes which you want to run again and again will go inside this function.
Six most important functions for Arduino programming
Digital I/O
     pinMode();
     digitalWrite();
     digitalRead();

Analog I/O
     analogRead();
     analogWrite();

Delay function
     delay();

In the following chapters we will know in details about all Arduino functions. To blink an LED, we need the following three functions:

     pinMode();

     digitalWrite();

     delay();


Steps
With pinMode();, we will configure digital pin 2 as OUTPUT.
    pinMode(2, OUTPUT);

With digitalWrite();, we will write a value HIGH to pin 2 (to activate the LED).
    digitalWrite(2, HIGH);

Using delay(); function we will wait for 1 second (or 1000 milliseconds).
    delay(1000);

Again with digitalWrite();, we will now write a value LOW to pin 2 (to deactivate the LED).     digitalWrite(2, LOW);

Finally with delay(); function we will again wait for 1 second (or 1000 milliseconds).
    delay(1000);


Complete sketch


Circuit diagram
blink an LED


Video output