Open Drain

He wants to use it as a DAC 0-9Volt. So single PNP will not help too much, as the arduino is 0-5V (@base), and the emitter will be @9V, so the PNP will be permanently open with @0-5V on the base.
NPN will not work as well.
You need a "high voltage high side switch" discussed many times here. So an NPN low-side driving PNP at high side (so in total 5-6 resistors, 2 transistors, 1 capacitor) :slight_smile:
The PWM integrator values is an example only - use values as you need. No warranties of any kind..

dac.jpg

tarlex:
Hello every one,

Firefox:
To put an Arduino Input/Output into Open Drain mode I assume I do the following.

I still not sure the right way to turn the pin as open drain.

I want to use a digital pin as PWM generator to control a DC voltage from 0 to 9V as it follows.

Thank you for your time and help

The hardware PWM only drives HIGH/LOW, so doesn't directly support open-drain, but there are
some cunning ways round it:

  1. software PWM - then you can use pinMode() to control the pin (or direct port manipulation).

  2. configure one pin as PWM, but connect the circuit to a different pin. Use a pin-change interrupt
    routine masked to the PWM pin to drive the second pin in an open-drain manner...

Yes.

With caveats, eg. See this video https://www.youtube.com/watch?v=CFt8hkh17_w starting at time 16:15 for a guy who was caught out by believing that... :slight_smile:

tarlex:
I still not sure the right way to turn the pin as open drain.

Can't you just make the pin INPUT/LOW (which is high impedance) or OUTPUT/LOW which is forcing the line low? Then use a pull-up resistor.

So basically toggling INPUT/OUTPUT modes is what you use for signalling. You can't directly PWM like this (you could do it via an interrupt).

I want to use a digital pin as PWM generator to control a DC voltage from 0 to 9V as it follows.

I don't see how open drain comes into this desire.

fungus:

[quote author=Nick Gammon link=topic=146785.msg1103014#msg1103014 date=1359938264]
Can't he just use INPUT/LOW as high impedance, and switch to OUTPUT/LOW to sink current?

[/quote]

Oh, wait, I already suggested that. Ooops. I thought some other guy must have said it, until I saw who made the post. :wink:

fungus:
With caveats, eg. See this video https://www.youtube.com/watch?v=CFt8hkh17_w starting at time 16:15 for a guy who was caught out by believing that... :slight_smile:

Yes but in his case he was trying to use the high-impedance mode to float up to 15V. Personally I wouldn't have attempted that because having 15V on the input pin would be out of spec, plus he discovered the protection diode on the pin.

... from 0 to 9V ...

Oops, I misread that bit too. Not a good day for reading comprehension. :slight_smile:

In that case I doubt you can do it (the open drain part) particularly easily. But I still don't see why you need open drain in this case. Can't you just pulse a MOSFET? It doesn't need to go open drain.

You can use a MOSFET as a bi-directional logic-level converter as described here:

http://ics.nxp.com/support/documents/interface/pdf/an97055.pdf?

To quote from the start of that:

For bi-directional bus systems as e.g. the I2C-bus, such a level shifter must be bi-directional, without the need of a direction control signal. With only one appropriate MOS-FET for each bus line the desired level shifting with automatic direction control can be done. The levels may have a vast range, depending on the used MOS-FET, e.g. down to 2 Volt at the “Lower voltage” side and up to 10 Volt or more at the “Higher voltage” side of the level shifter.

Note that the circuit described works with I2C (and thus works with open-drain) and also can work up to 10V.

Firefox:
They (the electronics world) call it an Output!! but if you google Open Drain the explanation is that it can only Sink current or present a High Impedance so it cannot output anything. :slight_smile: The Output definition is very confusing.

I think the confusion comes with equating the term "High Impedance" with the term "Input". The problem comes from making the term "High Impedance" a metaphor for Input. Yes, an MCU input typically has a high impedance (relative to a typical output) (FYI: there are low impedance inputs, such as a 50 ohm RF input, but that rarely applies to an MCU -- in fact, I can't think of a case). But that doesn't mean that ONLY inputs can present a high impedance.

A common drain (or common collector) output is merely an output that only "pulls low". In other words, when low, it has a low impedance, and when high it has a high impedance. The reason is simply that the output is literally the drain (or collector) of a single transistor. When the transistor turns on, the output presents a low impedance to ground. When the transistor is off, the output is a very high impedance, also to ground. One way of looking at it is this: a transistor, when used as a switch, is like a variable resistor that has two values: very low, and very high. Because the resistor is always connected between ground and the output, that resistance is always to ground, no matter if it is in its high resistance state or its low resistance state.

Why do such a thing? Mainly to translate voltage levels. The transistor drain (or collector) is tied to a different voltage than the voltage the MCU is running at. This can be used to convert, for instance, what would otherwise be a 5V output, to a 3.3V output. This is done by running the MCU at 5V, but tying the drain (or collector) output to 3.3V through a resistor.

Let's say we use a 2.2k resistor. When the transistor turns off, the output will have a very high impedance to ground, and a 2.2k impedance to 3.3V. The 2.2k resistor is, essentially, in parallel with the very high impedance to ground (via the very low AC impedance of the power source), thus the total impedance will, for all practical purposes, be 2.2k. When the transistor turns on, the output impedance is very low to ground, thus the output will be 0V, or nearly 0V. Once again, because this very low impedance is in parallel with the 2.2k resistor. Nearly 0 ohms paralleled with 2.2k will be no more than nearly 0 ohms, because the total resistance of resistors in parallel is never larger than the lowest resistance value of the two.

Another application is to translate to a higher voltage. For this to work, the MAX drain to source (or collector to emitter) voltage must be as high or higher than the voltage being translated to (and probably should be at least 1.5 times the voltage being translated to, for immunity to spikes and surges and such). Here's an example of this: MCU voltage: 3.3V. Voltage we want to translate to: 5V. Open drain output tied to 5V through a 1k resistor.

Another example: Say you want to turn on and off a series string of LEDs using an MCU running at 3.3V. A regular output would drive from around 0 volts, to around 3.3V (when an output must sink or source appreciable current, typically there is some voltage lost in the driving transistor, thus an output driven low, might be more like 0.4V and an output driven high, might be more like 2.8V--depending, of course, on how much current is flowing through the drain-source junction (or collector-emitter junction)). If our string has 4 LEDs and each LED drops around 3.2V, that adds up to 12.8 volts! An output that can only go as high as 3.3V will never be able to turn on those LEDs. But, lets say you have an MCU with outputs that can be configured as open drain, and suppose that output transistor can handle up to, say 20V. We could, simply, tie the cathode side of that string of LEDs to the open drain output, and, through an appropriate resistor, tie the other end of the string to the positive end of a 16V supply. Now, when the output transistor turns on, it connects the cathode end of that string to ground, and it lights up. When the output "goes high" (i.e. the transistor turns off), the turned off transistor presents a hugely high impedance to the cathode end of the LEDs, thus no current flows through the LEDs and they go dark!

FYI: when I say "through an um-tee-um k resistor" what I mean is, one end of the resistor is connected to the open drain output, and the other end it tied to the voltage being translated to. The output is, then, of course, taken from the side of the resistor that connects to the open drain output.

FYI2: It's an "output" because, for one, nothing is inputted to the MCU, and two, it is either driving the load to ground, or allowing the load to float--in other words, the load, on the output, is manipulated.

FYI3: technically, you could get away with driving that LED string with an open-drain output with a MAX drain to source voltage lower than the voltage used to drive the LEDs. Why? Because, the LED forward voltage will protect the MCU output from an over-voltage condition. Say the minimum possible full current forward voltage for these LEDs is 3.0V (as stipulated in the Datasheet). The minimum total voltage would be 4 x 3.0 = 12.0V. If using a 16V supply to drive those LEDs, the highest (under load) voltage the MCU output will ever see is: 16V -12V = 4V. So, technically, you could get away with using an MCU with output transistors capable of handling up to 5V. BUT, be dang sure the power supply will always be a rock solid 16V and if one of the LEDs shorts out, you don't mind a catastrophic failure in the MCU. Bottom line, such practices can cut costs, but are risky!

tarlex:
Thank you.

Not even messing around with atmel's internal registers?? I know there are other uControllers that allow to set a PWM output as open drain.

Do you mean by buffering something like this?

Cheers

Sorry, that won't produce the desired result.

The voltage across the capacitor, in this circuit, will never go higher than 5V (probably no higher than around 4.6V). See below for a proper circuit. If the PWM frequency is much higher than, say 5kHz, then a 100 to 330 pF capacitor across R1 should produce a better result. The component values are merely suggestions (Fritzing doesn't allow no values).

tarlex:
Hello every one,

I still not sure the right way to turn the pin as open drain.

I want to use a digital pin as PWM generator to control a DC voltage from 0 to 9V as it follows.

Thank you for your time and help

It's probably not advisable to expose an open-drain output to a voltage higher than 6V. I could find no specific specification in the Atmega328 datasheet (the UNO MCU), but the Absolute Maximum operating voltage is 6V, so from that one might infer that VsdMax is also 6V. In other words, when the output transistor (that functions as the open-drain output) is turned off, it is only defined as "off" for voltages at or below 6V (and above -0.5V). Voltages higher than that might destroy the transistor.

So, I would suggest you do it this way:

Note: the values are merely suggestions (Fritzing doesn't allow absent values). The values that will provide proper functionality are frequency dependent. Also, you might need to lower the value of R1 and even put a 100 to 470pF capacitor across it.

Though C1 = 10uF would give a much smoother output from standard Arduino PWM rates which are quite low
(500Hz and 1kHz approx depending on pin).

The ATmega328 has no open-drain outputs, all pins(*) have protection diodes and are
push-pull. You can pretend they are open-drain (if the output circuit doesn't go above
Vcc), but try to pull any pin above Vcc and you'll find they are not open drain.

(*) not true for RESET.