Monday, January 26, 2015

Remote Controlled LED

This project shows how to control an LED using microchip PIC16F877A microcontroller and RC5 remote control.
Related Topics:

Philips RC5 & LG TV Remote Control Decoder
RC5 Protocol Remote Control Decoder

 It is easy to control the brightness of an led using the PWM technique just you have to vary the duty cycle of the pwm signal as we should know. Here, using the IR remote control we are going to vary the duty cycle in order to control the brightness of an led connected to ccp1(first pwm output connected to RC2 of pic16f877a).
The following picture shows the RC5 remote control used in this project and the used buttons.
Note that I have used just the command part of the remote signal. The on/off button has a command = 12, down button command = 17 and up command = 16.
RC5 remote control

The circuit schematic is shown below:
rc5 ir remote control led pic16f877a mikroc code

Remote controlled LED mikroC code:
The code of this project is done using MicroC pro for pic.

//Control on an LED using remote control
//Used microcontroller: PIC16F877A @ 12MHz
//Used reomote control: equivalent of RC5 protocol
//Written by: BENCHEROUDA Okba
//http://www.elecnote.blogspot.com
//electronnote@gmail.com
//Use at your own risk

 // LCD module connections
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RD3_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD1_bit;
sbit LCD_D7 at RD0_bit;
sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISD3_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD1_bit;
sbit LCD_D7_Direction at TRISD0_bit;
// End LCD module connections

unsigned short ir_read, j, command=0;
short i = 0;
bit st;
unsigned char  *text, mytext[4];
 void Interrupt(){               //External interrupt occured
 //Check if the received signal is RC5 protocol
  delay_us(370);
  if(PORTB.F0==0){
  delay_us(889);
  if(PORTB.F0==1){
  delay_us(889);
  if(PORTB.F0==0){
   ir_read = 1;
  INTCON  = 0;                //Disabe the external interrupt
 }}}
 INTF_bit = 0;                //Clear Interrupt flag
}
   //Display the results on 1602 LCD
  void display_results (){
   Lcd_Cmd(_LCD_CLEAR);            //Clear LCD
   text = "Brightness:   %" ;
   Lcd_Out(1, 1, text);
   ByteToStr(i, mytext);  Lcd_Out(1, 12, Ltrim(mytext));
  }
 void main() {
 OPTION_REG = 0;
 TRISB.F0 = 1;
 TRISC = 0;
  PORTC = 0;
 PWM1_Init(10000);
 PWM1_Set_Duty(255); PWM1_Start();
 st = 1;
 Lcd_Init();
 Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
 Lcd_Cmd(_LCD_CLEAR);             // clear LCD
 text = "Remote" ;
 Lcd_Out(1, 6, text);
 text = "Controlled LED" ;
 Lcd_Out(2, 2, text);
 delay_ms(1000);
 INTCON         = 0x90;   //External Interrupt enabled

while(1){
ret:
while(!ir_read);         //Wait until IR RC5 protocl received
ir_read = 0;
delay_us(12446);
for(j = 0; j < 6; j++){
if (PORTB.F0 == 0) command|= (1<<( 5 - j));//Set bit (5-j)
else               command&=~(1<<(5 - j)); //Clear bit (5-j)

  delay_us(1778);}
  if (command==12) { st = ~ st;
  if (st == 0) PWM1_Stop(); 
  else PWM1_Start();
  INTCON = 0x90;          //External Interrupt enabled
  goto ret;}
  if (command==16) {i++;
  if(i>100) i=100;}
  if (command==17) {i--;
  if(i<1) i=0;}
  PWM1_Set_Duty(i*255/100); 
  PWM1_Start();


display_results();
INTCON  = 0x90;            //External Interrupt enabled
 }
}

RC5 remote control rc5 protocol

The following video how this project works in hardware circuit: