Saturday, March 29, 2014

A Simple Introduction to Arduino


Arduino is a important simple platform to do many things from blinking an led to running a robot and more on. First we going to see different Arduino boards available in the market. There a number of boards available but among them some of them are most famous. Eg : Arduino Uno, Arduino Fio, Arduino Mega. Choosing a Arduino board depends on  what you are going to do with that. So deciding which board is suitable for your needs makes you save your money.

Arduino Fio Xbee Side
Arduino Fio
First we look what are the things a typical Arduino board has :
  • A Micro controller
  • An Oscillator
  • A power suppply
  • A programming interface
  • A Xbee shield
Though there are some other things. I do not want to confuse you if you are new to this. Among all of this Micro controller is the main part we have to look more deeply. Actually what is micro controlller?. Micro controller is not actually a processor but it contains processor. We just can call it as a small computer. Actually a Micro controller has its own processor, RAM, programmable ROM, I/O ports with in that. We will go further into I/O ports. I/O ports actually belongs to the micro processor inside the micro controller. We will look a simple diagram to understand the I/O ports.

Now we can easily look what a port means. In the picture you can see Blue, Red, and Green colored grouped pins of the processor. We do not want to look at register level explanation of ports for the introduction to arduino as we hav to go alot to look. okay so this ports only a grouping of pins of a micro controller if we say easily. There are three kind of pins in a microcontroller. Analog, Digital, and PWM  ( Pulse Width Modulation) pins are those three.

What is a Digital Pin?
Well, A Digital pin is as the name implies you can give or get digital values such as HIGH (1) or LOW(0). A high means 5V r 3.3V which depends n the board and your upply voltage and you settings. A LOW is zero volt. Further these pins can be configured as input or out put according to you needs. if you want to get voltage from the micro controller you have to configure as output and vice versa.

What is a PWM pin?
It is similar to digital pin but one advantage is you also can configure it as analog pins.
What is an analog Pin?

Simply saying we can give only give readings to these pins. We can not take outputs from them. That means you can only consider them as input pins not output pins like Digital or PWM pins.

Mapping of Pin values to the voltage.?
Say you have a minimum voltage say 0V and maximum voltage say 5V . We take the voltage difference whic is 5V here. So this difference is mapped here with 1024 normally in arduino processors. So now you can simply calculate he voltag eqaul to the value given by the pin and vice versa. Simple mango sum :). Here I want to point out another pin which is called as AREF ( Analog Reference). This mapping is done with how we configure this AREF. It is programming part so we will look that in under another title.

What else you need to play with arduino?
All with hardware nothing is possible. We need to configure this pins and we have to somehow tell the processor to do this and do that. So we have to program the micro controller or say Arduino. To program we need a programming interface or editor. For Arduino, Arduino itslf offfers their own Arduino IDE.Here is the link yu an dowload and install with no fear.

http://arduino.cc/en/Main/Software#.UxyQefmSySo

The programming is similar to C syntax. If you want to write the program in C/C++ eclipse editor, It is also possible. You need to do some configuration. but that is beyond our purpose here.

What else you need?
Say We have written program but how to transfer that to arduino or microcontroller. For some Arduino boards we do  not need a UART. But boards like Fio needs a UART to transfer the program.You can upload the program using UART by clicking the upload button in the arduino IDE. Even if the program compiled and uploaded successfully, The arduino will respond only if your logic and hardware configuration is correct.


How to select a arduino board?
  • How many I/O pins you need. 
  • Do you need Bee modules in your project
  • Do you need to go for wearable products
  • Do you need solar cells to be connected ( Energy harvesting projects)

Say if you are not working with Xbee, Just consider a arduino board which does not have Xbee socket buit in with that board.

Done. If time allows, Will update further.






How to Configure a Digital Potentiometer (eg : MCP41100) as Rheostat or Potentiometer.

Digital Potentiometers are used for two purposes : One is as Rheostat mode and other one is Potentiometer mode. Lets see the pins of MCP41100 :



Here the potentiometer communicates with Arduino using SPI Protocol. By default, Arduino D10 is SS (CS bar), D11 is MOSI (SI) , D12 is MOSO, and  D13 is SCK. So connect the potentiometer pins to Arduino as mentioned mapping. Further :

Vss - GND
Vdd - Supply Voltage (2.7V to 5.5V)
PW0 - Output is taken
PA0 - To GND
PB0 - To Vdd ( For using as potentiometer) or To PW0 ( For using as Rheostat)

Now Circuit is ready. Time to move to programming.

Program : 
#include <SPI.h>  //Include SPI library

void setup()
{
  SPI.begin();  //Initialize SPI
  Serial.begin(9600);
  pinMode(ss, OUTPUT);  //Set Pin Direction ,Other SPI pins are     //configured automatically
}

void loop()
{
    for (int i=0; i<=255; i=i+1)  // This is because 8 bit Range
    {
      SetPot(0x11,i);
      delay(200);
    }
    delay(200);
    for (int i=255; i>=0; i=i-1)
    {
      SetPot(0x11,i);
      delay(200);
    }
  }

//Function to Set the Potentiometer
void SetPot(int regVal, int levelVal)
{
  digitalWrite(ss,LOW);
  SPI.transfer(reg);
  SPI.transfer(level);
  //Serial.print(level);
  digitalWrite(ss,HIGH);
}

Done.

Note 1 : If you have picked D10 as Slave Select you cannot name it as SS because by default it is named as SS. But you can select any pin for SS. You only need to set the declare and configure its pin mode.

Node 2 : If you want control voltage, You do not need a Digital Potentiometer because you can do it using Arduino only but if you need to get varying resistance use Digital Potentiometer.


Thursday, March 27, 2014

How To Add a Python Program To Run Every Time In Boot Of The Raspberry Pi

Say You want to add a python program to run when booting of Raspberry Pi without any log in even if power goes and then power comes. First to do this check your python program is working properly. So lets see how to do this :

Go to terminal and type the following : sudo nano /etc/rc.local

You will get a window of rc.local. There you type the command before exit 0 to run the python program. It will be as follows :

sudo python /home/pi/name_of_your_program.py

Here you should care about the directory where your program is. For example if the program  is in Desktop put the directory as correct.

Then you follow the simple saving of the rc.local.

Now you reboot your computer, you can see your program is running in the black window where you need not do anything.

But what is the drawback here is you cannot easily stop the program by simply using Ctrl+Z or Ctrl+C or with anything. You may thing it is bad but if you think as you are going to set up our product to a normal home user where he cannot do anything with just stopping using Ctrl+Z or with anything. So how can we do this ?

Yes it can be done with some modifications of the cmdline.txt file in the memory card. For this I come later. Easily you can do this when you switch of and switch on the Raspberry Pi, It will as you to recovery press "Shift". Press Shift and you can see an option "Edit config". Click on that you will get a window of Config Editor where you click cmdline.txt and put the following command at the end of previous commands :

init=/bin/sh      ( Note : Do not confuse with / and \)

Now you can Exit and you will will be in black window where you type the following command again :
sudo nano /etc/rc.local

you will get the rc.local window which you have edited before for adding auto start. Now remove the line and save it. Then you ave to shut down the Raspberry and start again.  To shut down the RPi sudo halt will or other sudo does not work. Type the following : poweroff -f

No power off and power on the RPi again and by pressing Shift remove the init=/bin/sh and exit. Now you can use RPi as normal one. That's all done!

Note : If you want to edit the cmdfile.txt from accessing memory card, You cannot see the file if you are using it in windows. This is because of partition issues. So you have to put the memory card in any Linux operating systems or Mac.