pinMode(0, OUTPUT_OPENDRAIN);

Status
Not open for further replies.

Frank B

Senior Member
Hi,

it may be obvious for some or most of you.. but it wasn't for me:

You can use a pin, if it is in "Open Drain" mode (aka "open collector") as in- and output at the same time.

Code:
void setup() {
 pinMode(0, OUTPUT_OPENDRAIN);
 digitalWriteFast(0,1);
}

void loop() {
 Serial.println(digitalReadFast(0));
 delay(250);
}
To test ist, connect a resistor, some kOhm (I tried it with 1K) with PIN 0, and the other end with GND or 3.3Volt.
You'll see the "0" and "1" in the serial monitor.

Not, of course, if you digitalWriteFast(0,0);
Perhaps this info is useful for others too (who did not know this, like me).

This is useful for some buses.
 
Last edited:
You can also switch between Digital Output and Analog Input along with setting the pins to High Z.

Sample code from some ILI9341 resistive touch testing I did. I need to dig around and find the full project;/
Code:
//Touch Detect
      pinMode(aYp, INPUT);
      pinMode(aXn, INPUT);
      pinMode(dYn, OUTPUT);
      pinMode(dXp, OUTPUT);
      digitalWriteFast(dYn, HIGH);
      digitalWriteFast(dXp, LOW);
//      delayMicroseconds(1);   //let things settle
      z1 = adc->analogRead(aXn, ADC_0); 
      z2 = adc->analogRead(aYp, ADC_0);
      
      digitalWriteFast(dYn, LOW);
  //Read X pos first
      pinMode(aYn, INPUT);   //reading this pin
      pinMode(dYp, INPUT);   //tristate this pin or atleast hope it goes high impedence
      pinMode(dXp, OUTPUT);
      pinMode(dXn, OUTPUT);
      digitalWriteFast(dXn, LOW);      
      digitalWriteFast(dXp, HIGH);  
//      delayMicroseconds(1);   //let things settle
      posx = adc->analogRead(aYn, ADC_0);  
      
      digitalWriteFast(dXp, LOW);
       
   //Barrowed from Adafruit TouchScreen    
    uint32_t rtouch;   //use float for higher accuracy, watch out for overrun when using 12bit values
    rtouch = z2;
    rtouch /= z1;
    rtouch -= 1;
    rtouch *= posx;
    rtouch *= 298;   //x resistance
    rtouch /= 4096;
      
      
      
      
  //Read y pos next

      pinMode(aXn, INPUT);   //reading this pin
      pinMode(dXp, INPUT);   
      pinMode(dYp, OUTPUT);
      pinMode(dYn, OUTPUT);
      digitalWriteFast(dYn, LOW);      
      digitalWriteFast(dYp, HIGH);  
//      delayMicroseconds(1);   //let things settle
      posy = adc->analogRead(aXn, ADC_0);   
      
      digitalWriteFast(dYp, LOW);
      
      touchtime = 0;
     
 Serial.print(posx,DEC);
 Serial.print("  posx  ");
 Serial.print(posy,DEC);
 Serial.print(" posy ");
 Serial.print(rtouch,DEC); 
 Serial.println(" rtouch ");      
    
}
 
FWIW, ye old (2012) maple leaflabs Wire I2C library originally bit-banged SCL/SDA (software I2C) utilizing maple's (STM32F103RET6) OPEN DRAIN pinMode. Just because I could, it was easy to port that library to do software I2C on teensy OPEN DRAIN pins with pullup resistors.
 
Status
Not open for further replies.
Back
Top