Arduino Tutorial Series : Lesson 03 - Hello world !

Lets get started with Arduino programming with an overview of Arduino IDE and Arduino Sketch ...

Arduino IDE


Arduino IDE is very simple. There is no complicated stuff in it.

Arduino Sketch

Arduino sketch is the program we write and upload to the microcontroller on Arduino board.

An Arduino sketch consist of two main parts (two functions),

setup()

setup() function is the set of code which runs first up the Arduino is turned on. This is where we initialize the components of out Arduino sketch. This initializing may include,

  • Input, Output pin configuration
  • Variable declaration
  • External library initialization
setup() function is a must have in an Arduino sketch. Without setup(), your sketch will not run inside the microchip.

loop()

loop() is the function the microcontroller runs repeatedly in a loop. loop() function is executed after setup() function is excecuted once. This set of code includes the main body of your Arduino sketch. All the tasks is done in this loop function.

Although a basic Arduino sketch should include loop() function, it is not compulsory. If you need your arduino sketch to run once, there is no need of loop() function. You can enter all those commands to setup() function. 


First Arduino Project - Blinky


What you need...

  • An Arduino board
  • a Breadboard
  • an LED
  • 220 ohm resistor
  • connection wires

Hardware setup


Connect the LED to the arduino as shown in the figure. LED is connected to Digital IO pin 13 of the arduino.

220 ohm resistor is used to limit the current flowing into the LED. Arduino Outputs 5V from its output pins. 220 ohm resistor protects the LED from burning itself.

Note :

For debugging purposes, a permanent LED is connected to IO pin 13 and soldered on the Arduino board itself. You can use it for your blinky project instead of external LED.

Arduino Sketch

Download the sketch file - Blink.ino


Lets break the code in to pieces ...

Inside the setup() function, pinMode(13, OUTPUT) function initializes pin 13 as an output pin. The syntax for pinMode function,

pinMode(PIN_NUMBER, MODE)

There are three modes :

  • INPUT
  • OUTPUT
  • INPUT_PULLUP - Builtin pull up resistor is enabled
Inside loop() function, 

digitalWrite(13, HIGH) sets up 13 pin to voltage level high (5V). This means the LED is turned ON. Next delay() function is used to delay the program execution. The parameter input to delay function is the count of milliseconds to delay.

Then, digitalWrite(13, LOW) sets up 13 pin to voltage level low(0V).  This means the LED is turned OFF. Then another delay of 1 Seconds is called.

This code runs infinitely blinking the LED every one second.

Uploading Code


Click on the Arrow button on the top of Arduino IDE to compile and upload the sketch to Arduino.



No comments:

Powered by Blogger.