Archive for the ‘Pic microcontroller Tutorial’ Category

Stepper Motor Interfacing With Microcontroller

►Introduction

A stepper motor is a brushless, synchronous electric motor that converts electrical  pulses into mechanical movement. Every revolution of the stepper motor is divided into a discrete number of steps, and the motor must be sent a separate pulse for each step. The stepper motor can only take one step at a time and each step is the same size. Since each pulse causes the motor to rotate a precise angle, the motor’s position can be controlled without any feedback mechanism. As the electrical pulses increase in frequency, the step movement changes into continuous rotation, with the speed of rotation directly proportional to the frequency of the pulses. Step motors are used every day in both industrial and commercial applications because of their low cost, high reliability, high torque at low speeds and a simple, rugged construction that operates in almost any environment.

►Unipolar stepper motor
The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.

Unipolar Stepper Motor Windings

►Bipolar stepper motor
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance (or no continuity).

Bipolar Stepper Motor windings

Stepper Motor interfacing with Microcontrollers

Stepper motors can be used in various areas of microcontroller projects such as making robots, robotic arm, automatic door lock system etc.Here,I will discuss different controlling types (Half step and Full step), Interfacing Techniques (using L293D or ULN2003) to control stepper motor.

Step Sequence

Stepper motors can be driven in two different patterns or sqeunces. namely,

  • Full Step Sequence
  • Half Step Sequence

►Full Step Sequence

In the full step sequence, two coils are energized at the same time and motor shaft rotates. The order in which coils has to be energized is given in the table below.

Full Step Sequence

►Half Step Sequence
In Half mode step sequence, motor step angle reduces to half the angle in full mode. So the angualar resolution is also increased i.e. it becomes double the angular resolution in full mode. Also in half mode sequence the number of steps gets doubled as that of full mode. Half mode is usually preffered over full mode. Table below shows the pattern of energizing the coils.

Half Step Sequence

►Step Angle

Step angle of the stepper motor is defined as the angle traversed by the motor in one step. To calculate step angle,simply divide 360 by number of steps a motor takes to complete one revolution. As we have seen that in half mode, the number of steps taken by the motor to complete one revolution gets doubled, so step angle reduces to half.

As in above examples, Stepper Motor rotating in full mode takes 4 steps to complete a revolution, So step angle can be calculated as…

Step Angle ø = 360° / 4 = 90°

and in case of half mode step angle gets half so 45°.

So this way we can calculate step angle for any stepper motor. Usually step angle is given in the spec sheet of the stepper motor you are using. Knowing stepper motor’s step angle helps you calibrate the rotation of motor also to helps you move the motor to correct angular position.


►Connecting Unipolar Stepper Motor with Microcontroller(PIC16F887) using ULN2003

Stepper Motor Interfacing with microcontroller Using ULN2003

►Connecting Unipolar Stepper Motor with Microcontroller(PIC16F887) using L293D

Stepper Motor Interfacing With microcontroller Using L293D

Source Code

Here,I  have used PIC16F887  Microcntroller  and Code is written in C using mikroC PRO for PIC.Adjusting the delay will increase or decrease the speed of the motor. Here just for demonstration i have taken some delay, you can change it as you want.

►Programming Full step Sequence

void main() {ANSEL  = 0;                // Configure AN pins as digital I/O
ANSELH = 0;
PORTD = 0;
TRISD = 0;                 // Configure PORTD as output

while(1){
PORTD=0x09;
Delay_ms(500);
PORTD=0x0C;
Delay_ms(500);
PORTD=0x06;
Delay_ms(500);
PORTD=0x03;
Delay_ms(500);
}
}

►Programming Half step Sequence

void main() {

ANSEL  = 0;                // Configure AN pins as digital I/O
ANSELH = 0;
PORTD = 0;
TRISD = 0;                 // Configure PORTD as output

while(1){
PORTD=0x08;

Delay_ms(500);
PORTD=0x0C;
Delay_ms(500);
PORTD=0x04;
Delay_ms(500);
PORTD=0x06;
Delay_ms(500);
PORTD=0x02;
Delay_ms(500);
PORTD=0x03;
Delay_ms(500);
PORTD=0x01;
Delay_ms(500);
PORTD=0x09;
Delay_ms(500);

}
}


Advertisements

Bluetooth Module Interfacing with Microcontroller

Bluetooth® wireless technology is becoming a popular standard in the communication arena, and it is one of the fastest growing fields in the wireless technologies. It is  convenient, easy to use and has the bandwidth to meet most of today’s demands for mobile and personal communications. Bluetooth technology handles the wireless part of the communication channel; it transmits and receives data wirelessly between these devices. It delivers the received data and receives the data to be transmitted to and from a host system through a host controller interface (HCI). The most popular host controller interface today is either a UART or a USB .Here,I will only focus on the UART interface, it can be easily show how a Bluetooth module can be integrated on to a host system through a  UART connection and provide the designer an optimal solution for Bluetooth enabled systems.

Here,I will show two examples of  hardware interface between Bluetooth wireless technology and UART.One example shows an interface between an  Bluetooth module and a PC via UART, and the other example shows an interface between a Bluetooth module and a  Microcontroller via UART.

I have a used WT32 bluetooth module.To know more click here.

WT32 Bluetooth module

Supply voltage at VCC  pin can vary between 1.8 V and 3.3 V. VCC and BTEN  combined to a single 3.3 V supply voltage.

Interface between an  Bluetooth module and a PC via UART

Now connect the PC with Bluetooth module through RS232 over MAX232 or MAX233 level converter.

Bluetooth module Connection with PC

Now,Test  the connection with hyperterminal or any serial port communication software .Here,I have used hyperterminal for test.

Hyperterminal settings

 

– 9600 baud
– no parity
– 8 databits
– no flowcontrol
– 1 stopbit

You can be change this settings via hyperterminal with WT32 bluetooth module command.See more in user guide.

Interface between an  Bluetooth module and a Microcontroller via UART

Connect the bluetooth module with microcontroller.Here,I have used PIC16F887 microcontroller.

Now,Test the Communication between PC and Microcontroller Device.You can use the following code that is written in C using mikroC PRO for PIC.

Source Code

char uart_rd;
void main() {
ANSEL  = 0;                     // Configure AN pins as digital
ANSELH = 0;
UART1_Init(9600);               // Initialize UART module at 9600 bps
Delay_ms(100);                  // Wait for UART module to stabilize
while (1) {                     // Endless loop
UART1_Write_Text(“TEST”);
Delay_ms(2000);                  // Wait

}
}

Output

Now,you can be see the Data “TEST” on Hyperterminal that will send by microcontroller via Bluetooth Module.

Hyperterminal

(more…)

Servo Motor Control Using Microcontroller

A Servo is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio controlled cars, puppets, and of course, robots.

The guts of a servo motor are shown in the picture below. You can see the control circuitry, the motor, a set of gears, and the case. You can also see the 3 wires that connect to the outside world. One is for power (+5volts), ground, and the white wire is the control wire.

Servo motor

So, how does a servo work? The servo motor has some control circuits and a potentiometer (a variable resistor, aka pot) that is connected to the output shaft. In the picture above, the pot can be seen on the right side of the circuit board. This pot allows the control circuitry to monitor the current angle of the servo motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not correct, it will turn the motor the correct direction until the angle is correct. The output shaft of the servo is capable of travelling somewhere around 180 degrees. Usually, its somewhere in the 210 degree range, but it varies by manufacturer. A normal servo is used to control an angular motion of between 0 and 180 degrees. A normal servo is mechanically not capable of turning any farther due to a mechanical stop built on to the main output gear.

The amount of power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control.

How do you communicate the angle at which the servo should turn? The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90 degree position (often called the neutral position). If the pulse is shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degress. If the pulse is longer than 1.5ms, the shaft turns closer to 180 degress.

Standard time vs. angle

Procedure

If you want to control your servo motor from a microcontroller,then you must follow-

bring high a digital port
wait between 1-2ms
bring low the same digital port
cycle a few dozen times per second


Here,i have used PIC16F887  microcontroller.Code is written in C using mikroC PRO for PIC.

Source Code

void main(){

int i;
ANSEL  = 0;                                    // Configure AN pins as digital I/O
ANSELH = 0;

PORTD=0x00;
TRISD=0x00;

while(1){

//Move Anti Clockwise direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(1000); //want servo to move to 0 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}

//Move Clockwise Direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(2000); //want servo to move to 180 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}

}
}

Circuit Diagram

Creating The First Project in MiKroC pro for PIC

we  will create a new  project,write code,compile it in the mikroC pro for PIC  compiler .

First, Start up the mikroC pro for pic compiler and select the new project option from project menu,as shown in figure on below.

When you will select new project option,then you will get a window ,as shown in figure on below.Click Next.Now Follow the given steps.

  • Select the device that you want to use
  • Select the  device clock
  • Specify where project will be saved
  • Select initial library manager state

After all,Click finish and  a new blank window to write a program in will appear.See figure below.

Now,type your program .when the  program is written,it is necessary to compile it into a program code(.hex),by selecting one of the build options from the build menu.

All the errors detected during compilation will be shown in the message window.If no errors are encountered,the compiler will generate output files in the project folder containing the project file.

Led blinking program with pic16f887 and mikroC PRO for PIC

Description

The following program flashes 8 LEDs on  the PORTC pins of the pic16f887.

Source Code

void main() {
PORTC=0;  //initialize portc
TRISC=0b00000000;   //configure portc as output
ANSELH=0;    //configure an pin as digital I/O
ANSEL=0;
while(1){
PORTC=~PORTC;     //toggle portc
delay_ms(10000);   //1s delay
}

}

Circuit Diagram

Led blinking circuit