Basics of Arduino Programming – Part 1

Basics of Arduino Programming – Part 1

The Beauty of the Arduino is that,It can be moulded to make any type of system.The magic wand for moulding the Arduino is:The codes.I am going to teach you the very basic thing you need to know about programming an Arduino.

Before I begin,I recommend you install and configure latest Arduino IDE and buy an Arduino board.You can continue learning without these things also but these things will makes the learning process more clear and practical.

I will be covering the Topics from Programming structure to working with Serial ports and much more.

Arduino syntaxes resembles a lot like C syntaxes,So If you know the basics of C,It is going to be much easier to learn Arduino programming.

The Programming structure :

Every Arduino program must consists of two parts,.

void setup()
 {
 statements;
 }
 void loop()
 {
 statements;
 }

In programming world,these two parts are known as functions. Functions are small bit of codes that can be called any number of times.

void setup() - In This function the Input and output pins are defined using Built in functions(BIF’s).which I will cover later.

void loop() - You are actually doing stuffs in this block only,The code defined in this block continuously loops through until the Arduino is turned off.

For example If you are defining a code to turn on an LED for some time any again you turn off the same LED for some time.The Arduino loops though this code again and again which will result making the LED blink.

void setup() function :

As I have mentioned earlier,setup() function is used to define the PinModes and to initialize the Serial communication.

some built in functions like pinMode(),serial.begin() and serial.flush()can be used in this block.

The syntax :

pinMode(pin, mode) ;

for example:

pinMode(5, OUTPUT);
pinMode(7, INPUT);

The above code declares the digital pin 5 as Output pin and pin 7 as Input pin.

Note that,unlike C the Arduino programming language is case sensitive.If you write pinMode as pinmode then the compiler will throw an error.It is usually good to capitalize the first letter of the second word to make the code readable.

I will be discussing about serial communication later in this series.

void loop() function :

Lets see the code below before we move on

int led = 13;
 void setup()
 {
 pinMode(led, OUTPUT);
 }

void loop()
 {
 digitalWrite(led, HIGH);
 delay(1000);
 digitalWrite(led, LOW);
 delay(1000);
 }

I want you to compile this code in Arduino IDE and if possible upload it to your Arduino and see what happens.

As we discussed earlier the pin 13 is declared as a variable led and variable led is assigned as a Output pin.(Note that pin 13 is connected to onboard LED).

Inside loop() function we have called some built in functions (BIF’s) like digitalWrite() and delay().

digitalWrite(variable, Value);

The above BIF is used to turn on/off a Digital pin which is already declared using pinMode.So the code

digitalWrite(led, HIGH);

Tuns on the led pin,which is pin 13.

delay(1000);

The above code insist the Arduino to wait for 1000 ms that is 1 second.So the Led connected at pin 13 will be continuously in ON stage for a period of 1 sec.

If you need more precision on delay,you can use

delayMicroseconds(us)

You can define the delay time in micro seconds scale.

Now the code

digitalWrite(led, LOW);
delay(1000);

Turn OFF the led after 1 sec and again wait for 1 second before the loop ends.As I mentioned earlier the void loop() function loops through until Arduino is turned off.

Congratulations…You have made your first Arduino project and your first ever Arduino code works flawlesly.

In next part I will be discussing some other built in functions and syntaxes.To get instant notification about our articles,Subscribe our newsletter here.

Do you have any queries or Feedback ?? Let’s discuss right below

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>