Sunday, 29 March 2015

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

Friday, 13 February 2015

 RASPBERRY:

What will be the impact of introduction of Rasberry pi 2 and Windows 10 for pi2 ?


Raspberry pi is a product that i am in love with and this piece of pi is getting better and better with the passing time. The introduction of raspberry pi 2 has opened door for new set of things like image processing and features like face tracking are going to be much smoother. Although i am yet to receive my raspberry pi 2 and get my hands dirty but still just by watching the videos from tech community i can say that the user experience is much much better ,and the system has become much more responsive ,faster and fluid to use. In fact now it has actually become a full grown computer for more intensive users as compares to the one before. As i can see the minecraft running without lags and no frames are being missed ,The overall graphics of the desktop seems good.


But i have also heard of an issue with raspberry pi ,the other day i was reading a post on internet and a guy named alasdair allan pointed out that using a flash while capturing a picture (such that the flash light falls on pi 2) is causing the pi to reset and as a remedy this issue can be avoided by covering the chip with something opaque,as the phenomenon is due to the photosensitive nature of chip labelled as U16. i would like to test this too once i get my pi. There is also a demonstration video where he showed it.






And the other interesting thing is the announcement of windows 10 for pi 2. It seems giants like Microsoft have understood the power of open source community and opensource products. It is indeed a good move by Microsoft to stay in the changing markets. We already have a windows CE (now officially known as windows embedded compact) for embedded systems but this is not a popular product at least i just know about its name and thats it. Well the stretagists at microsoft must have understood that now is the time of embedded portable systems and if they have to stay in the market they will have to re orient and reconstruct or at least tweak their product line. I think this is what Windows 10 is aimed at.

This is in fact a great feature for pi,i think there are budding developers who are a bit reluctant to dive into linux ecosystem but are very familiar with windows and as soon as you speak of linux they seem to be gasping air, So may be such people will be able to adopt the raspberry pi in a better way so i think this will really enhance the user pool . And wait a second does introduction of windows for pi mean we will see companies like Nortan,AVG ,McAFee coming out with a antivirus for pi. Hahahahah.

Although i still think Windows is no match for linux but still i would like to use windows too because i have spent more time with it as compared to Linux.

Any ways did you register for a developer copy of windows 10 if not please visit. https://dev.windows.com/en-us/featured/raspberrypi2support

Sunday, 25 January 2015

SYSTEM ON CHIP: THE RASPBERRY


In a short span of last  2-3 years we have seen some incredible computing products which are not only quite powerful but are also very compact and are loaded with some fantastic features.There are many products like Beagle bone, PCduino, BananaPi, and the newest product to join the party is the Edison ,created by Intel. But one product that has become the hot Favorite of so many developers around the globe is  the RASPBERRY PI.Here is how it looks if you haven't seen it before. This is a B+ model.

It can do almost all the daily  tasks that your computer can do. You can surf on it,watch movie,write and edit documents ,yes you can't play a high end game on it but then there are things  that you can do with it which you can't do with your regular desktop computer or at least it is more feasible and practical to do it with the pi,Some example of such tasks could be like controlling  motors on a robot, your own real time translator, sensor data processing unit,may be an home automation solution and many other such things.

HOW IT CAME INTO EXISTENCE

Before i dive into the product specifications  i would like to  quickly share about the ethos behind the development of this product. The idea for an affordable computer came when Eben Upton, Rob Mullins, Jack Lang and Alan Mycroft, based at the University of Cambridge’s Computer Laboratory saw a gradual decline in the skill set of students applying  to study Computer Science .They figured out the reasons for such decline (for reasons you can go to history)and to fix the issue they came out with raspberry pi  which hit the market in 2012 . And this idea proved to be a great success and as of October 2014, about 3.8 million boards had been sold.


SPECIFICATIONS AND MORE
 Five models of raspberry pi have come out so far. 

below is a comparison chart of the specifications of different models. I have taken this chart from t element 14 you can find it  at element14_doc. i couldn't have made a better compilation.

OPERATING SYSTEMS AND OTHER SOFTWARES

A raspberry pi requires an operating system just like your normal desktop. Raspbian is the recommended operating system for any beginner.But there are many different operating systems available for raspberry pi,most of them are based on linux. On the website of raspberry pi you would find reference to Raspbian, Pidora, Openelec, RaspBMC. Openelec and RaspbBMC are generally used when you would like to use the pi as a media center. Non linux distributions like RISC OS are also there. Pawn pi is another distribution which is used for penetration testing etc but again it is for advance users.
For the windows users using a linux based distribution  might seem to be an intimidating task in the beginning but it is not that difficult. First thing you find is there is no .exe files for installation and if you want to install anything connect the pi to the internet and go to the repositories and shoot some commands like aptget, and sudu  for  administrator privileges.You can find a lot of content all over the internet where you can learn all this.
Python is the language you use to control the voltage on the GPIO and do some talking over I2C and SPI to control some external hardware. Other languages can be used like c ,c++ but python works out of the box and is also fun to use.Also there are so many open source libraries and a strong community support to find solutions whenever you get stuck somewhere.



HOW MUCH WILL YOU HAVE TO SPEND TO GET ONE?


CHEAP ISN'T IT? well not so cheap actually. This product is often marketed as a 30$ computer or a 20$ computer for a different model. But mind it that is just the cost of that green chip SOC ONLY. To actually make that thing do something and do some development on it you would require some peripherals like keyboard mouse,wifi dongle,HDMI cable,monitor. So if one really want to start working with a pi you need to spend more. A complete kit withall the bits and pieces you need like  camera,pi,keyboard mouse, cables will cost you around 12000 INR. only after you spend this much you will be able to create something. 
Here is a picture depicting the connections of various peripherals

Of course there are ways around like you can do SSH using softwares like putty and then you can also take the advantage of VNC  this will  eliminate the requirement of additional keyboard and mouse. There are some other tricks too to find a way around and manage with lesser resources but may be it will make the learning curve for a beginner much more difficult.

But all in all a great product to play with,it gives capability to do a lot of things. These things are going to be a part of our surroundings,there are people who  are using these to make a commercial product  like advertisement boards, photo souvenir printers,  Raspberry pi foundation seems to promote this kind of products and in fact the compute module is made with an idea of embedding pi into a commercial product but  there seems to be licensing issues with the video codecs.With developers having access to such cool SOC we are going to see more and more innovative products based on pi,particularly the products in the IOT (Internet Of Things) segment will get a great push. 
So that solves for this time,get you pi,learn python and get started.

Here is a video to start learning python with


DEEPANKAR MAITHANI