Author Topic: Optocoupler to microcontroller input  (Read 2823 times)

0 Members and 1 Guest are viewing this topic.

Offline villagerTopic starter

  • Newbie
  • Posts: 8
  • Country: 00
Optocoupler to microcontroller input
« on: April 23, 2014, 02:10:09 pm »
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.

Code: [Select]
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?
« Last Edit: April 23, 2014, 02:13:15 pm by villager »
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: Optocoupler to microcontroller input
« Reply #1 on: April 23, 2014, 03:24:19 pm »
some simulations
Quote
Using Proteus simulator
Quote
The problem is that when I run the simulation
Quote
It's been so long since I typed a code
Just one code?
Quote
What do you suggest?
Buy some real physical hardware and forget the simulators...
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6460
  • Country: nl
Re: Optocoupler to microcontroller input
« Reply #2 on: April 23, 2014, 05:34:20 pm »
I personally always ground the emitter of a npn transistor and place a resistor in series with the supply and the collector.
Then place the input of the microcontroller at the collector and it should be '1' when optocoupler is off and '0' when on.

Quote
The forward current and typical voltage for the LED of the 4N25 are 50 mA
sounds more like a maximum current then typical, at 5mA - 10mA it should already conduct.
« Last Edit: April 23, 2014, 05:37:47 pm by Kjelt »
 

Offline villagerTopic starter

  • Newbie
  • Posts: 8
  • Country: 00
Re: Optocoupler to microcontroller input
« Reply #3 on: April 23, 2014, 08:20:38 pm »
I tried doing the pull-up resistor but it changes nothing. I used the voltmeter to see if there's voltage between the terminals of the pull-down resistor when I press the button, and yes, there's around 4.9 volts. When the button is not pressed, it reads 0 volts of course. It's really weird. Could it be a problem with the simulator? I look for other answers.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf