Recently I was working on a security project using PIC16F877A. The program works fine on Proteus, but when I build my circuit on a breadboard, it gives random sentences. It should wait for the input through the keypad, but instead it enters random inputs by itself and displays wrong password.

Here is the code:

char keypadPort at PORTb;
// Lcd pinout settings
sbit LCD_RS at Rd4_bit;
sbit LCD_EN at Rd5_bit;
sbit LCD_D7 at Rd3_bit;
sbit LCD_D6 at Rd2_bit;
sbit LCD_D5 at Rd1_bit;
sbit LCD_D4 at Rd0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISd4_bit;
sbit LCD_EN_Direction at TRISd5_bit;
sbit LCD_D7_Direction at TRISd3_bit;
sbit LCD_D6_Direction at TRISd2_bit;
sbit LCD_D5_Direction at TRISd1_bit;
sbit LCD_D4_Direction at TRISd0_bit;
int kp;
int password[5];
int DefaultPassword[5]={1,2,3,4,5};
int i;
void main() {

     lcd_out(1,1,"Enter Password");
     for(i = 0 ; i<= 4 ; i++)
     kp = 0;
      kp = Keypad_Key_Click();
      while (!kp);


      case  1: kp = 1; break;
      case  2: kp = 2; break;
      case  3: kp = 3; break;
      case  4: kp = 'A'; break;
      case  5: kp = 4; break;
      case  6: kp = 5; break;
      case  7: kp = 6; break;
      case  8: kp = 'B'; break;
      case  9: kp = 7; break;
      case 10: kp = 8; break;
      case 11: kp = 9; break;
      case 12: kp = 'C'; break;
      case 13: kp = '*'; break;
      case 14: kp = 0; break;
      case 15: kp = '#'; break;
      case 16: kp = 'D'; break;

     if( strcmp(password,DefaultPassword) ==0)
     lcd_out(1,1,"Wrong Password");
     goto Begin;


and this is the output:

enter image description here enter image description here

Added: This is the schematic:

enter image description here

  • 1
    \$\begingroup\$ Any weason to use goto instead of while loop? I guess the main problem is in the Keypad_Key_Click function, which you, of course, have chosen to hide from us... \$\endgroup\$ – Eugene Sh. Aug 23 '16 at 13:57
  • \$\begingroup\$ Post the function Keypad_Key_Click() \$\endgroup\$ – Armandas Aug 23 '16 at 13:58
  • \$\begingroup\$ keypad_key_click() is a function that stores the value read by the keypad into the variable called kp \$\endgroup\$ – Mahmoud Mansour Aug 23 '16 at 14:01
  • \$\begingroup\$ We understand what it is supposed to do. But apparently it doesn't do it. So post the relevant code. \$\endgroup\$ – Eugene Sh. Aug 23 '16 at 14:04
  • 1
    \$\begingroup\$ Well, according to documentation it looks like it should wait for the input indeed. If it doesn't, I would suspect some hardware problem. You better post your schematics here. \$\endgroup\$ – Eugene Sh. Aug 23 '16 at 14:10

From the photos so far, I see 3 hardware problems:

  1. No Vdd & Vss connections to pins 32 & 31. This is required for reliable operation. You cannot assume that the PIC will behave correctly at all times, if you only connect to the power pins 11 & 12. You must connect to pins 32 & 31 as well.

  2. No decoupling capacitors close to the PIC for either Vdd & Vss pins 11 & 12 nor 32 & 31. These are required for reliable operation.

  3. No sign of pull-up resistors near the keypad connector on Port B, near pins 40-33. Without these, you are likely to get sporadic false keypress detection, which fits with your symptoms.


  • Please supply your schematic, as already requested by Eugene Sh. Due to your low "rep" (points) you can't add another link into your question, but add the link to the image into the comments of your question, and someone will add the image itself into your question.

  • Please supply another photo which includes all of the breadboard, preferably taken from directly above the breadboard itself.

Updated to add:

Thanks for your schematic. That confirms that you are missing the pull-up resistors shown in this MikroC Keypad Library page. Here is the example schematic from that page:

Schematic from MikroC Keypad Library page

Notice the 10 kΩ pull-up resistors on the "column" connections. You need those pull-up resistors on whatever are the equivalent pins or your keypad.

Your schematic also shows no decoupling capacitors. I recommend that you research about them, and why they are required. You will find plenty of information here on EE.SE and elsewhere.

As you have now learned, a working Proteus simulation does not mean that the same schematic will work "in the real world". Proteus does not simulate real electronics phenomena e.g. floating inputs, power supply noise/spikes etc. etc.!

  • \$\begingroup\$ i added everything but the problem is still there, even with the keypad completely away from the pic it display random inputs and type wrong password. \$\endgroup\$ – Mahmoud Mansour Aug 23 '16 at 15:06
  • \$\begingroup\$ it was the pull-up resistors, many thanks \$\endgroup\$ – Mahmoud Mansour Aug 23 '16 at 15:22
  • \$\begingroup\$ @MahmoudMansour - Glad these suggestions solved your problem. Feel free to upvote/accept my answer :-) \$\endgroup\$ – SamGibson Aug 23 '16 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.