Maker Pro
Maker Pro

Revised inputs from optocouplers

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
I am absolutely a newbie in PIC programming. Only have experience of playing with LEDs.

For a new project, I am considering to incorporate PIC16F887 or something similar.

Following is the flow of the process:
1) Power ON
2) Analog section sends outputs from 6 optocouplers to the PIC
3) Depending upon the state (High/Low) of these inputs from the optocouplers, the PIC processes and sends output to some, LEDs and 3 relays via transistors.
4) The relays revise the outputs of the optocouplers
5) Again the PIC processes and sends the final output to LEDs

How the outputs from the optocouplers be interfaced with the PIC?
I shall be using PICBasicPro.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,884
Joined
Nov 17, 2011
Messages
12,884
How the outputs from the optocouplers be interfaced with the PIC?
It depends on the type of optocoupler you use.
If you use on with a logic output (active), you connect it directly to the PIC's inputs as you would connect a logic gate.
If you use an optocoupler with an open collector output, use a pull-up resistor to generate a logic level signal.

Read this ressource.
 

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
Perhaps, my question is not clear.

I wanted to know how to handle those inputs in programming, do I have to use interrupts?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,884
Joined
Nov 17, 2011
Messages
12,884
Sorry, this wasn't really obvious from your 1st post.

You can, but you don't necessarily have to use interrupts. You can also poll the inputs.It al depends on the circumstances like:
- do you have enough inputs on the µc that are capable of generating an interrupt at all. If not, you will require extra logic.
- how often do you expect the inputs to change? If they change often, polling may have an advantage over interrupt handling.
- how long is the allowed latency from a changed input to recognition within the program? Interrupts may provide less latency than polling.
- how much processing time can you spend on polling the inputs? Polling will take up more processing time than handling interrupts.
 

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
You can also poll the inputs.
Could you please elaborate? This would be my first project with such inputs. And I have very little programming experience.

do you have enough inputs on the µc that are capable of generating an interrupt at all
The µC can be selected according to the requirement. I have 6 inputs and 14 outputs.

how often do you expect the inputs to change?
Only once.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,884
Joined
Nov 17, 2011
Messages
12,884
Could you please elaborate? This would be my first project with such inputs. And I have very little programming experience.
follow the link, polling is described there. In addition, heres's a discussion of polling vs. interrupts.
The µC can be selected according to the requirement. I have 6 inputs and 14 outputs.
But often not all inputs are capable of generating an interrupt. In this case you will need external logic to create an interrupt whenever an input changes and then read the status of the input pins into a variable for further evaluation.
Only once.
Once per second, per minute, ...?
 

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
Once per second, per minute, ...?
It is quite clear in the first post. Here is the flow of the process:

1) Start - Power ON
2) Analog section sends outputs from 6 optocouplers to the PIC
3) Depending upon the state (High/Low) of these inputs from the optocouplers, the PIC processes and sends output to some, LEDs and 3 relays via transistors.
4) The relays revise the outputs of the optocouplers
5) Again the PIC processes and sends the final output to LEDs
6) Stop - Power OFF

On receiving the first set of inputs, the µC generates output, which is locked.
After receiving the revised inputs, the µC generates further/additional output, which is final.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
How does the microcontroller know when these signals from the analogue section are valid and should be actioned? Are they valid immediately on power-up? Or after a short time? Or something else?

Is there a time constraint between when the micro reads these states and when it sends the appropriate states to the relays and LEDs?

How does the micro know when the new input values from the optocouplers are valid? Does it just wait a fixed amount of time?

Is there a time constraint on the microcontroller calculating and outputting the new states?

If there is no particular time constraint, there's no need for interrupts.
 
Last edited:

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
How does the microcontroller know when these signals from the analogue section are valid and should be actioned? Are they valid immediately on power-up? Or after a short time? Or something else?
After a very short time, almost immediately.
Lets suppose the six inputs' states are 1, 1, 1, 0, 1, 1

Is there a time constraint between when the micro reads these states and when it sends the appropriate states to the relays and LEDs?
The µC sends output within few microseconds.

How does the PIC know when the new input values from the optocouplers are valid? Does it just wait a fixed amount of time?
As soon as the relays receive output from the PIC, the states of the six inputs change something like 0, 1, 0, 0, 1, 0

Is there a time constraint on the microcontroller calculating and outputting the new states?
There is no calculation/processing involved. Just decision making, read the inputs and send the outputs.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
The µC sends output within few microseconds.
Is that important? The relays will take typically 2~3 ms to activate...
As soon as the relays receive output from the PIC, the states of the six inputs change something like 0, 1, 0, 0, 1, 0
If that's controlled by the relays, they won't update immediately. The micro will need to wait a few ms to be sure the relays have activated and the inputs are valid.
There is no calculation/processing involved. Just decision making, read the inputs and send the outputs.
So the outputs are a direct copy of the inputs? In that case you could just use 74HC374 or similar latch.

We could go back and forth for a few dozen more posts trying to figure out the best way to do what you want. But how about describing the whole project to us, so we know exactly what you want to do, and why. That generally saves a lot of time.
 

abuhafss

Aug 3, 2010
348
Joined
Aug 3, 2010
Messages
348
Well, please visit this site
I want to use this circuit + PIC to have an LED-based result. Here is my idea:
BJT pin identifier.gifI simply want to replace the LEDs with optocouplers and the switches with relays.
The outputs from the optocouplers will be fed into the PIC which will:

a) illuminate a green LED if the transistor is PNP or a yellow LED if the transistor is NPN
b) illuminate a red LED if the base is connected to red wire OR a blue LED if connected to blue OR green if connected to green wire

c) after a short delay (if required) energizes the coil of the relay connected to base lead. This will change the states of inputs from the couplers. The PIC will now

d) illuminate the specific color LED, corresponding to the lead color, connected to the collector
e) and similarly another LED for emitter

OPTIONALLY
f) illuminate an additional LED to indicate ----> darlington
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,884
Joined
Nov 17, 2011
Messages
12,884
Use Polling - your µC seems to have nothing else to do, so it can as well spend its time in a polling loop.
Add debouncing - the relay contacts will bounce (give multiple contact events for one switch event. This site shows you how to do it in hardware or software.
 
Top