Saturday, 1 August 2015

Bluetooth Controlled robot using Arduino





Introduction

Its a robot which will move by the characters it will receive through bluetooth. You can use any medium for transferring those characters to your robot. Keep in mind that these characters are those which is programmed in our robot, and your robot knows which task to perform when receiving that specific character. We will use an app called "Android Bluetooth RC Car" you can download it from here:






Thursday, 12 February 2015

Interfacing LCD with PIC Microcontroller

Hello Guys!
Today I am going to show you how to interface an LCD with PIC Microcontroller using MikroC Pro for PIC.

I assume that you know how to glow an LED using PIC Microcontroller, and you have tried it on hardware as well.

Okay now lets begin:

Step1 (Writing Code):


MikroC Pro has a build in Library code for LCD Interfacing with PIC Microcontroller. Open up MikroC Pro Select new project:


A dialog will appear click next:


I am using PICF877A so I am selecting PIC16F877A then click next:


Select a crystal frequency that you're going to use, I am selecting 16MHz, click next:



It will ask the path for saving file, and click next, next and next, copy the code below and paste it in mikroC.


// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "Programmed by";
char txt2[] = "Shahrukh Saleem";

//main method
void main()
{
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
Lcd_Out(2,1,txt2);
}


Compile your code to generate a .hex file or you can download the .hex file from here

Step2 (Simulation on Proteus):





you can download proteus file from here

NOTE: double click on the controller and select the .hex file

Step3 (Burn your PIC using PIC Programmer):


Well I have explain in detail about how to burn code in your PIC Microcontroller, in my previous tutorial of glowing led's through PIC Microcontroller

Step4 (Implement on Hardware):





Final result:


Make sure to share this tutorail with your friends so that every one can get the most out of it.
Thankyou!

Regards:
Shahrukh Saleem Qureshi

Sunday, 28 December 2014

Glowing LED's through PIC Microcontroller

This is a simple tutorial on how to glow LED's using PIC Microcontroller, this will be helpful to those who have very little knowledge about microcontroller's and want to get started with PIC.
To begin keep in mind that there are several PIC microcontroller's out there you can choose any if you want. Here I am using PIC16F877A (40 pin pic microcontroller) because this controller has plenty of circuits on internet, you can take a look.

Ok keep in mind we write code either in C language or Assembly, here I will use C language, because it is easy to understand. To write your code you need a software or an IDE, The three main softwares to write your code are:

1) MPLAB 
2) CCS        
3) MikroC Pro

MPLAB is a little tough to begin with, I will use MikroC Pro. MikroC is easy to understand plus its help library provides enough information to begin. To burn your code on to a PIC Microcontroller we need a circuit that transfer's your code to PIC, (of course you can't connect your PIC directly to PC).

This circuit is called 'PICKIT' which is designed by microchip to burn your code to PIC. There are many versions available like PICKIT1, PICKIT2 and the latest one PICKIT3. If a PICKIT's is locally manufactured it is called a 'PIC Programmer'. PICKIT or PIC Programmer both have the same function, the difference is the manufacturer. Local PIC Programmer's are inexpensive  as compare to PICKIT. To run this PICKIT or PIC Programmer you need a separate software, the name of software is 'PICKIT2 Programmer' Software although it was developed for PICKIT2 but works with many locally PIC Programmer's as well.

Simple as this:



Some Important factors you should keep in mind:


>> Every PIC Microcontroller has different Capacity, functions and number of Pins, it depends as your requirement which one you choose.

>> There are different bits available like some PIC's are 8 bits some are 16 bits.

>> Pins are divided into Port's. So that we can control them easily through coding like, this particular PIC16F877A has 40 pins. Take a look:

Double sided arrow means pin can be used either for i/p or o/p,
Vdd, Vss and MCLR are just i/p's.

Don't confuse yourself, its just looking difficult but its not.
As I told you Pins are divided into ports like Port A, Port B, Port C and so on. (this PIC16F877A has Port A,B,C,D and E, always read data sheet before buying a PIC)

>> If port C has 8 pins then Port C is 8 bit port and will consist of RC0, RC1, RC2, RC3, RC4, RC5, RC6 and RC7 which are pin15, 16, 17, 18, 23, 24, 25 and 26 respectively. (As you can see in diagram)

>> How much port A ?
Port A has 6 pins means port A is 6 bit. i.e. RA0, RA1, RA2 RA3 RA4 and RA5 which are pin 2, 3, 4, 5, 6 and 7 respectively. (Now you can find Port B, D, and E easily)

>> To run a microcontroller you need a crystal which serves as an oscillator (it is a 2 pin component, it can be 8MHz or 16MHz or any depending on the speed you want the microcontroller to respond). The crystal is connected to OSC1 (pin13) and OSC2 (pin14). (in this PIC)

>> While programming in C, if you want to take input or output from a port, use the keyword "TRIS" followed by the port name.

>> To take input like from port B write: "TRISB = 1;"

>> To give output write "TRISB = 0;", to give output from port B.

Whats ALL YOU NEED TO KNOW...!!!

LETS START..!

STEP1: Write Code in MikroC Pro

To make a new project in mikroC go to the icon shown below:


A dialog box will appear, click next


Select the PIC microcontroller you will use, then click next. I am selecting PIC16F877A.


Enter the value of crystal you will use, and click next. Here I am using 16MHz crystal.


Select the directory for mikroC files,


Click next.


Skip this and click next.


Ok its done, click finish.


This will appear:


before you write code make sure to click "check all" to use all the library functions, as shown:


like this:


Write your code, I have written this one:

/*      Pic microcontroller tutorial
                  by
            Shahrukh Saleem         */


unsigned int i = 0;               //UNSIGNED Integer declared

void main()                      //main method
{
  TRISB = 0;                      //PORTB AS OUTPUT

for(;;)
 {                        //Infinie loop
  for(i=0;i<=255;i++)              //for loop from 0 to 255
  {
    PORTB = i;                     //Send value of i to PORTB Where LED's are connected
    Delay_ms(250);                 //Delay of 250,
  }
 }
}

(download link of .hex file is provided at the end)

We will discuss about this code, latter.


After your done goto Project-->>Build, short key is Ctrl+F9. This build will generate a .hex file. This is the file which is to be burn on your PIC Microcontroller.


If no errors are found you will see this:


Now, lets test it on proteus. Make the circuit as shown:


Double click on controller, a dialog box will appear, locate this hex file that you generated, it will be in the project's folder. Select 16MHz crystal, since during coding we selected 16MHz.


Double click on crystal, select 16MHz from here as well.


Every thing is done, play the circuit the led's will glow according to binary.



STEP2:(Connect PIC Programmer)


Connect your PICKIT or PIC Programmer to your PC. From image below see the pic configuration of PICKIT2, other PIC Programmer also have similar pin configuration like this, locate the pin of pic programmer and through jumper wires connect it to PIC16F877A,



From diagrams above we can see that:
Pin1 of PICKIT2 = Pin1 of PIC
Pin2 of PICKIT2 = Pin11 and Pin32 since both are Vdd
Pin3 of PICKIT2 = Pin12 and Pin31 since both are Vss (Ground)
Pin4 of PICKIT2 = Pin40 (PGD Pin)
Pin5 of PICKIT2 = Pin39 (PGC Pin)
Pin6 is not connected.

STEP3: BURN PROGRAM TO PIC MICROCONTROLLER


After making these connections open PICKIT2 Programmer Software, (you can download it free from here: http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20v2.61.00%20Setup%20dotNET%20A.zip )




1) Now click Read button
2) Click Erase 
3) Click Blank Check
4) Go to file-->> import hex and select the hex file that you generated through MikroC Pro
5) Click Write
6) Click Verify

Now you have successfully transferred your code to your PIC. 

STEP4: (Implement on Hardware)

here is the video take a look :


Understanding the CODE:

unsigned int i = 0;               //UNSIGNED Integer declared

void main()                      //main method
{
  TRISB = 0;                      //PORTB AS OUTPUT

for(;;)
 {                        //Infinie loop
  for(i=0;i<=255;i++)              //for loop from 0 to 255
  {
    PORTB = i;                     //Send value of i to PORTB Where LED's are connected
    Delay_ms(250);                 //Delay of 250,
  }
 }
}

In this code an integer is declared which is "i", this is an unsigned integer which has range from 0 to 255 signed integer's range from -127 to +127. After his there is the main method, in which Port B is defined as output so TRISB = 0 (discussed before).

After that take a look at the inner for loop since initially "i" = 0 then PORTB = 0 means PORTB = 0000 0000, when "i" = 1 then PORTB = 1 means PORTB will display 0000 0001. For example if we have "i" = 15 then PORTB = 15 this means PORTB = 0000 1111 since PORTB is 8 bit thats why from LSB to MSB only 4 bits are filled. ( Note that RB0 is LSB while RB7 is MSB ) when "i" reaches 165 then PORTB = 165 means PORTB = 1010 0101, this implies only LED's at RB0, RB2, RB5 and RB7 will glow.

Now you must be wondering why I used the "Delay_ms(250);" ? Well if you delete this line then the LED's will turn on and off so quickly that you can't watch it through naked eye. To increase the delay time, this Delay_ms(<value>) is used the value is how much delay you want. Whenever the value of i changes and it goes to PORTB you can watch it through the delay like whats happening on PORTB.

Now this whole is included in an infinite for loop 
for(;;)
{ .....
.....
}
Obviously you don't want this circuit to stop after the value of i reached to 255, if you want this program to run again and again then this infinite loop is used whenever the compiler terminates and executes the whole program, due to this infinite loop it executes the program again.

OK SO THIS WAS A SIMPLE TUTORIAL ON HOW TO GET STARTED WITH PIC MICROCONTROLLER, FOR MORE INFORMATION YOU CAN READ THE BOOK "Pic Microcontroller and Embedded Systems using Assembly and C for PIC18 family by Muhammad Ali Mazidi"

Download the hex and proteus files: 



You can share this tutorial with your friends so other people can get the most out of it. 
Thanks.

Regards:
Shahrukh Saleem Qureshi

Sunday, 31 August 2014

Obstacle Avoiding Robot With IR Sensors

Hello, this is my first blog post. Currently I am in 4th semester of Electronic Engineering at Sir Syed University Karachi Pakistan. I wanted to make a robot without microcontroller but I didn't wanted to make an LFR because I already made it as a project of EDC in 3rd semester. So I searched on the internet looked some really cool robots but they included microcontroller programming that I seriously don't wanted to do at this stage. After quite searching I came with the idea of "Obstacle Avoiding Robot". I got the Circuit diagram from http://www.roboticsbible.com here is the photo after completion:

Obstacle Avoiding Robot

Ok LETS MAKE IT...!! 

Step 1: (COMPONENTS)
Gather the components:

For IR SENSORS:
1) 1K Resistor 
2) 10K Resistor
3) IR Receiver LED's
4) IR Transmitter LED's
5) LM358N OP-AMP IC
6) 10K TRIMPOT
7) 8 Pin IC Logic Chip Socket
8) 3 Pin wire connector

For Main Circuit:
9) IC 7407 Hex inverter IC
10) IC L293D Motor Driven IC 

Other Parts:
11) 12V Geared Motor
12) Wheels
13) Sheet for making body
14) Motor holder (Pipe holder)
15) Castor Wheel
16) 2 Pin wire connector
17) Battery
18) Battery holder
19) Switch
20) Jumpers
21) Verroboard
22) Soldering Iron
23) Soldering Wire

Step 2: (MAKING IR SENSOR MODULES)
Ok so we have all the parts in gathered LETS START BUILDING...!!
First we will make IR Sensor here is the CKT Diagram:
CKT Diagram for IR SENSOR MODULE

NOTE: THERE IS A MISTAKE IN THE DIAGRAM THE POTENTIOMETER WILL BE CONNECTED TO PIN 2 AND IR RECEIVER WILL BE CONNECTED TO PIN 3 OF LM358N IC.

I spend hours troubleshooting this diagram since the circuit was not running correctly finally I found the mistake the pin 2 and 3 of LM358N was wrongly connected.





Make two IR Sensors. 
TESTING:
Connect the battery, 9V battery can be used doesn't matter. I won't harm the IC LM358N
now move your hand towards the IR receiver the LED on pin 1 should glow when your hand is near IR Transmitter. The IR Transmitter LED should be a little tilted towards the IR Reciever LED, it should not be parallel. So that the Infrared rays transmitted from the IR Transmitter after hitting to an object should be reflected towards the Receiver and hence the LED on pin 1 glows. Notice I used two IR Transmitter LEDS across the IR Receiver LED just for accuracy.


Step 3: (MAKING MAIN CIRCUIT)
Lets make the Main circuit diagram:

Main Circuit Diagram

After building this main circuit connect it to 2 IR Sensor module circuits as shown in figure. 

Step 4: (MAKING BODY)
I didn't made the body special, just used single sheet of plastic and pasted the circuit's over it, as shown in figure.


Step 5: (ASSEMBLY)
Assemble all the parts, connect the both IR Sensor Modules with the main circuit. and place it on the body of the Robot. Connect the switch. The IR Sensors should be at LEVEL with the tires as shown
You must be wondering the +5V fixed power supply on the left hand side. When I completed the robot I noticed that the robot was even slow on +9V battery (since I was using 12V Geared motors) so I added another +9V battery. So now I had two batteries both +9V one was new and the other was giving I think 8+ Volt. I added both so I got 9+8 above 17 volt. And now the robot was running with speed.

Step 6: (MAKE A TRACK)
If you want you can make the track I used Sheets of paper as shown in pictures:




ROBOT IN ACTION:
HERE IS THE VIDEO:

https://www.facebook.com/video.php?v=473220692820285&l=4380642576510616884