View Full Version : Electronics Newb question: Open Drain
09-12-2007, 04:36 AM
Currently I am reading through an electronics book, the propeller manual, and the PE lab files, more or less at the same time, all the while doing little tests on the propeller whenever I learn something new or want to try something. Haven't finished all of those books yet, suffice to say I'd like my next little experiment to not involve an LED http://forums.parallax.com/images/smilies/tongue.gif .
I figured that taking a reading from my GP2D02 infrared distance sensor should be sufficiently easy. For those who are not familiar with the GP2D02, it's really quite simple. The sensor is connected to a host microcontroller with two wires, one is input (Vin) and one is output (Vout). Vin is used to tell the sensor to begin a measurement, and then (after some delay) to clock the result from Vout one bit at a time.
My question is about a small-print footnote on the GP2D02 datasheet: "Vin terminal: Open drain drive input". I've heard of the term "open drain" before and have done some searching, but I can't really figure out what it means exactly, or what it implies when connecting the GP2D02 to a propeller. The examples I've seen, that deal with interfacing to the GP2D02 (using various host controllers), involve connecting Vin to the host output through a diode. In some examples, the explanation for this diode is that it limits the current to Vin (which operates at 3.3v). I think if the host operates at 5V, but I'm not sure if I remember correctly. In other examples, the diode is said to be present because Vin requires an "open collector". I'm not sure if I've seen an example that involved a 3.3v host (or even if this has anything to do with that diode).
So I'm a bit confused. All the more because, what little I know about diodes, and how it is connected in the examples I have seen (polarity-wise), would suggest to me that no current could even reach the input terminal of the sensor at all (or maybe that's the whole point?). Is there a simple way to explain what this open drain thing really means to me, and how a diode helps in this instance? Furthermore, would I need a diode to connect the GP2D02 Vin to my propeller as well? I could just use a diode and be done with it, were it not for the following two problems. I'm still working on my electronics inventory, and so I don't have any diodes at all, yet. Even if I did, I wouldn't know what kind of diode to use (or if it even matters).
Thanks in advance for any help.
Edit: By the way I attached the GP2D02 datasheet to this post, just in case.
P.S. I didn't see any GP2D02 objects in the propeller object exchange. I would be happy to upload mine when I finish it, if anyone is interested. Suggestions are welcome (method that returns a single measurement, or a dedicated cog, spin or assembly, etc... or all of the above).
Post Edited (_Mark) : 9/11/2007 9:41:06 PM GMT
09-12-2007, 04:50 AM
I am sure you will get many answers http://forums.parallax.com/images/smilies/smile.gif May be this will be also helpful content.honeywell.com/sensing/prodinfo/solidstate/technical/mr_chapter4.pdf (http://content.honeywell.com/sensing/prodinfo/solidstate/technical/mr_chapter4.pdf)
And forget those horror stories about diodes as soon as possible - please do!
09-12-2007, 05:04 AM
Heheh, probably a good thing that I don't know any horror stories about diodes (...yet!)
Thanks for the link. I found that file earlier today as well, it is linked to from the (english) wikipedia page about Open Collector. Some wikipedia browsing gave me a vague clue as to the difference between tri-state and open collector, but I just can't quite give it a good place in my head yet. Especially the implications when hooking up pins and wires and pins and diodes and pull-ups and more shiny pins.
09-12-2007, 05:21 AM
Well - again - forget those diodes!
When doing (micro-)electronics, you sooner or later have to understand where current comes from (no it's not the white storks...) and how an input and an output works in the first place.
If I should start it would take some time... But there is really a lot of information in the internet... Maybe you might like this: zone.ni.com/devzone/cda/tut/p/id/3544 (http://zone.ni.com/devzone/cda/tut/p/id/3544)
Alas, some good links I know of are German only...
09-12-2007, 07:42 AM
That's a great site, thanks a lot. Ok, so everytime I read about it, more pieces fall into place in my overwhelmed little brain. I can read German too, but not as easily as English. Still, if you think your German links could be helpful to me, I'd much appreciate it.
So thinking out loud, if I understand right, an open collector output line needs either a pull-up or a pull-down resistor, to drive the line (high or low resp.) when the output lets it float. Some types of open collector outputs have the two possible states, low and float (in which case a pull-up is needed). Other types have the two possible states high and float (in which case a pull-down is needed).
I have not yet seen any examples of the GP2D02 that involves an external resistor on the Vin line of the sensor, so I must assume that, either (1) the sensor has one already, or (2) none of the examples I've seen actually use open collector outputs to connect to Vin, hence the diode. I'm guessing it's the latter. On the GP2D02 datasheet I only see a (pull-up?) resistor on Vout (which tells me something). But nothing on Vin. Furthermore, I think most microcontrollers use tri-state outputs.
So, the examples I have seen probably don't involve open collector outputs, otherwise the diode wouldn't be necessary, right? And a pull-up or pull-down resistor would be. I'm guessing pull-up in this case, because I'm also guessing from the datasheet that Vout has one, and that line most likely comes from the same IC (or is this rubbish reasoning...). Besides, the timing chart on the datasheet suggests that the "default" state on Vin is high (i.e. the sensor does nothing until Vin is driven low, at which time it either starts a measurement or spits a bit out on Vout).
If I understand something else right, when configuring a microcontroller I/O pin to function as input (as on the propeller by clearing the appropriate dira bit), then that pin is "tri-stated" i.e. floating. I don't suppose it is possible to "fake" an open collector output with the propeller this way? To illustrate:
' Drive output low:
' Let output float (resistor pulls it high):
Of course then I'd need to add the pull-up and lose the diode (and be damn sure no other cogs interfere with that pin). If it would work at all... But resistors I have, and diodes I don't http://forums.parallax.com/images/smilies/tongue.gif
P.S. Everybody knows that storks don't bring current. They bring bugs.
Post Edited (_Mark) : 9/12/2007 1:14:59 AM GMT
09-13-2007, 12:32 AM
(a) Indeed it is possible to "fake" an OC in exactly the way you describe it. You can even leave the OUTA down and just toggle the DIRA.
OC is used more often than commonly thought, as it is:
- an absolutely acceptable bus technique ("wired or")
- very handy for connecting to a different voltage. Drivers are very often OC.
(b) I recommended to study the different outputs of integrated curcuits.
(1) The most simple one is the collector (or "drain") circuit: You draw off your voltage at the collector resistor. When you just leave out this resistor - to be added externally - you have an OC circuit, which - as already remarked - is very flexible.
(2) The most annoying draw-back of collector circuits is that a mass of current flows, even without any load whatsoever This can be avoided by combining a PNP and NPN transistor (or equivalent FETs - then called CMOS) to form a complementary output sometimes called totem-pole .
Current only flows during signal switches (without load, i.e.). This is the output of the classical TTL chips and their CMOS successors
(3) Tri-state (or simply: TS) is just an extension if this, allowing to "break" the output - forming the "third state". As this is not done by a mechanical switch of course, the break is not a total one.
The draw back of tri-state output is that it needs an additional gate, adding to the switching time and to cost, especially when higher current is at stake.
Hope this helped you sorting things out!
Post Edited (deSilva) : 9/12/2007 5:37:02 PM GMT
09-13-2007, 05:42 AM
Yes, at least I think it has brought me again a bit closer to understanding this particular topic. I found out about that totem pole thing (push/pull output) yesterday while browsing wikipedia. It also explained how the push/pull relates to a tri-state. How it relates to a collector wasn't clear to me until I read your post (even though it's still a bit vague to me how exactly those two transistors in the totem pole work, but that's alright for now).
But apparently I'm not there yet http://forums.parallax.com/images/smilies/tongue.gif I tried to make it work this evening according to the "fake OC" setup. Unfortunately, it didn't. After driving Vin low, my program waits for Vout to go high in response, which never seems to happen (should take around 70ms). When I took out my cheap little multimeter, it turned out that Vin never gets below approximately 1.8 or 1.9 volts. My guess is that this isn't low enough for the sensor to see it as a low, which would be the reason that it doesn't respond. And when I let Vin float, it reads a little over 3.6v, even though the Vdd output of the board I'm using (Franz's EasyProp), to which the pull-up is connected, gives me a nice 3.33v. I don't know if this is to be expected.
I tried different pull-up values on Vin. I started with 12K (or actually 10K and 2x 1K, in series) because that's what the sensor has on its Vout, according to the datasheet. Then I tried 3x 4.7K in series, then a 22K, then a 100K, then a 10K. It didn't seem to make any difference. I clearly don't really know what I'm doing here.
By the way, what seemed weird to me, is that my program gets "unstuck" as soon as I poke even a single multimeter probe either to ground, or to Vin. I placed the probes where the ground and Vin wires from the breadboard stick into the female header on the EasyProp, to determine the voltage on Vin. I hope that was the right way to do it. Apparently, it caused Vout to briefly go high, after which the program clocks out 8 bits from the sensor (and always receives zeroes). Then the program loops to get a new sensor reading, and gets stuck again as before, even with the multimeter probes still making contact.
Am I seriously messing things up here? Perhaps I should try something else first and come back to the GP2D02 another time.
Just in case it is of interest, here's my code (by the way, the first run of this program was without the Debug statements in GetMeasurement, which I added later to see what went wrong):
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x
GP2D02clk = 14
GP2D02dat = 15
PUB Main | lTemp
' Paranoid code
Debug.start(30) ' Start Ariba's PropTerminal object (param is not used...)
lTemp := GetMeasurement(GP2D02clk, GP2D02dat)
PUB GetMeasurement(aPinClk, aPinDat): lResult | lTemp
dira[aPinClk]~~ ' Switch clock pin to output to drive Vin low
lResult := 0
lTemp := |<aPinDat
waitcnt(clkfreq / 1000 + cnt) ' Short pause before we start monitoring Vout
Debug.str(string("Waiting for signal on pin "))
waitpeq(lTemp, lTemp, 0) ' Wait for Vout to go high
dira[aPinClk]~ ' Switch back to input, Vin will be pulled high
' Start clocking out the bits. Minimum wait between transitions is said to be
' 2 microseconds... I guess we'd be safe even if this were assembly. See here
' for example code: http://www.seattlerobotics.org/encoder/200112/gp2d02.html
dira[aPinClk]~~ ' Drive Vin low
lResult := (lResult << 1) | ina[aPinDat] ' Take a bit
dira[aPinClk]~ ' Pull Vin high
waitcnt(clkfreq / 666 + cnt) ' Enforce minimum 1.5ms wait before going again
It's maybe worth mentioning that I got this thing to work a few years ago, with a different microcontroller. This was a Freescale ColdFire, successor of the Motorola 68000 series. I probably just borrowed a diode from someone to hook it up, but I can't really remember. I haven't done much else with it (or any other microcontroller, until now), because I realized I was taking on too much at once.
Any feedback would be welcome, even if it means I should first study more on electronics before trying this particular sensor again.
Post Edited (_Mark) : 9/12/2007 11:23:08 PM GMT
09-13-2007, 12:17 PM
I just get the shrewd suspicion you are running the GP2D02 from 5V!
If that is the case, then please connect the Pull-up to 5V. In fact that is what this is all about!
09-13-2007, 05:11 PM
Indeed I am running it from 5V. This weird device requires 5V to operate (it has an internal regulator), but Vin should never exceed 3.3v. I think that means I should not pull it up to 5v. How would that help when attempting to drive Vin low, even?
Correction: I double checked, and the "Absolute Maximum Ratings" table on the datasheet says Vin should not exceed 3v. I suppose this means the 3.6v it was getting yesterday, or even 3.3v, would be too much... Although, come to think of it, I don't recall having seen any examples that take any measures towards this, other than the diode. And neither did I when I got it working some years ago. The Brainstem controller board even has a port specifically for this sensor which already has the diode, and I believe it operates at 3.3v too.
Is there something I should know about that diode? http://forums.parallax.com/images/smilies/tongue.gif Does this all mean the "fake OC" setup won't work in this case?
Post Edited (_Mark) : 9/13/2007 10:27:02 AM GMT
Beau Schwabe (Parallax)
09-13-2007, 09:53 PM
Look at this document under ... "Interfacing Issues"
"Since the internal logic of these detectors runs at ~3 volts, it is necessary to protect the clock input from being driven above this voltage. This can be done with a resistor network to divide the voltage but the desired method is to use a small signal diode such as the 1N4148 high speed switching diode. This diode is connected in such a way as to only allow current to flow with a low logic level input. The internal circuitry of these detectors pulls the input high otherwise."
Beau Schwabe (mailto:email@example.com)
IC Layout Engineer
09-14-2007, 01:35 AM
o.k. I missed the 3V restriction...
Nevertheless, it should run WITHOUT this diode, just by driving the Propeller pin either low or floating.
I understand this somewhat irritating "open collector" remark now in so far, that the chip has an internal pullup (but to which voltage?)
However an external pull-up (of say 10 k) will do no harm, either connected to 5V or to 3V3.
I was irritated about Mark's remark he meassures 1V9 at the clock line when driven low. This does not seem possible, except he was doing an "integral" reading, adding 3.3 and 0 to this value....
09-14-2007, 04:52 AM
Thanks a lot for that link, Beau. I used to visit that site every now and then a few years ago, but I don't think I've bumped into that page before. I will certainly read it. From the portion that you quoted, do I understand correctly that the sensor already has a pull-up attached to Vin? Because then they failed to show this on the datasheet. In that case, perhaps the "fake OC" setup described above will work if I remove my external pull-up (though it's still not clear to me why my pull-up pulls it up too much, so to speak, and why the internal pull-up of the sensor does not). I do finally understand now, why the solution with the diode works (it didn't occur to me before that the current does not necessarily flow from the propeller output to the sensor input).
deSilva, I wasn't sure if I measured the voltage in a correct way, but I also don't know how else I should do that here. Thanks a million for your time and effort so far. Hopefully my troubles with this will be over soon, as I managed to get my hands on a couple of diodes, so if all else fails I could always just stick with that http://forums.parallax.com/images/smilies/tongue.gif I'm not at home right now though, so I'll have to test it later.
Still I find it interesting to learn how this kind of stuff works. I just sat down with someone who explained me some more about different kinds of outputs, how the transistors function in each of them, etc. One other suggestion I got as to connecting the sensor, was to actually create an open collector output and place it between the propeller and the sensor, as follows:
[ ] 10k
Prop ----- BC547
It also acts as an inverter, so to output a logical high to the sensor, the propeller should output a zero. Anyway, I'll probably continue reading and testing tonight or tomorrow, will let you know how it turns out.
(edit... for some reason the code tags and smileys are not working for this post... I'll see if I can fix it later, right now I gotta scoot)
(edit again... fixed)
Post Edited (_Mark) : 9/14/2007 3:12:35 PM GMT
Beau Schwabe (Parallax)
09-14-2007, 05:18 AM
The way I read the datasheet is that the 3V is internal.· Now it seems from several sources that it is strictly 3V.... If that is truly the case, an external pull-up connected to 3.3 V is going to deliver 300mV above what it needs.· Depending on how sensitive the internal IC design is I/O protection diodes or not, a situation where an I/O pin is greater or less than the voltage supply rails can cause a condition known as latch up.· Even though the IC supply is 5V, it states that there is an internal regulator running at 3V. <--That as far as the I/O is concerned with regard to Vin would be the supply rail.
Basically all you need to do "from the datasheet" is figure out a way to drive the pin low.· You don't need a diode in this case; simply make the pin an INPUT for an implemented HIGH signal and an OUTPUT (driven LOW) for a LOW signal.· The internal pull-up to 3V or whatever it is set at in the IC will do the rest.
Beau Schwabe (mailto:firstname.lastname@example.org)
IC Layout Engineer
09-14-2007, 11:00 PM
Alright, I tried the new fake OC approach (without the external pull-up, as Beau also suggested), as well as the diode approach (incl. changing the code to keep dira set as output and use outa for outputting highs and lows). Neither method works, my code keeps getting stuck waiting for a response from the sensor on Vout. Is it possible that I irrevocably damaged the sensor during previous tests by pulling Vin up to 3.3v (thereby exceeding the apparently strict rating of 3v)?
I'm finding that I can't think of a way to diagnose it that makes sense. As I was told I didn't use the multimeter correctly last time, and I don't have a scope, and viewport isn't going to tell me anything new in this case. As far as I can tell, having switched off all DIL switches on the EasyProp, the pins I use (14 = Vin, 15 = Vout) are not connected to anything else on the board such as pull-ups (contrary to a bunch of other pins). What else could I try, or look for?
I'm hoping it can still be made to work, of course, but my gut tells me I broke the sensor. It should have worked at least with the ubiquitous diode approach.
Beau Schwabe (Parallax)
09-15-2007, 12:57 AM
If the device is regulating and "pulling-up" at 3V as it is supposed to, then you should be able to measure at least 3V on Vin without anything else but your meter connected to it.
Connect one terminal of your meter to Vin, and the other terminal to GND and report back what you get.· Perhaps someone else who has one of these chips (I do not) can verify this test and report their reading as well.
The next question would be, what does your circuit look like? can you provide a schematic?· Are the ground connections shared between the Prop and the GP2D02?
Beau Schwabe (mailto:email@example.com)
IC Layout Engineer
Post Edited (Beau Schwabe (Parallax)) : 9/14/2007 6:03:24 PM GMT
09-15-2007, 04:09 AM
With nothing connected, except the sensor's Vcc (+5v) and GND, to the corresponding pins on the EasyProp (which is powered by the usb port), then using the multimeter probes at the dangling Vin wire, and its GND wire respectively, I get a reading of either 3.72v, or 3.62v... I've taken the exact same measurement in the exact same conditions a couple of times, having the usb cable disconnected from the EasyProp in between measurements (and of course reconnecting it before measuring again). Sometimes it reads 3.62v and other times it reads 3.72v. I can't explain this. The reading appears to be consistent as long as the usb cable remains connected.
In either case, the propeller itself is inactive (i.e. has run past the end of the short test program that runs from eeprom when power is first applied), and nothing else is connected to it. I also measured Vout, though only once, and it read 0.57v. Don't know what that could mean.
In case it's of significance, when measuring across the EasyProp's +5v output and GND, I read 4.85v. I have no idea why Vin reads way too high when the supply voltage is even below 5v. I'm guessing it should be able to work with 4.85v, though that's obviously not an "expert opinion". I'm also guessing that, if 4.85v would not be enough for the internal regulator to produce 3v, then I should not be reading up to 3.72v on Vin... Unless the regulator is fried? Don't see how that would have happened, though.
Here's an ascii-art diagram of my current setup (by the way, I see lots of schematics being attached to posts on this forum, and I was wondering, what would be a useful tool for drawing those, if not MS Paint..?)
------------- (sensor casing connected to ground)
----- | ------
| GND |----------+----------| GND | (pin 1)
| | | |
| +5v |---------------------| Vcc | (pin 3)
| | | |
| P15 |---------------------| Vout | (pin 4)
| | | |
| P14 |---------|<----------| Vin | (pin 2)
----- 1N4148 ------
On a side note (probably not relevant to the problem, but I'm sure you're wondering), shortly before the first time I used the GP2D02 a few years ago, I remember reading somewhere that the sensor's casing actually conducts, and that distance measurements are significantly less noisy when connecting the casing to ground. I have found this to be true.
The 4 wires of the sensor are soldered to a pair of pin headers (with two pins each) that plug into a breadboard. One header for Vcc and GND, and one for Vin and Vout. All connections between the breadboard and the EasyProp are made using jump wires, which plug into the female header on the EasyProp. Via the breadboard, the sensor's Vcc and GND are connected to the EasyProp's +5v and GND on that female header. Likewise, via the breadboard, Vin connects to P14 and Vout to P15 (both of which I unplugged before measuring the voltage accross Vin and GND, or accross Vout and GND). The 1N4148 diode sits on the breadboard between the sensor's Vin wire, and the wire that connects to P14. The black marker on the diode is facing P14.
(edited to avoid blatantly bumping this topic with a new post - but I noticed just now that the above ascii-art graphic looks as I intended when viewing from windows, but not from linux (using firefox in both cases). It may just be me, I just thought I'd point it out, in case anyone is still looking. Just copy/paste into any monospace font text editor to clear it up)
Post Edited (_Mark) : 9/19/2007 2:24:16 AM GMT