# input module for Mico controller , advice needed

Status
Not open for further replies.

#### deepmak

##### Member level 2
greetings all
i'm trying to make an input module for a micro controller circuit that monitor the status of multiple switches (NC/NO) , the input module should be able to deal with both type of switches without the need of changing the PIC programming.
i tried to solve this using optocoupler since the switches are connected to 24VDC to 27.5 VDC (batteries with charger ), the output of the optocoupler is connected to an inverter , the input of the PIC pin can be selected using a jumper according to the normal operation mode of the switch (No or NC )
could you please check the attached photo and advise if any other consideration i should make , the micro controller and 24 VDC will share same ground.

also i was checking online , any of you used the 6N137 optocoupler ? can it serve for the same function using the jumper to apply 5 v or 0v at the enable pin ? and would it be better to use the 6N137 this way to make less complicated PCB ?
thanks in advance
Regards

Last edited:

#### HMS1021

##### Full Member level 2
6N137 is not the solution
If Ve (pin 7) is low then the output will always be high per the truth table on page 2 of the data sheet. The device is not enabled when Ve is low so this is expected. If you do not need the isolation of a opto a single MOSFET and resistor would be much cheaper.

Depending on how the switch is used there may be a software solution. If the switch button is momentarily pushed and released (like a door bell, vending machine selection, etc) then simply use the time in each state to decide if it is pushed.
As an example:
Assuming a 100ms loop using a timer interrupt:

if button(current_loop) = button(current_loop - 1)
button_pushed = 0
else if button(current_loop) not= button(current_loop - 1)
button_pushed = 1

With this approach it does not matter whether a NO or NC is used. In software you can also control the minimum time the button must be held to be considered pushed and the maximum time it is push before it is ignored by counting the number of loops between changes. Example: 2 to 10 loops = 200ms to 1000ms.

deepmak

### deepmak

Points: 2
Helpful Answer Positive Rating

#### deepmak

##### Member level 2
Good day HMS and many thanks for your post and suggestion
as a matter of fact , the switches are latched type and not momentary ,regarding the MosFet and the resistor configuration , i haven't used them before so i did some search and i believe you mean the CMOS compatible voltage divider, here another issue i would ask about , what if the voltage (24 VDC ) rise to 30 VDC for some reason ? would the output of the divider increase proportionally ? if this is the case i guess there might be moments that the input at the Micro pin will go above 5 vDC.
also thanks for getting my attention to the 6N137 i checked the datasheet again , the seen the truth table , it is no use as you said.
i also asked my friend who requested this module , the environment where he will use it is like machinery room , with alot of relays and contactors working on 220 , 380 VDC and 24VDC would this pose another challenge on the input module design ?
thanks again
Regards
i asked regarding the switches , they are outputs of some sensors , once a switch change state , this means an abnormal condition and the controller should react with flashing corresponding LED and a sounder will activate , a reset button should silent the alarm but keeps the corresponding LED lit , until the situation is corrected, however the controller should not reset it self if a switch toggled say for only few seconds and back to normal. i simulated this case for one switch and did the coding for it , so i will repeat the code for multiple switches.
also i looked around , i think what you meant exactly is using FET/MOSFET as constant current source , thus , the variation in DC voltage do not affect the output at the resistor,however im not sure yet about the thermal stability, any ideas ?
thanks again

Last edited:

#### HMS1021

##### Full Member level 2
The MOSFET is used as a switch, it is basically a single gate open drain logic device.
Vgs (voltage gate to source)
Vds (voltage drain to source)

A MOSFET (eg 2N7002) with a max Vds of +60V and a max Vgs of +/-20V in a small SOT23 package is $0.20 USD and 0402 resistors are <$.01 each.

To accommodate the NO/NC switches you could also add the components for a similar circuit but using a P channel MOSFET (source to Vdd) and then use your jumper to select the desired output. Personally I like your circuit with the jumper and inverter. You should add a 10k pull down resistor on the inverter's input to ensure it does not chatter when not used.

In the schematic Vdd should be >3V
Also a 0.1uF cap parallel with R2 would tame some switch bounce. For pennies you could also add a back to back TVS diode across R2 to harden it against ESD or other off board threats.

deepmak

### deepmak

Points: 2
Helpful Answer Positive Rating

#### deepmak

##### Member level 2
Hello again
i was thinking of a way to simplify the input module while still being able to deal with the different normal state of the switches (NC/NO) without the need of extra components , I've come out with the idea of using the internal EEPROM to store the default (normal ) status of the switches and then determining the status of abnormal condition with that status , keeping a simple input module hardware consisting of only opto-couplers and their resistors , say i will use the optocouplers in inverted output mode (output from collector ) and dealing with the software part as following :
at the start of the program it will check for the value of a char or short variable (i will name it :programmed) at the location 0x00 of EEPROM ( location can be changed ), and compare it to a set value , say 0xFA;
since this will be the first run there will be no value for programmed , the software will cause one of the output led to flash (say the one connected to RB0), the user if supposed to press 2 buttons together for like 2 seconds;
pressing and holding the 2 buttons will tell the software to read the inputs say at PORTA and store it in a char or short variable named Def_inputs , and write this value at EEPROM at location say 0x01;and the value of programmed as 0xFA at location 0xFA and then lit another output LED for one second ,say the one at RB1 . this will indicate the capture of normal status info;
the program will then read the value of Def_inputs and the status if each switch is stored in Sbit variables defined like :
Sbit Normal_SW1 at Def_inputs.B0 ;
then the process of the switches status can be processed accordingly.
the "field " programming of the inputs should be done while all switches are in normal status
a mean of DIP switches for programming can be added as well
any comments or suggestions are welcome , also sorry for being lengthy and if this post should be moved to microcontroller section kindly move it
thanks and regards

Status
Not open for further replies.