Thursday 25 February 2016

Program Arduino Uno in C Language

 

 

Hello friends,
In this tutorial, i have shown how to program Arduino Uno in C language. Arduino uses its own language to program Arduino boards, because it is easy to code & understand. But Arduino boards can also be programmed in C language.
This tutorial is for beginners who are just thinking to program Arduino Uno in C language. (I am a beginner too ;P )

So let's get started.

Step 1: Setting up things..


To write code in C, we need a software, you can use AVR studio & any other coding software. Here we will use code blocks.


It is light weight, easy to use & it has a built in software to directly upload code to your Arduino. you can download it here. 

When download is done, open it & select new project.
A dialogue box will open.
  • Select AVR project, (Yes, there is a Arduino project too, because code blocks also support Arduino programming language too. )
  • Click Go,
  • Click Next
  • Give the name of the project, (you can also change the destination folder for saving this project.)
  • Click Next.
  • It will ask for the compiler, default compiler is already selected,
  • Click Next
  • Here you have to select chip of your Arduino, ( i am using Uno, so i will select Atmega328p.)
  • Click finish.
  • A blank screen open. On the left side. There is work space
  • Double click on sources & select main.c.
 

Step 2: Writing code.


Copy or type this code there. ( you should prefer type, so you can improve your typing and coding skills.

#include <avr/io.h> // header file file for input output pins
#include <util/delay.h> // header file for delay.

int main (void)
 {
  /* set pin 5 of PORTB for output*/
  DDRB |= _BV(DDB5);

  while(1) 
  {
    /* set pin 5 high to turn led on */
    PORTB |= _BV(PORTB5);
    _delay_ms(1000); //delay 1 second

    /* set pin 5 low to turn led off */
    PORTB &= ~_BV(PORTB5);
    _delay_ms(1000); //delay 1 second.
  }
 } 
 
Understanding of some code.
  • <avr/io.h>         : To tell the chip where all the ports & pins are located.
  • <util/delay.h>   : To use built in delay features. for accurate delay.
  • DDRB               : Data direction register for port B.
  • PORTB5           : In Uno, PORTB5 is connected to digital pin 13.
  • BV                     : Bit value.


Now Click on the Gear like icon located below the menu bar. it will compile & build all the necessary files. Now go to Tools in menu bar & Click on Arduino Builder.

Step 3: Uploading Code to Arduino

 

 


A Arduino Builder program will open. Now attach the Uno to PC.
  • Click on Load sketch / hex file & browse the hex file from where you had stored it. ( default is in C drive/file_name/bin/debug/ )
  • Select board type as Arduino Uno
  • Dont change Clock.
  • Select the COM port of Uno.
  • As soon as you click on COM port, the Arduino Builder will start to program it.
  • When programming is done, L led on Uno should blink.
  • If not, then do the step 3 again.
Note : I have used Uno in this tutorial, but you can use any arduino, you just have to know which pin of IC is connected to which digital or analog pin of Arduino. 

If you any suggestions or you get any error, you can write me in comment section below.
happy programming ^_^

 

No comments:

Post a Comment