How can I digitalRead a pin that is in pinMode OUTPUT?

后端 未结 11 1080
不知归路
不知归路 2020-12-09 02:30

I have a very simple test sketch in which I\'m trying to set a pin to HIGH and then read its state with digitalRead. Here is my sketch.

<         


        
11条回答
  •  心在旅途
    2020-12-09 03:16

    Are you trying to set the default input to HIGH?
    If so you are looking to activate the pull-up register:

    void setup() 
    {
        Serial.begin(9600);
    }
    
    void loop() 
    {
        delay(1000);
    
        pinMode(3,INPUT);         // default mode is INPUT  
        digitalWrite(3, HIGH);    // Turn on the internal pull-up resistor, default state is HIGH  
    
        delay(1000);
        Serial.println(digitalRead(3));
    }
    

    Excerpt from DigitalWrite:

    If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor.

提交回复
热议问题