1
\$\begingroup\$

enter image description hereI am trying to operate switch relay when Resistance is at peak value (1Mohm) using PIC16F877A. enter image description here

Now the issue is as below,

When i run the same code with Proteus , Its working fine. But when i run the same code on Hardware i am not getting the output i.e. Relay and LED is not getting ON.

Some observation from my end on hardware are as below. 1> When i try to increase the value of variable resistor till the peak value, The value of 5 V is getting transmitted at a half variable resistor value 2>The pin at which relay is connected is not getting 5 v voltage as a result it is not getting operated.(highlighted in yellow in attached proteus schema)

proteus schema and C code is attached.

can anyone help me to find what might be the potential issues possible in this, Appreciate the kind help in advance.

#include<pic.h>
#include<xc.h>

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 2000000

void delay(int cnt)
{
    while(cnt--);
}

void ADC_Init()
{
    ADCON1=0XCE;
    TRISA=0XFF;
}

int ADC_Read(unsigned int ch)
{
           ADCON0=0X81;
           ADCON1=0XCE;
           TRISA=0XFF;

    int result=0;
    delay(50);
    GO_nDONE=1;
    while(GO_nDONE==1);
    result=ADRESH;
    result=result<<8;
    result=result|ADRESL;
    return result;

}
int main()
{
    unsigned int adcValue=0;
    unsigned int channel;

    TRISA0=0x01;

    PORTB=0X00;
    TRISB0=0X00;

    TRISC0=0X00;

     ADC_Init();
     while(1)
     {
         adcValue=ADC_Read(0);

                    if(adcValue==1023)
                    {
               RC0=1;
                       RB0=1;
                       delay(100);

                    }
                    else
                    {
               RC0=0;
                       RB0=0;
               delay(100);
                    }

     }

   return 0; 
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ The 4K7 may be too big for that LED, does your LED light up at 1mA? Also a 10K resistor on the base of that transistor looks very big, I would have used something like 100Ohms, remember the transistor will amplify the base current, so if the resistor is too big it cannot turn on the relay ... \$\endgroup\$
    – Cobusve
    Dec 25, 2018 at 16:57
  • \$\begingroup\$ Also what exactly is VDG in your schematic connected to in real life? That symbol means GND, but it would have to be 5V for your circuit to work? From the schematic it looks like the voltage on MCLR will reduce as you turn the POT which would disable the processor so nothing would turn on ... \$\endgroup\$
    – Cobusve
    Dec 25, 2018 at 17:04
  • \$\begingroup\$ Dear Cobusve, Appreciate the kind reply. I have changed the scema as per exact real life scenario. Please review , Might give a better idea. \$\endgroup\$
    – PICFanPri
    Dec 26, 2018 at 2:50
  • 1
    \$\begingroup\$ 1 Meg is much too high a value for the pot - try 10K or so. The PIC ADC input may have some leakage current, which would affect the voltage at the pot wiper. The effect of the leakage woul be much less with a 10K pot. From your reported measurement, you may have the pot connected incorrectly. Disconnect the pot wiper (usually the middle pin) from the PIC, and measure the voltage on the wiper as you adjust the pot - it should go from Zero at one end of travel, to +5V at the other - if not, you have a faulty pot or wrong connections. \$\endgroup\$ Dec 26, 2018 at 5:43
  • \$\begingroup\$ You have the watchdog enabled and no watchdog reset in the code. Try WDTE = OFF \$\endgroup\$
    – Dorian
    Dec 26, 2018 at 19:41

1 Answer 1

2
\$\begingroup\$

You have the watchdog timer enabled and no watchdog clear in the code.

Without the prescaller the watchdog will reset the chip after 10 to 35ms so it won't go past ADC_Read function which contains a 50ms delay.

Also Peter had a good point , use a smaller pot, the maximum ADC source impedance is 10kohm for PIC microcontrollers you're 50 times over it.

\$\endgroup\$
2
  • \$\begingroup\$ Appreciate the kind response. But it is working now on hardware with the same configuration. It’s a pot issue. \$\endgroup\$
    – PICFanPri
    Dec 31, 2018 at 4:49
  • \$\begingroup\$ @PICFanPri It seems that the delay call is also taking care of the watchdog reset when needed. Happy New Year. \$\endgroup\$
    – Dorian
    Dec 31, 2018 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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