Quantcast
Viewing all articles
Browse latest Browse all 215

Temperature controlled DC motor using PIC 16F877A

Image may be NSFW.
Clik here to view.
Temperature-controlled-dc-motor-fan-pic-16f877a-microcontroller
Temperature Controlled DC motor
You might have come across several applications where we need to control a specific device based on analog parameter. This Embedded system works in a similar concept where we are about to control the speed of a DC motor using based on the external temperature. The rise in temperature will result in increase in speed of the motor and vice versa. These type of systems can generally be used to maintain temperature of a room or object automatically.

DESIGN:

  • The temperature is measured by means of a temperature sensor LM35.
  • The output voltage of the sensor is fed to the A/D channel of the Microcontroller.
  • Based on the sensed temperature the speed of the motor is controlled using PWM .
  • Several temperature ranges was set in the code to vary the motor speed based on the level of temperature sensed.
  • The speed of the motor is controlled by using PWM.
  • The motor is driven using a driver IC l293D, See a brief explanation on its working and wiring here

LM35:

Image may be NSFW.
Clik here to view.
LM35-pin-diagram-temperature-sensor
LM35 Pin Diagram
Lm 35 is used to sense the external temperature which is capable of sensing temperature ranges from -55 to 150 C. The output voltage is proportional to the temperature hence there is no need of trimmers to calibrate the reading. The output voltage of this sensor varies by 10mv per degree change in temperature.

CALIBRATION:

We are using a 10 bit ADC and Vcc as Vref to the ADC module of the Controller. So in order to determine the step size we have to divide the Vref by our resolution that is 2^10 ( 1024 ).

                                            Step Size = 5 / 1024 = 4.83mV


We obtain a change of 10mV with each rise or fall in temperature from the sensor. And value in the ADC register will alter by two steps with each degree change in the sensor since two increments of step size i.e 4.833mV * 2 = 9.96mV which is approximately equal to 10mV. So in order to obtain the original value we have to divide the contents in the ADC register by 2
                                            Real value = ADC value / 2


CODE:

This code was built using CCS compiler for PIC Microcontrollers.


  1. #include <16F877A.h>
  2. #device ADC=10                              //Setting ADC bits
  3. #FUSES NOWDT               
  4. #FUSES NOBROWNOUT          
  5. #FUSES NOLVP                
  6. #use delay(crystal=20000000)
  7. #byte lcd=0x08
  8. #byte TRIS_lcd=0x88
  9. #bit rs=0x06.2
  10. #bit en=0x06.3
  11. #bit TRIS_rs=0x86.2
  12. #bit TRIS_en=0x86.3
  13. long int adc_value,real_value;        
  14. int i;
  15. char value[4];
  16. unsigned char text[]="Temperature:";
  17. void display(unsigned char a, int b);
  18. void motor();
  19. void main()
  20. {
  21.   TRIS_lcd=TRIS_rs=TRIS_en=0;
  22.   display(0x38,0);
  23.   display(0x01,0);
  24.   display(0x0c,0);
  25.     for(i=0;i<=12;i++)
  26.     {
  27.      display(text[i],1); 
  28.     }
  29.   setup_timer_2(T2_DIV_BY_16,255,1);    //Setting timers for PWM      
  30.   SETUP_ADC_PORTS(AN0);                   //Setting ADC PORTS
  31.   SET_ADC_CHANNEL(0);                       //Selecting Channel
  32.   SETUP_ADC(ADC_CLOCK_INTERNAL);
  33.      while(TRUE)
  34.       {
  35.         delay_us(20);
  36.         adc_value=READ_ADC();            //Reading ADC value
  37.         delay_us(10);
  38.         real_value=adc_value/2;           //Obtaining real value
  39.         motor();
  40.         sprintf(value,"%lu",real_value);  //Changing int to char
  41.         display(0x8c,0);
  42.           for(i=0;i<=3;i++)
  43.            {
  44.              display(value[i],1);
  45.            }   
  46.       }
  47.  }
  48. void display(unsigned char a,int b)         //LCD sub routine
  49.   {
  50.     lcd=a;
  51.     rs=b;
  52.     en=1;
  53.     delay_ms(10);
  54.     en=0;
  55.     delay_ms(10);
  56.   }
  57.  
  58. void motor()
  59. {
  60.   if(real_value<10)
  61.    {
  62.    setup_ccp1(CCP_OFF);             
  63.    }
  64.   if(real_value>10)
  65.    {
  66.      setup_ccp1(CCP_PWM);
  67.       if(real_value>=10&&real_value<=29)
  68.       {
  69.         set_pwm1_duty((int16)204);         //20% duty cycle PWM
  70.       }
  71.       else if(real_value>=30&&real_value<=69)
  72.      {
  73.        set_pwm1_duty((int16)510);         //50% duty cycle PWM 
  74.       }
  75.       else if(real_value>=70&&real_value<=99)
  76.       {
  77.          set_pwm1_duty((int16)715);      //70% duty cycle PWM
  78.        }
  79.       else if(real_value>=100&&real_value<=150)
  80.        {
  81.          set_pwm1_duty((int16)919);     //90% duty cycle PWM
  82.        }
  83.     }
  84.  }

The above code uses built in functions in the CCS compiler to use the A/D and PWM feature in the Microcontroller. The received analog value is calibrated to display the temperature in the LCD. The "real_value" int value is converted to character using "sprintf" in order to display the temperature values. 

The temperature ranges and duty cycle of the PWM is given using the subroutine ".motor". So the microcontroller runs a check on the temperature every time and alters the speed of the motor based on it. 

Like this Project, share this with others through social sites and don't forget to follow us there for more of these.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles