1
\$\begingroup\$

I have an LED connected to pin RA1 of PIC18F25K20 (powered by 3.3V supply) in the following way:

5V rail -> Resistor (200 Ohm) -> LED Anode -> LED Cathode -> RA1

RA1 : Input -> LED is off
RA1 : Output Low -> LED is ON.

Is this the correct way to connect the LED, as the PIC is powered by 3.3V and LED is connected to 5V?

I initialized RA1 to be an input as shown in the DEVICE_Init routine. Code is written in C language - Microchip C18 compiler.

void main()
{
    DEVICE_Init();
    //wait one second
    while (1)
    {
        TRISA = 0; //led is on
        //wait 2 seconds
        TRISA = 0x06; //led is off
        //wait 4 seconds
    }
}

void DEVICE_Init (void)
{
    OSCCON = 0x73; //PRIMARY INTERNAL OSCILLATOR
    PORTA = 0;  
    LATA = 0;           
    TRISA = 0x06;           //RA1, RA2 INPUT
    ....
}

When I first power up the chip, the LED turns ON for a fraction of a second and then turn off. My understanding is that the IO pins are input on reset. So why would the LED turn on for a brief moment at power up?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I had RA1 & RA2 as inputs with LEDs connected..ignore RA0 please..on the other hand, irrespective of the i/o pins, it behaves the same on all.. \$\endgroup\$
    – user101095
    Commented Jun 6, 2017 at 18:54

3 Answers 3

2
\$\begingroup\$

PIC I/O pins wake up in high impedance state on power up.

The reason you see the LED glitch is probably because the 5 V supply is coming up faster than the 3.3 V supply, and the LED cathode is temporarily held low thru the protection diode on that pin in the PIC. This is not good, and can lead to latchup on power up.

If you can arrange to be sure the 3.3 V supply comes up before and goes down after the 5 V supply, then your scheme can work. However, it is better to use a transistor, or to run the LED from the 3.3 V supply. The latter will also waste less power, assuming your 3.3 V supply is reasonably efficient.

\$\endgroup\$
3
  • \$\begingroup\$ Can't run it from 3.3V as it's blue led..and trying to avoid using transistor.. \$\endgroup\$
    – user101095
    Commented Jun 6, 2017 at 18:39
  • \$\begingroup\$ How can I make sure that 3.3V rail comes up first before 5V and dies after 5V? One way I was thinking is to change the bypass filtering electrolytic capacitors for each of this linear regulators..what else can I do to ensure this? \$\endgroup\$
    – user101095
    Commented Jun 6, 2017 at 20:37
  • 1
    \$\begingroup\$ @user: It can be hard to ensure. Using a transistor to switch the LED is likely easier and more reliable. \$\endgroup\$ Commented Jun 7, 2017 at 10:44
0
\$\begingroup\$

Your LED is turning on for a split second because the outputs are reset as Hi-Z (high impedance) or floating. To fix this issue, put something like a 10k resistor from the output to ground. This is known as a pull-down resistor and will make the output low on startup. I asked a similar question here:

https://stackoverflow.com/questions/41557868/atmega128-output-flicker-on-startup

\$\endgroup\$
2
  • \$\begingroup\$ For a small resistor value (less than 100K), led stays dim on when it's not suppose to be on and for higher resistor value, I see it turning on at the power up for a split second.. \$\endgroup\$
    – user101095
    Commented Jun 6, 2017 at 17:57
  • \$\begingroup\$ That's because with the higher resistors, the LED path still has less resistance, so it's going to take the floating voltage. There's probably a magical number, but without seeing everything you're using, it will be impossible for me to get you that. \$\endgroup\$
    – biggi_
    Commented Jun 6, 2017 at 18:05
0
\$\begingroup\$

Do not initialize them as inputs only as outputs. Outputs can source & sink. Any pull down resistors will not do as in this configuration diode is sourcing current. Do not pull-up!!. Just initialise ports asap. Your diode has to have Uv > 1.7V.

\$\endgroup\$
1
  • \$\begingroup\$ So do you mean in order to turn the LED off, I should make pin output high instead of input? \$\endgroup\$
    – user101095
    Commented Jun 6, 2017 at 18:24

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.