Saturday, 28 July 2012

Arduino language tutorial (Lesson 1)

Introduction :

This is an easy tutorial about how to write sketches ( codes ) for arduino platform .
Arduino is an open source physical computing platform based on a simple input/output (I/O) board and a development environment that implements the Processing language.

Getting Started :

Arduino is composed of two major parts: the Arduino board, which is the piece of hardware you work on when you build your objects; and the Arduino IDE, the piece of software you run on your computer. You use the IDE to create a sketch(a little computer program) that you upload to the Arduino board. The sketch tells the board what to do.
Look at the Arduino UNO board: you’ll see a black chip with 28 “legs”—that chip is the ATmega328, the heart of your board.
The board consists of the following :
>> 14 Digital IO pins (pins 0–13)
>> 6 Analogue In pins (pins 0–5) These dedicated analogue input pins take analogue values (i.e., voltage readings from a sensor) and convert them into a number between 0 and 1023.
>> 6 Analogue Out pins (pins 3, 5, 6, 9, 10, and 11)These are actually six of the digital pins that can be reprogrammed for analogue output using the sketch you create in the IDE

The Software (IDE)

The IDE (Integrated Development Environment) is a special program running on your computer that allows you to write sketches for the Arduino board in a simple language.
the code that you have written is translated into the C language (which is generally quite hard for a beginner to use), and is passed to the avr-gcc compiler, an important piece of open source software that makes the final translation into the language understood by the microcontroller.
you can download the IDE software from the following link (it's free open source software ) :
after installing the software and running it the following window appears , here we will write the code,verify it (check that it is correct ) and upload it to the board.
Now we are ready to write arduino sketches.

Lesson 1 :

any code or sketch in arduino consists of three main parts.
1- In the first part we declare the variables and constants we use in our program.(Declaration part)
2- In the second part we define the inputs and the outputs used in our sketch. (Initialization part )
3- The main program the microcontroller always executes. ( Main code)

we will illustrate Arduino language by a simple example which is the Blinking (flashing ) LED.
the LED will be connected to pin13 in the arduino board as shown :
Here is the code :
///////////////////////////////////////////////  part 1 /////////////////////////////////////////
// Example 01 : Blinking LED
const int LED = 13; // LED connected to digital pin 13
////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////// part 2 ///////////////////////////////////////////
void setup()
{
pinMode(LED, OUTPUT); // sets the digital pin as output
}
///////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////// part 3 //////////////////////////////////////////
void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(1000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(1000); // waits for a second
}
///////////////////////////////////////////////////////////////////////////////////////////////////

The Code, Step by Step
const int LED = 13;                               // LED connected to digital pin 13
>> const int means that LED is an integer number that can’t be changed (i.e. a constant) whose value is set to 13. We are using this command to specify that the LED we’re blinking is connected to the Arduino pin 13.

pinMode(LED, OUTPUT);                      // sets the digital pin as output
>> pinMode tells Arduino how to con-figure a certain pin. Digital pins can be used either as INPUT or OUTPUT. In this case, we need an output pin to control our LED, so we place the number of the pin and its mode inside the parentheses.
INPUT and OUTPUT are constants in the Arduino language. (Like variables, constants are assigned values, except that constant values are predefined and never change.)

loop() is where you specify the main behaviour of your microcontroller. It will be repeated over and over again until you switch the board off.

digitalWrite(LED, HIGH);                           // turns the LED on
>> digitalWrite()is able to turn on (or off) any pin that has been configured as an OUTPUT. The first argument (in this case, LED) specifies which pin should be turned on or off (remember that LED is a
constant value that refers to pin 13, so this is the pin that’s switched). The second argument can turn the pin on (HIGH) or off (LOW).

delay(1000);                                                  // waits for a second
>> delay() basically makes the processor sit there and do nothing for the amount of milliseconds that you pass as an argument. Milliseconds are thousandths of seconds; therefore, 1000 milliseconds equals 1 second. So the LED stays on for one second here.

digitalWrite(LED, LOW);                           // turns the LED off
This instruction now turns off the LED that we previously turned on.

delay(1000);                                                // waits for a second
Here, we delay for another second. The LED will be off for one second.

Then the Main program is repeated again and again until the power is switched off the board .

END OF LESSON 1




- Reference : O'REILLY  Getting Started with Arduino by Massimo Banzi  , 2nd Edition.

GO TO :

LESSON 1
LESSON 2
LESSON 3
LESSON 4

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

1 comment:

  1. In the world of new technology and invention LED writing board are most efficient and cost effective way for restaurants, shopping malls, coffee shops, bars, nightclubs and virtually any other public place etc to promote their business and sell a product which brings them a profit.
    If you have any Query then click hereLed writing board

    ReplyDelete