I have a Microchip PIC16F18076 microcontroller and have programmed it with the following code, which simply toggles an LED (connected to RA1) state depending on the value of another GPIO pin (RA0):


simulate this circuit – Schematic created using CircuitLab

int main(void) {

  int wasState = RA0_GetValue();
  int inState = RA0_GetValue();
  while (1) {
    inState = RA0_GetValue();
    if (inState != wasState) {
      RA1 = inState;
      wasState = inState;

When testing this I found that the LED on RA1 toggled to ON almost as soon as power hit RA0, but when power was removed from RA0 the the LED would take about a second to turn off. Simplifying the code in the while loop further to:

inState = RA0_GetValue();
RA1 = inState;

did not yield different results. What am I missing, and how can I achieve the near-instant time that the LED turns on in?

  • \$\begingroup\$ It appears it may be a hardware problem, can you post a simple schematic? \$\endgroup\$
    – Gil
    Jan 28, 2023 at 3:44
  • \$\begingroup\$ @Gil I'm not perfect at schematics but I hope this is good enough \$\endgroup\$ Jan 28, 2023 at 4:54
  • 2
    \$\begingroup\$ The schematic you posted can't be the wiring you actually have. Check power supply polarities and such. Please correct. \$\endgroup\$
    – DKNguyen
    Jan 28, 2023 at 5:02
  • 1
    \$\begingroup\$ Check the GPIO pins with a scope. There is probably oscillation or pickup of mains frequency radiation. Also, you do not show bypass capacitors or details of the power supply. They are important. \$\endgroup\$
    – PStechPaul
    Jan 28, 2023 at 5:06
  • \$\begingroup\$ can you post a photo of your test circuit? \$\endgroup\$ Jan 28, 2023 at 6:05

2 Answers 2


I'm going to assume you have Vss and Vdd mixed up on the chip image and +5 goes to the Vdd and to the wire.

This particular MCU does not have internal pull-downs available. If you have configured the input GPIO to have no pull-up then an open input will float around and it might float down below the '0' threshold eventually, or not.

If you turn on pull-ups using the WPU register it won't work at all since the pull-up will be in parallel with the wire.

So you can either turn on the weak pull-up, short the input to ground to change the state and change whatever logic in your program, or you can add a physical resistor (like 5k or 10k) from the GPIO to ground so it has a definite state when the connection to +5 is removed.

  • \$\begingroup\$ Yes I did mix that up thank you! \$\endgroup\$ Jan 28, 2023 at 16:22

The circuit works as expected in real life.

You just seem to expect it to work differently, but in real life that circuit is not used because it does not work in practice.

Chip inputs are not like lamps that either have current flowing or no current flowint to determine between 1 and 0. They are sensitive to the voltage on pin, not current, which is ideally zero.

The input pin will be floating when you disconnect the wire, and you can't trust floating CMOS inputs. Due to exteremely small leakage currents, the voltage may forever float at the level you set it to, or it may take multiple seconds for the voltage to change to the other level.

You just need a pull resistor to set the pin voltage when button is not pushed. Try a 10k.


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.