I'm doing some simulations with optocoupler as a switching circuit to isolate between between high voltage input and a PIC16F690 mcu. Using Proteus simulator, I made the following circuit:
The forward current and typical voltage for the LED of the 4N25 are 50 mA at 1.3 V typically as rated in the datasheet. The 215 ohm anode resistor was calculated based on an assumption that the input is a 12V swinging signal (high and low), which I mimic by using a button, so it was chosen to provide the 50mA current at 1.3V drop for the LED. The 10Kohm pull-down resistor is used so that whenever the input signal is high, the collector will conduct current via the 5V supply down through the emitter.
I wrote code that tells whenever the signal at RB4 is high, RC0 goes high and the blue LED illuminates. Whenevr RB4 goes low, RC0 goes low and the blue LED turns off. The code is written using Mikroc IDE.
void main() {
ANSEL = 0x00;
TRISC = 0x00;
TRISB=0xFF;
PORTC = 0x00;
OSCCON = 0b01110001; /* set internal clock to 8 MHZ */
OSCTUNE = 0x1F;
for(;;){
if(PORTB.B4==1){
PORTC.B0=1;
}else
PORTC.B0=0;
}
}
The problem is that when I run the simulation, the blue LED doesn't illuminate regardless of the button state (pressed or not). But when I reverse the condition statement to if(PORTB.B4==0), it illuminates. It's been so long since I typed a code, so it could be a problem in code, configuration or hardware. What do you suggest?