Reading and writing to the same Arduino Pin

Hello All,

I'm currently working on a temperature controller using Arduino and Ds18B20. The Temperature sensor is connected to Digital Pin 7 and I have running using the example codes. Everything seems to be working fine, so far. But I'm currently confused with a question here.

I'm trying to turn on a 12v cooling fan connected to Digital Pin 8 if the temperature shoots up above 30 Deg C. If the temperature falls below 30 Deg C the fan would shut off (Digital Pin 8 is low). What I want to do is check if the Digital pin is High and keep it high till the temperature falls below the set temperature.

Is it possible to use digital read and digital write on the same pin within in loop statement ? A snap shot of the code im currently using is below:

if (millis()- previousMilliTempCheck >= 120000 ){
      if ( sensors.getTempCByIndex(2)> maximumTemperature){
        digitalWrite(8, HIGH); // want to check here if the pin is high then keep it high
         lcd.setCursor(0,0);
          lcd.print("Fan On");}

Any suggestions and guidance would be helpful.

Thanks Ritzy

Is it possible to use digital read and digital write on the same pin within in loop statement ? A snap shot of the code im currently using is below:

No. You can do a digitalRead on pin-8 using another pin but you can't use pin-8 to test if pin-8 is high. First of all because it is either an OUTPUT AND HIGH/LOW or it is INPUT and can be used to test ANOTHER pin . Second, from a programming standpoint, this question would never be asked because if you are interested in the STATE of pin-8 then you would create a boolean flag (google "arduino Boolean") called STATE8 and set it to TRUE immediate after you set pin 8 to HIGH, and set to FALSE immediately after setting pin 8 to LOW. Then you simply say if (STATE8) //If STATE8=TRUE If you wanted test it for FALSE you would If (! =STATE8) see example below:

  True if the operand is false, e.g.
if (!x)
    { 
        // ...
    }

Furthermore, your approach suggests you're waiting till the last second to take action in the case of a temperature increase. The standard approach would be to test the temp to see if it is increasing, not just if it equals some maximum. so at time T1, test temp. T2 test temp (falling/same/increasing) [you could have a boolean flag for each if you wanted] T3 test temp (increasing ? ===> set increasing flag ( TRISING = TRUE) // set TRISING flag) T4 test temp (increasing?====>Elapsed time (based on millis() since temp STARTED rising ? T5 test temp (increasing? ====> If temp rising longer than x milliseconds , then ==> fan ON. T6 test temp (decreasing ? ===> set TFALLING flag

TRISING = FALSE;
TFALLING = TRUE;

do the same thing as above for falling, but use the elapsed time to decide when to turn fan off.

This should prevent it from ever getting to 30 degrees.

What I want to do is check if the Digital pin is High and keep it high till the temperature falls below the set temperature.

Why do you need to test the state of the pin ? You set its state so you know what it is already. It will not go LOW unless/until your program sets it LOW.

Second, you have not explained what you why or how you want to modify it.

@UKHeliBob, Yeah, I tried to explain that to the OP in the previous post. What he is really asking is how to control the fan based on temp and he thought he should be checking the fan control pin but I explained it is easier to use flags for decision making.

You can digitalRead a pin you're also using for digitalWrite but generally the consensus is that you shouldn't need to. If you need to keep track of the state of the pin, you can use flags as mentioned above.

Often in a temperature control scenario though, you turn on a heater when it's too cold and turn it off when it's too hot. In a cold situation, if you set a pin high to turn on your heater on every iteration of loop, no harm done if it was high already so you really may not care whether the heater is currently on, just that it's too cold so you ensure that it is on.