Tuesday, August 7, 2018

Pulse Width Modulation Based on Analog Input using PIC18F2550



Hello people in this project I will discuss an interesting topic. The main focus will be on PWM (Pulse Width Modulation), though I have also used ADC (Analog to Digital Converter) and USART (Universal Synchronous and Asynchronous Receiver and Transmitter). Here ADC will be the modulation factor of PWM. Depending on the ADC value width of the pulse will be varied. We used USART protocol to see the corresponding value of PWM.

In this project we will use PIC18 series IC PIC18F2550. This is a 28 pin IC. Also it is a popular IC in PIC18 series.

To do this we need following software:   

   1. MikroC pro for PIC (student verison)
   2. Proteus 8 professional

MikroC pro for PIC will be used for writing and compiling the code. Here we will use C language for programming. Proteus 8 professional will be used for the simulation.

This project is completely simulation based, that's why I am not giving any equipment list. But you can do hardware test, if you want. You can connect a dc motor with the PWM pin to see the effect of PWM, which will vary based on analog input.

What is PWM


Pulse width modulation's main job is to convert the digital signal into analog signal. In this micro controller we have ADC but we don't have any DAC. This PWM module helps us to convert the digital signal into analog signal. PIC18F2550 PWM module is 8 bit. That means we can vary the duty cycle from 0 to 255. Here duty cycle refers that in each cycle how long a pulse will be turned on and how long it will be turned off. 0 means it will be turned off and 255 means it will be turned on. For example just take random number 127, this means in each cycle 50 percent time it will be turned on and 50 percent time it will be turned off. If we set the duty cycle to 192, that means 75 percent time it will be turned on and 25 percent time will be turned off. And pulse is determined by the frequency which we can set by using PWM1_Init() function. If we set the frequency to 5000 hz, then pulse will be 1/5000 s. Because we know f=1/T or T=1/f. Following picture can illustrate the basic concept of PWM.



Figure 1


Application of PWM can be controlling speed of a motor, DC to AC inverter, Light dimming and many more. 

CODE


float valADC;
float valADC1;
float valADC2;

char x[4];

void main()
{
     // Set PORTA as Input
     TRISA = 0b11111111;
     PORTA = 0;
   
     // Set PORTC as Output
     TRISC = 0b00000000;
     PORTC = 0;
   
     // Initializing UART
     UART1_Init(9600);
   
     // Initializing ADC
     ADC_Init();
   
     // Initializing PWM
     PWM1_Init(5000);
   
     //  Starting PWM
     PWM1_Start();
   
     while(1)
   
     {
   
       valADC = ADC_read(0);
       valADC1 = valADC/1023;
       valADC2 = valADC1*255;
       PWM1_Set_Duty(valADC2);
     
       IntToStr(ValADC2,x);
       UART1_Write_Text("Analog Input_1 is: ");
       UART1_Write_Text(x);
       strcpy(x,"");
       UART1_Write(13);
       delay_ms(1000);

     }

}

Schematic



Figure 2


Figure 3


Figure 4


Figure 5


Figure 6

Line Follower Robot using Arduino

This project was a part of Line Follower Robot Competition