Go Down

Topic: emulate open collector (Read 3 times) previous topic - next topic


With the need to drive some electronics with open collector / open drain outputs from Arduino, I just realized that it may just be possible to emulate open collectors with a bit of smart programming. Maybe I'm not the first to come up with this idead, maybe someone has tried it before and has good or bad experience with this method.

The idea is to use the ATmega's internal pull up resistor when in HIGH state as follows:
- To emulate an open collecter / open drain output LOW:
=> pinMode( n , OUTPUT ); digitalWrite( n , LOW );

- To emulate an open collector / open drain output HIGH:
=> pinMode( n , INPUT ); digitalWrite( n , HIGH );

If this works, it'd save an external resistor, transistor and the programmed logic does not need to be inverted.

Thoughts anyone? Anyone tried it before? Caveats? ...


That's how the I2C bus works, but you don't need to set the output HIGH if using external pull-up resistors:

Code: [Select]

void setup ()
  digitalWrite (pin, LOW) ;

void loop ()
  pinMode (pin, OUTPUT) ; // drive pin low
  pinMode (pin, INPUT) ; // hi-Z state
[ I won't respond to messages, use the forum please ]


That's a smart option too!

But I was hoping to reduce the number of external components as much as possible because I haven't got a PCB, ... The only thing I'm worried about with my option is if the slopes will stay well defined during the time that the pin is switched from output to input and ........ enabling the pull up. That'd be solved with yours.



Direct port manipulation would mean changing pin direction and output within a fraction of a us of each other, rather than the several us that pinMode() and digitalWrite() take to execute.

Also the builtin pull-ups are "between 20k and 50k" which is rather weak for some uses (long I2C or OneWire buses for instance).  I2C recommends 4k7.
[ I won't respond to messages, use the forum please ]


The weak resistance is a good point, didn't realize the internal pull up's are that large.

My proof of concept runs on an Arduino Mega, but I eventually need it for an ATtiny, so I'm fiddling with direct registers anyway :)

Go Up