Sunday 29 March 2015

Arduino TIps & Tricks

How to bootload  atmega 328P  using Arduino Uno board as ISP?

 In situations when you accidently fry an atmega 328P chip on your arduino board you will have to replace it with a new chip. When you would search for this chip online you would find it selling as atmega328P and atmega 328P with optiboot. The one with the bootloader preinstalled is generally priced 50 to 100 INR more then the other one. If you choose to buy the one with optiboot , you can put it on your arduino board as soon as you receive it. But if you chose to go for the first option you will need to bootload it first. In this post I will share with you how to bootload an IC  by using an arduino board as an ISP (In system programmer). However this can be done by using a dedicated programmer like USB asp or any other programmer.

STEP-1: Preparing the IDE and Arduino
  • Connect the arduino uno to the computer
  • Check if the right Com is selected on the arduino IDE.Tools>SerialPort>ComX
  • Check if the right board is selected by going to  Tools>Boards>ArduinoUno
  • Go to the Arduino IDE >examples>ArduinoISP and upload it to your Arduino board.
  • select arduino as ISP by going  to Tools>Programmer>Arduino as ISP.

STEP-2: Making the bare minimum circuit on a breadboard.

Place the IC to be bootloaded on a breadboard and make the connections as shown in the pictures below.


STEP-3:
Go to tools then click on burnbootloader. If you followed eveything correctly.You will see the following messages


So now you can use this IC on any arduino board and upload codes to it using the COM port on computer.



Arduino Tips & Tricks

What is a bootloader? why is it needed? 

what & why of Bootloader?

Bootloader is a small piece of code that lives in a reserved space on the atmega IC. It plays a very crucial role in giving  arduino board user the ease of programming the board over a USB connection.
Lets look at the arduino uno board to know about it in detail.

When an arduino board is programmed from the USB conncection  you program the atmega 328P chip. But there need to be someone in between the computer and atmega 328P which can do the conversion from USB to serial interface,so that the data can be programmed on atmega328P.A chip atmega 8u2  as depicted by a small square on the pictures handles the conversion from USB to serial interface.  So there needs to be  some software on the atmga328P to handle the communication with atmega8u2. This software or code is what we call the BOOTLOADER .

DOWNSIDES OF HAVING THE BOOTLOADER

The bootloader gives you the ease of programing the arduino board over the USB connection. But it has a few downsides which are written below.

  1.  It consumes 512 bytes out of 32k of program memory on the arduino.
  2.  Every time the Arduino board is powered on or it resets the bootloader waits for 0.5 sec to check if the computer is trying to upload any sketch,if  there is a sketch that needs to be uploaded it is uploaded otherwise the program counter starts working through the sketch that is already their in the atmega328P . So everytime you reset or power on or click on upload button  on the Arduino IDE  the board is first reset then then it waits for 0.5 sec. Hence the execution of your sketch doesn't start instantaneously due to bootloader.

Can we get rid of bootloader?

If the above two drawbacks pose to be  a hindrance for your application you can get rid of the bootloader. But once you get rid of it you will not be able to use USB connection for programming. Rather you would require a dedicated hardware like AVRISP MKII like shown below or  usbasp programmer or another Arduino board to program this board.

In such a case you will program using the ICSP header as shown in the figure.In such a case the communication with the micro-controller will be using SPI(Serial peripheral interface). Once you will program your atmega 328 using an AVRISP MKII o other dedicated programmer it will erace the bootloader from your IC.In my next post i will share about how to bootload an IC.



Saturday 21 March 2015

Arduino Tips & Tricks: 

 Simulate the functionality of Arduino boards on a simulation software PROTEOUS.

A simulator software gives you ability to test your code without actually running the code on the hardware. It virtually creates the environment of your target hardware. Proteous is one such simulator software. By default it supports simulation of code for 8051,PICand AVR controllers. But thanks to the hard work by developers who had made libraries that lets you even simulate an arduino code. All you need to do is download the files and paste them in the library folder of your Proteous software.In this post i will share with you the steps you need to follow.

Step 1: Download the library files from here.
Step 2: unzip the rar and you will find two files with extensions .IDX and .LIB. Copy them.
Step 3: Paste them in the library folder of your proteous. Depending upon the version of proteous you have the path will be either of the following.

C:\Labcenter Electronics\Proteus 7 Professional\LIBRARY

C:\Documents and Settings\All Users\Application Data\Labcenter Electronics\Proteus 8 Professional\LIBRARY

Step 4: If your proteous ISIS was already open close it and reopen it. Now you will be able to pic the arduino boards . Go to ISIS professional>component mode>pick from library.
similarly any other library can be added to the proteous.


Step 5: Now that we have got the libraries in place. Lets make a blinking program. write the following code in the Arduino IDE and hit the compile button. the sofware will generate  a hex file . If you donot know how to locate this file please visit my another post here

int led = 13;
void setup() 
{                
  pinMode(led, OUTPUT);     
}
void loop() 
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Step 6: Make the connections and add the hex file.  After adding the hex file click on the play button in the left bottom of the software window.


If you followed everything correctly. Here is what you will see.




Friday 20 March 2015

Arduino Tips & Tricks: 

Where is the main() function in the arduino codes?

While writing a code  on AVR studio or keil uvision every code should have a main function. And during the days when i started programing my teacher told me that every c /c++ code should have a main() function and there should be one and only one main in any program. While writing big programs the programmers tend to write different parts of program in different tabs for better readability and understanding and sometimes  write #include<abc > in the tab where main function is written to include those files 
But in an arduino environment you never find  something like below
void main()
{

}
well does that mean it does't have one. It seems so but it is not actually like that as basics never go out of fashion. It is just another way of arduino to make things simple by hiding the void main. Actually what happens behind the scene is something like below
int main(void)
{
    init();

    setup();

    for (;;) {
        loop();
    }

    return 0;
}
so the setup() and loop() are actually part of main. And the loop function is actually a function under infinite for loop. This also explains why the setup()  runs only once and loop() is executed again and again

Arduino Tips & Tricks:

 Where is the hex file in the Arduino environment?

The thing that made Arduino a big big success is its ability to hide all the complexities and yet give the user so much freedom to create new things. Working on 8051 or bare avr  after writing and compiling  a code in keil uvision or avr studio  a .hex is generated which is burnt into the micro-controller IC  by placing the IC in a zip socket on the burner or using an ISP(in system programmer). But in case of arduino ,the arduino environment takes care of everything and for a long time i even dint know if there is a hex file that is also here in a background.
The clever Arduino environment first checks the code to make sure it is in confirmation with the c /c++ . then the code is passed to a compiler called avr-gcc which converts the code from human readable to machine readable and the code is linked with standard  arduino libraries. and after all this a single hex file is created which contains all the information of your code and then through the com port  ,using an avr chip (this is an avr micro-controller apart from the main controller where you put your code )programmed to handle usb to serial conversion this file is uploaded to your chip's program memory and your arduino board executes the code.

Where is the .hex actually stored? 

To locate the hex file created by your arduino IDE. follow the following snapshots

1. Go to file>Preferences. And check the box next to compilation . 


2. Now compile or upload your program. As you will compile or upload after you check that box you will get a path to a folder where there are temperary files of your system like this C:\Users\HP-PC~1\AppData\Local\Temp\build8697542525264515009.tmp. Copy this path and paste it as shown,remove the name of hex  file. 


3. Once you will hit enter after pasting your path you will reach the place where your hex file is stored.



What is the advantage of knowing about the location of hex file?

There are some great advantages of knowing this information.
  1.  If  you don't actually have a Arduino board, but you want to use the arduino libraries and the ease of coding  in Arduino environment or a code from big arduino community that you want to run .And all you have is  any AVR chip and an ISP programmer like mk-II  or a USB-ASP or some other varient you can compile the program using arduino IDE ,obtain the .hex file and can burn it using the software provided with your programmer. 
  2. There is a software named Proteous which can simulate a microcontroller. You just put in a .hex file and it will simulate and execute the code just like on real hardware. The hex file obtained by the above method can be used to do that too. 


Thursday 19 March 2015

Arduino Tips & Tricks: 

Making the blinky blink with a one liner

Any one who has ever used an Arduino would have certainly wrote a program to toggle  a LED. In fact it is the first code which you will run when you will get  an arduino board in your hand,this is one of the way of saying " Hello! world" from arduino or any other programable embedded system.
But the code we use to make the LED blink for the first time is not one of the most efficient way of making it happen. There can be number of ways of writing a code to achieve a particular objective . However there can be various pros and cons of going with a particular approach. While beginning the programming the first thing in the mind of a programmer is to accomplish the functionality and efficiency of the code both in terms of density of code and actual performance that it will provide is something that is on a sideline. But as you go pro you become hungry for greater performance and more condence code  rather then merely achieving the objective.
So here is a quick talk about the two approaches of making blinky blink,one is a beginner way and the second one is a way that you would prefer more as a pro.
const int ledPin=13;// give name to pin 13
void setup()// this runs just once
{
  pinMode(ledPin,OUTPUT);// set the pin 13 to output
  digitalWrite(ledPin,LOW);// pull pin 13 to ground
}
void loop()// keeps on running again and again
{
  digitalWrite(ledPin,HIGH);// turn on the LED
  delay(1000);// wait for 1sec.
  digitalWrite(ledPin,LOW);//turn off the led
  delay(1000);
  
}
The code above will give you a nicely blinking led 13 on the arduino board. You have achieved the functionality you intended to achieve.But there is a much better way of doing this.Here is a code below that provides same output but the code is simply better than before.
const int ledPin=13;
void setup()
{
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}
void loop()
{
  digitalWrite(ledPin,!digitalRead(ledPin));
  delay(1000);
}
How is it working?
Everytime the loop is executed arduino reads the state of pin no 13 to which our LED is connected using digitalRead(ledPin) then using the '!' NOT operator the value of state is altered ,Suppose the loop is under execution and the program checks the state of pin 13 for the first time,it should be low ,the NOT operator performs its action on this state and finally a HIGH is passed to the pin 13,then the program stops for 1Sec and then again when loop is executed this time the  state of ledPin is HIGH so  this time the pin is made LOW. This keeps on happening again and again
We have seen the two ways of making the LED blink. Both the codes perform the same task. lets see how much space does each of them actually take on an arduino.
You can see here the pro version takes 1242 bytes while the beginner version takes 1084 bytes of space on an Arduino. It hardly matters which code you use when it is just about making the LED blink but when the we are trying to do something more complex may be like listening to  a character over serial and making the LED blink accordingly the pro version is just a better way at the expense of a few more bytes.
Here is a quick code which listens for a particular character over the serial and when the character 'h' is received  it checks the state of LED on pin 13 and alters the state to HIGH if it is LOW and vise versa. After you upload this code on your arduino open the serial monitor write h and hit enter. the LED will glow
const int ledPin=13;
char val;
void setup()
{
  Serial.begin(9600);// Enable Serial at a baud rate of 9600
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
}
void loop()
{
  if(Serial.available()>0)// gets the number of bytes available to read
  {
    val=Serial.read();// reads the character and stores in in val
    if(val=='h')// checks if the received character is 'h' or not
    {
  digitalWrite(ledPin,!digitalRead(ledPin));// toggles the LED
 
    }
  }
}
The codes used in this post can be downloaded here