Single MCU GPIO pin, read and write, amplify the output, protect the input - Page 1
Forum section: electronics / beginners

author: bonanz
author_rank: Contributor
timestamp: June 01, 2013, 07:12:00 am
content: Is there a simple way to do this or is it even possible?I have a mcu, with a bi directional GPIO pin. I'm interfacing with the "real world" with timed 3.3v pulses, these are slow pulses separated by the order of seconds. When I'm transmitting a pulse, I think I need to source more current. If I was only transmitting with this pin to the "real world" I would maybe just use a unity gain opamp setup on the GPIO pin...but I also need to read back a voltage pulse that the "real world" interface will send to me at some point, so it may be a little messy and I would want to protect my GPIO when it's an input with some diode clamp setup or opto isolate or something so I can just see I have "high" pulse received for xxx ms lenght of time.so I wanted in my software pseudo code:set pin 0 as input, read input continuously to see if I get a pulsewhen I need to transmit a pulse, change the pin to an output, set my pin high for necessary time,then switch my pin back to an input and resume listening for pulses.so my concerns are:the "real world" sending me 3.3v at the same time I'm configured as an outputbeing able to source enough current when I'm an outputbeing able to protect my input from "real world-ness" when I'm configured as an inputI feel like this has to have been addressed before for similar GPIO input/output switching, supplying more current when an output.is there a more clever, bidirectional/unity gain/protection circuit I can put line to take care of this? or... I've kinda stumped myself trying to think about this but I feel like there's a simple thing I'm missing.

author: SeanB
author_rank: Super Contributor
timestamp: June 01, 2013, 11:38:08 am
content: Simple answer is no. You need a pin to read the voltage, and another to do the driving at high voltage/current.

author: digsys
author_rank: Unknown
timestamp: June 01, 2013, 12:23:52 pm
content: It is OFTEN done, but you'll need to add a few components to do it properly. How many depends on your approach.It is often easier to select the "next size up" CPU, but I know that it isn't always an option.Basically you're setting up a DATA BUS. It may be 2-8 bits wide, depending on how many pins you are "short".You load outputs into a latch IC, it can be a simple D-Latch or something like a HC573 (as example). Once loaded you reconfigure theMPU bus as Inputs and Enable En controlled buffers. In a 4bit bus you have 4 IN and 4 OP sharing 4 pins, plus you need ~2 control pins.You can do it on a SINGLE IO pin, but you'll need to have more parts and set up a more complex P/W control system.It's a very common predicament to be in.

author: Paul Price
author_rank: Super Contributor
timestamp: June 01, 2013, 01:56:37 pm
content: Digsys:The mcu output has some sort of totem pole output stage. As an output, it can be damaged by attempting to sink a large current forced into the low logic state. On the other hand, the high logic state is internally current limited so overloads like a short circuit will result in the pin changing state or excess current at that pin but will not damage your mcu.I resolve this problem in my designs by connecting the inp/out pin to the cathode-anode midpoint connection of two schottky diodes connected in series, the top cathode connected to VDD, the bottom anode connect to VSS. Then feed the output pin into the anode of a third schottky diode paralleled by a 1k or so resistor. This should be more than ample protection in both in and out directions and I see you are only dealing with slow signals.If you are attempting to really idiot protect the MCU from sustained very high voltages, make sure your input pin has the above and also an added 5V zener (anode to VSS) to protect against any large current coming in from the outside world pumping up your VDD supply voltage.In most of my designs, a series resistor from the IO pin to the external world and use a single schottky diode connected to VDD at the IO pin has protected the mcu. If I expect High voltages, I use just a single resistor and no schottky diodes, just a 4.7V zener at the input pin connected to a resistor to the outside world.

author: digsys
author_rank: Unknown
timestamp: June 01, 2013, 02:24:45 pm
content: Quote from: Paul PriceThat's why you (3rd party) run it as a DATA BUS, with Buffered Outputs and Buffered Inputs. That way, worst case, you damage a MUCHsimpler to fix IC/Component, and the CPU is unaffected, and "could" detect such a fault. ALL the usual protection applies.

author: Rufus
author_rank: Super Contributor
timestamp: June 01, 2013, 02:56:01 pm
content: Quote from: bonanz on June 01, 2013, 07:12:00 amWhat you describe is too vague to give a good answer.If you need to signal bi-directionally over a wire and you don't have reliable knowledge of the direction at both ends it is usual to use an or-tie or open collector system where either end can drive the line low and one or more resistors pull the line high. You could also drive high and pull low.Protection of the I/O circuitry is more of a separate issue.

author: MasterOfNone
author_rank: Regular Contributor
timestamp: June 01, 2013, 03:37:08 pm
content: You could try a Bi directional Voltage level shifter (with auto direction detect) based on the TXB0108 Chip or similar. But I don’t know if they can source enough current for your needs. I came across these chips recently when I was interfacing an old 1980s micro computer to some 3.3v logic on a breadboard. Since I only needed to use it on the breadboard, I just purchased the Adafruit module.http://www.adafruit.com/products/395As I said I think the trouble with these chips could be sourcing enough current for your needs.

author: Paul Price
author_rank: Super Contributor
timestamp: June 01, 2013, 03:51:58 pm
content: Do you need both the output and the input going to the same connection point in the external world?Just what does the external world circuit look like?

author: bonanz
author_rank: Contributor
timestamp: June 01, 2013, 04:41:15 pm
content: Thanks for all the responses.I'll provide a little more description.I have an lpc1769 demo board which I was going to try to use as the mcu for this. according to its you can configure some IO pins for open drain mode. I have plenty of pins there. I'm not sure I understood "next size up" mcu. I guess my original post made it seem as if this was a pin limitation problem, but its not, its a me being a noob problem. If its better to put read/write on separate pins with some clever external circuitry that's definitely an option. I also picked up a psoc 4 pioneer board but have not messed with it at all yet. there might be some clever solution with its internally configurable stuff, but i dunno. sticking "classic" thinking for nowit is kinda like a command/status communication sytem with the pulses. the com/resp pulses however can actually be 2 pulse separated by a small space. it is very slow. so a command might be like 100ms followed by 100ms space, followed by 200ms pulse. The status from the "real world" could be similar. Or they may be a single pulse depending on the command/response. The commands are sent at 4 second intervals, so every 4 seconds a command pulse would be sent by mcu, the status is the same.The "real world" may be outputting a status at its own 4s interval when I connect to it or it may be not reporting a status. so when I connect to it I may want to start sending command x, and the command will repeat the pulse(s) at 4 seconds. but during the 4 second wait I need to listen for the response pulses which could come whenever and because everythings so slow I could connect while i'm getting a response. if I get a response in during a 4 second read period, I need to decipher the pulse widths and space (if there is one) to understand the responses.eventually I want to sync up the command responses so every 4 seconds i send a command, and in between at 2 seconds I should expect the response. but at the time of connection it is unknown if there is a response status or not. so I may just listen for 4 seconds and if I sense a response pulse, begin a timer until I send out my command pulses to sync up with status broadcasts i receive.because the pulses and in between time are so slow, I was thinking of just using timers to monitor the pin while waiting for a response to determine pulse/space widths and the same for generating my commands. so It makes sense from the mcu side to have an input pin that reads all the time and a transmit pin that fires out a command whenever, but they are both on the same line and I don't want to read my commands if they were tied together to muddle up my status deciphering.The other thing is the current sourcing for my pulses. I believe I may need to source up to 1A on my pulses possibly. I'm not exactly sure of this number but I'm going to put a 1A limit on it, maybe a fuse here for max protection. so thats why i'm a little concerned about when I connect up i don't want to connect up to an io pin configured as an output and receive a response pulse and sink a load of current and let the smoke out.Yes paul, the main thing thats causing me brain fail, is its the same connection point to the world. Its like a double ended sort of asynchronous pulse generator/decoder rugged system on a single pair of lines that could have sort of unrelialbe loading differences along the line where I need to source up to one amp, and protect myself from sinking to much current or spurious voltage values. the voltages from the response pulses coming from the world be 3.3v, but I'm not entirely sure I can trust them at this point.I was thinking of using a 74hc4052 or something like that while searching around to switch between my possibly high current driving pulses and reading...but i'm not sure if that's a good idea. i could probably kludge together some rube goldberg circuit to handle this all, but i'm trying to keep it simple, but my noob-ness is making that hard

author: Paul Price
author_rank: Super Contributor
timestamp: June 01, 2013, 04:57:24 pm
content: Thanks for taking the time to make an explanation, but you must consider, the MCU pin can only supply (sink/source) a few10's of mA of current.Why do you think you need to source about 1-Amp? Of course, it is possible to do this simply with more external circuitry.The 74HC4052 has many pins and analog switches, but it must be used as a low mA range low current switch.You should consider whether the MCU or the external circuit is to be the master or a slave in communication. You should decide who initiates a communication conversation session, then you always know when and what to do in response, MCU-wise.If you have plenty of unused pins available, the problem can be solved quite simply, I'm sure. Just tell me why you need 1-Amp to output to what kind of device (a motor, a person being tortured?)

author: bonanz
author_rank: Contributor
timestamp: June 01, 2013, 09:19:14 pm
content: I'm really not sure about about the 1A portion, I believe this is is the case because I think the other side is low impedance and can sink a lot of current and might. I'd like to make it work for up to 1A. basically i'm trying to simulate one of the this same device for the purposes of this messaging, but I'm not exactly sure of the details of the device other than the pulse messaging scheme and I'm pretty sure I've seen 1A output fuses and know power transistors are used on the output on similar devices that interface with this in the same way.