Amazon Store

Tuesday, 17 March 2015

Getting Started with Arduino


 

Any AVR microcontroller based board which follows the standard arduino schematic and is flashed with the arduino boot-loader can be called an arduino board. The arduino is refered to as open source hardware, since the standard schematic is open to everyone and anybody can make their own version of arduino board following the standard schematic. All arduino boards should be compatible with the arduino IDE which can be used to program the arduino boards. The arduino IDE is also open source and anybody can contribute their libraries to the arduino. There is no other tool available which helps in easy prototyping like the arduino does.  The arduino board has all the required circuitary to get the built-in AVR microcontroller running. The output or inputs can be taken from the boards or given to the board using convenient connectors. Both digital and analog inputs and outputs are available in all arduino boards. The arduino boards can also communicate with other devices using standard communication ports like USART, IIC, and USB etc. The most impressive thing is that most of the arduino boards are bread-board compatible which makes both the hobbyist and developers happy.

When it comes to programming the arduino board anyone who have basic knowledge of c programming can quickly get started with the arduino IDE. It is very simple to understand and has built-in functions for almost every simple or complex task. There are lots of libraries available which can be used tointerface the arduino board with any other devices. One can hardly find a hardware module of which the interfacing code is not there in the arduino. Moreover there are lots of examples which can help one to get used with the arduino in a very short time.

This article discusses the steps required for getting started with arduino and explains how to code a simple LED blinking code.

 

The very first thing to do is to select the arduino board which suits the requirement. Visit the website of arduino where one can get all the details about the arduino boards and arduino softwares. Beginners are recommended to use arduino duemilanove board or arduino uno board. Those who are used to with handling the hardware things, soldering etc. can choose arduino pro-mini board. All the details of the available arduino boards can be found from the arduino website itself. This article and the following ones are explained based on the arduino pro-mini board and the IDE version 1.0.3 for windows. The advantage of this board is that it comes in very small in size; bread board compatible burg stick connectors can be soldered according to our requirements. It is very breadboard friendly and occupies very less space of a typical breadboard.


The only difficulty is that it requires another board to load the program into it through USB port. The compatible USB to TTL converter board can be obtained from the nearest vendors or by online purchase. The two boards are so cost efficient that they together cost less than other basic arduino boards.

The latest version of the arduino IDE can be downloaded from the arduino website itself, available for windows, linux, mac etc. One have to simply copy the entire folder into a preferred location in your PC, open the folder click on the arduino icon and that's it, no need to install. 

The programing language is based on C and any one has a basic idea can uickly get start with it. Moreover there is a separate page discussing about the programming details only where one can refer all the details onarduino programming language.

If everything has been done in the correct way the arduino IDE opens up and it looks like the following image in windows;

 

If everything has been done in the correct way the arduino IDE opens up and it looks like the following image in windows;

The first step is to save the project in a folder which is selected to contain all projects and experiments with the arduino. Click file -> save as, select the required folder and give the file name. For all the experiments with the arduino it is recommended to create a folder named "ARDUINO WORKSPACE" and save this particular project as "_1_led_blinking". Sketch names can only consist of ASCII characters and numbers (but cannot start with a number). They should also be less than 64 characters long. The arduino sketch is saved in the file format '.pde'.

Now go to the examples and find the basic led blinking code as shown in the following image;

Now a separate window with the code opens up which is not supposed by the user to edit, since it is a working code and should be kept as such for future references. Select all and copy paste the entire code into the _1_led_blinking window and close the new window with the 'blink code' safely.

All the arduino codes have a small description about it and about the hardware connections at the very beginning of the code file itself as marked inside the green box in the following image.

There is a built-in LED connected to the digital pin number 13 of the arduino pro-mini board and this particular code is for blinking that led with a delay.

An arduino code has two basic functions namely "setup ()" and "loop()". The setup() is the function where all the initial settings like setting the pin as input/output, initializing the serial communication with baud rate etc. The loop() is actually an infinite loop inside which the rest of the code should be written. The user-defined functions if there is any should be written separately outside the setup() and loop() and can be called from inside both of them.

The function pinMode() is a built-in function used to set a particular pin as input or output. The first parameter is pin number and the second parameter suggests whether the pin should be input or output.

For example to make pin number 5 as output

pinMode (5, OUTPUT);

To make pin number 6 as input

pinMode(6, INPUT);

In this particular example the pin13 is already defined as led using the statement

int led = 13

and hence came the following statement

pinMode(led, OUTPUT);

which can make the 13th pin of the arduino board as output.

The digitalWrite() is another function which can be used to write a digital value (logic 0 or logic high) to a particular pin which has already been made as output using the pinMode() function.

For example to make the pin number 5 as logic high

digitalWrite(5, HIGH);

And to make the same pin as logic low

digitalWrite(5, LOW);

The function delay () is a very useful function for almost all the projects and it can generate a delay in milliseconds between the code steps.

For example to generate a delay of 5 seconds,

delay(5000);

Once the coding has been done its time to verify the code and arduino is so user friendly that people won't get too many errors. There is a button for verifying the code at the top left corner of the IDE as shown below;

Once the verification has been done the code is ready to upload to the board. Connect the board to the USB port of the PC and install the driver which comes along with the board. After installing the driver come back to the arduino IDE and select the board from the list using tools>boards>arduino pro mini

After selecting the board, one should also select the COM port on which the board is connected as shown in the following image;

Now try to upload the code to the board by clicking the upload button as shown in the image below;

 

Now one can try changing the delay or connecting an external LED to another pin and all such basic things. No separate power supply is required as the arduino pro-mini board is USB powered.

Refer to the code and circuit for blinking an external LED connected to the pin number 5 of the arduino board.

 

Code:

// Pin 5 has an LED connected on it through 1K resistor.

// give it a name:

int led = 5;

 

// the setup routine runs once when you press reset:

void setup() {                

  // initialize the digital pin as an output.

  pinMode(led, OUTPUT);     

}

 

// the loop routine runs over and over again forever:

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

}

 

 

One Time Programmable Password Lock

Locking and unlocking any device (or system) like locker, cupboard, automatic door, suitcase etc with the help of password is most widely used technique for providing secured access. Only the authorized person - who knows the password, can lock or unlock the system. Once device is locked, if any other person tries to unlock it and enters wrong password then system gives ALERT message or warning (or something like that).

The general operation for password lock system is

·         The system is locked by entering correct password

·         It can be unlocked by entering correct password

·         If wrong password is entered then system will not be locked or unlocked and system shows warning

·         If anyone tries to enter wrong password again and again, system becomes inactive (HANG) so that further attempts to enter password can be prohibited

 

There are two different variants in this system

1.     Factory programmed password: The password is default – factory programmed. It cannot be set or changed. To lock or unlock system user has to enter that default password every time. He/she cannot set his own password

2.     User Programmable password: the password can be set or changed. User can set his own password. In this type also there are two different variants. In one type password can be set or changed many times and in other type password can be set by user only once – that is called  one time programmable password system

Here the given project demonstrates one time programmable password system using micro controller AT89C51. The system has default password initially that can be changed only once. User can set his own 5 digit password once. Afterword password set button is disabled. User has to enter correct password to lock or unlock the system. Also user gets only three attempts to enter correct password. If he enters wrong password thrice, the complete system hangs. No further operation is possible. The only way is to press master reset

Circuit description

 

(Check the circuit diagram tab for complete circuit for one time programmable Password Lock)

·         The 4x3 keypad is connected to PORT3. 4 rows are connected to P3.4 – P3.7 and 3 columns are connected to P3.0 – P3.2. the key press event is detected using row-column scanning

·         Four LEDs – two red and two green are connected to PORT2 pins P2.0 – P2.3 such that LED turns ON when pin is high

·         One buzzer is connected to pin P2.5 through npn transistor connected in switch configuration. It generates beep sound when pin is high for moment

·         Pin P2.4 drives single change over type relay through another npn type transistor. Setting pin high/low will switch ON/OFF the relay

·         Data pins D0-D7 of LCD are connected with PORT0. Control pins Rs and En are connected with pins P2.7 and P2.6 respectively. RW pin is connected to ground. One 1K pot is connected to VEE pin (3) to control LCD brightness

·         A 12 MHz crystal with two 22 pF capacitors is connected with crystal terminals as shown

·         One reset pushbutton in parallel with 0.1 uF capacitor is connected to reset pin (9) as shown to provide manual reset to the micro controller

 

Circuit operation:

 

 

 

 

·         When circuit is switched ON all the LEDs are OFF, relay is OFF and the message is displayed on LCD as "to lock system enter password"

·         After 2 second in place of word password the blank spaces are displayed to enter user password

·         And the circuit waits for user input through keypad

·         User enters password digit by digit. It is displayed as * on LCD. After entering all digits user has to press enter key

·         When enter key is pressed, the circuit compares entered password with set password

·         If they match the system is locked. The correct LED (green) blinks, locked LED (green) turns ON, short beep sound is generated and relay switched ON

·         LCD shows message "system locked" for 2 seconds and after that again it shows message "to unlock system enter password"

·         If entered password does not match, wrong LED (red) blinks, beep sound is generated twice and message is displayed on LCD as "wrong password"

·         If user enters wrong password 3 times then the system HANGs and starts generating continuous beep sound till reset button is pressed. The message is displayed on LCD as "system HANG press reset"

·         To set new password user presses set new password button. Immediately the message displayed on LCD as "set new password (5 digit)"

·         Then by entering digits one by one user can set new password. The digits are displayed on LCD. At the end user presses enter button. This sets new password and LCD shows message as "new password is xxxxx"

·         If again user presses set new password button then message is displayed as "password can be set only once"

Software program:

The complete circuit operation is due to the software program embedded inside micro controller. All the functionalities are implemented using software logic and the program. The program performs all the functionalities like

·      Takes user inputs through keypad

·      Displays different messages on LCD

·      Give indications on 4 LEDs of password correct or incorrect system locked or unlocked

·      Switch ON or OFF relay to turn ON or OFF any device connected with relay

·      Give audio notification for different events

The program is written in C language. It is compiled using KEIL (IDE) cross compiler tool. It is compiled for generic 8051 micro controller platforms so it can be used for all different micro controllers of MCS51 family like 89C51 / 89C52 / 89S52 etc. after compiling the program the HEX file is generated. That HEX file is loaded into the internal FLASH (EEPROM) of micro controller using any suitable EEPROM programmer. 

Programming :

#include <reg51.h>

#include <string.h>

sbit rs = P2^7;                   // rs pin of LCD

sbit en = P2^6;                   // en pin of LCD

sbit led1 = P2^0;

sbit led2 = P2^1;

sbit led3 = P2^2;

sbit rely = P2^4;

sbit led4 = P2^3;

sbit buz = P2^5;

char str1[5] = "12345";           // default password

char str2[5];

int j;

 

void writecmd(unsigned char a);   // function initializations

void writedata(unsigned char b);

void busy(void);

void writestr(unsigned char *s);

void wait_for_2sec()

  {

int y,z;

        for(y=0;y<50;y++)

          for(z=0;z<10000;z++);

  }   

void writecmd(unsigned char a)

 {

       busy();

       rs = 0;      

       P0 = a;      

       en = 1;

       en = 0;

 }

void writedata(unsigned char b)

 {

       busy();

       rs = 1;      

       P0 = b;      

       en = 1;

       en = 0;      

 }

void busy()

{

       int k;

       for(k=0;k<1500;k++);

}

void writestr(unsigned char *s)

{

       unsigned char l,z;

       l = strlen(s);

       for(z=0;z<l;z++)

       {

         writedata(*s);

         s++;       

       }

}

void keydly()

  {

    int x,y;

       for(x=0;x<100;x++)

         for(y=0;y<1000;y++);

   }

 void sound_buzzer(unsigned int t) // produce beep sound

  {

        unsigned int p;

        for(p=0;p<t;p++)                 // beep sound once or twice

          {

               buz = 1;

               keydly();

               buz=0;

               keydly();

        }

  }                 

void main()

  {

       int t=0,i,p=0,f=0,r,y,c=0,l=0,s=0,a=0;

       P3=0x00;                                 // ports P3 and P2 output ports

       P2=0x00;     

       writecmd(0x3C);                          // configure LCD

       writecmd(0x0E);

next:  r=0;i=0;                            

       writecmd(0x01);

       if(l==0) writestr("To Lock System");     // display message

       else writestr("To UnLock System");

       writecmd(0xC0);     

       writestr("Enter Password");

       wait_for_2sec();                         // after 2 second                

       writecmd(0xC6);

       writestr("________");                    // show space to enter password  

       writecmd(0xC6);     

loop:  P1=0xF0;

       while(P1==0xF0);    

       while(P1!=0xF0)

         {

              P1=0xFE;

              if(P1==0xEE)

                {

                     if(f==0)

                       {

                           writedata(0x2A);

                           str2[i]=0x31;

                       }

                     else

                       {

                           writedata(0x31);

                           str1[i]=0x31;

                        }

                     t=1;

                }

              else if(P1==0xDE)

                {

                     if(f==0)

                       {

                            writedata(0x2A);

                           str2[i]=0x34;

                       }

                     else

                       {

                           writedata(0x34);

                           str1[i]=0x34;

                       }

                     t=1;

                  }

              else if(P1==0xBE)

                {

                     if(f==0)

                       {

                           writedata(0x2A);

                           str2[i]=0x37;

                       }

                     else

                       {

                           writedata(0x37);

                           str1[i]=0x37;

                       }

                     t=1;

                  }

              else if(P1==0x7E)

                {

                     writecmd(0x01);

                     if(f==1)

                       {

                            writestr("new password is");

                            writecmd(0xC0);

                           for(y=0;y<5;y++) writedata(str1[y]);                  

                           f=0;

                       }

                     else

                      {

                       if(i==5)

                         {

                            for(j=0;j<5;j++)

                             {

                                  if(str1[j]==str2[j]) p++;                      

                                  else

                                    {

                                         led1=0;

                                         led4=1;

                                         writestr("wrong password");

                                         sound_buzzer(2);

                                         a++;

                                         break;

                                    }

                             }

                            if(p==5)

                              {

                                   c++;

                                   if((c%2)==1)

                                    {

                                         l=1;

                                         rely=1;

                                         led2=0;

                                         led3=1;

                                         writestr("System Locked");

                                    }

                                   else

                                    {

                                         l=0;

                                         rely=0;

                                         led3=0;

                                         led2=1;

                                         writestr("System Unlocked");

                                    }

                                    led1=1;

                                    led4=0;

                                    sound_buzzer(1);

                                }

                              }

                            else 

                             {

                                   led1=0;

                                  led4=1;

                                  writestr("wrong password");

                                  sound_buzzer(2);

                                  a++;

                             }

                            }     

                            if(a==3)

                             {

                                  writecmd(0x80);

                                  writestr("System HANG!!..");

                                  writecmd(0xC0);

                                  writestr(" * Press RESET *");

                                  sound_buzzer(1);

                             }

                     r=1;

                     t=1;         

                     p=0;                

                }                                                                   

              if(t==1) break;

              P1=0xFD;

              if(P1==0xED)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x32;}

                     else {writedata(0x32);str1[i]=0x32;}

                     t=1;

                }

              else if(P1==0xDD)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x35;}

                     else {writedata(0x35);str1[i]=0x35;}

                     t=1;

                }

              else if(P1==0xBD)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x38;}

                     else {writedata(0x38);str1[i]=0x38;}

                     t=1;

                }                         

              else if(P1==0x7D)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x30;}

                     else {writedata(0x30);str1[i]=0x30;}

                     t=1;

                }                 

 

              if(t==1) break;

              P1=0xFB;

              if(P1==0xEB)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x33;}

                     else {writedata(0x33);str1[i]=0x33;}

                     t=1;

                 }

              else if(P1==0xDB)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x36;}

                     else {writedata(0x36);str1[i]=0x36;}

                     t=1;

                }

              else if(P1==0xBB)

                {

                     if(f==0) {writedata(0x2A);str2[i]=0x39;}

                     else {writedata(0x39);str1[i]=0x39;}

                     t=1;

                }

              else if(P1==0x7B)

                {   

                  writecmd(0x01);

                     if(s==0)

                       {

                           writestr("set new password");

                           writecmd(0xC0);

                           writestr("(5 digit):");

                           f=1;                      

                           i=-1;

                           s++;

                       }

                     else

        {

          writestr("password can be set");

          writecmd(0xC3);

          writestr("only once");

          r=1;

        }

                     t=1;                      

                 }                

              if(t==1) break;

        }

        keydly();     

        i++;     

   t=0;

        if(a==3) while(1);

   if (r==0) goto loop;

   else goto next;

  }

 

Electrical Control Panel