Unconnected digital pins as sensors

Hi,

I am using Arduino NANOs as controllers in model railroading and using unconnected digital pins to write and read states of phototransistors for detection.

My question is do I need to terminate these pins high or low with a resistor (<10K)? OR just use PULLUP as these pins are defined as OUTPUT.

My sketches are fairly long:

  • Basically phototransistors are under the track and when a train blocks the light, a value of LOW will be set on a digital pin.
  • Later I check for the state of that pin and activate the servos and flashing LEDs for that crossing. -When the train reaches the other end of the crossing a similar action is taken and the state of the phototransistor is written to another pin, and read to deactivate the circuit.

BTW, I am using this method, because I tried to just define a variable (ie: SENSOR1) and write and read to/from it, but was unsuccessful at getting it to work. I am new at writing C or any programs but understand how they work. So there may be a better way, that I do not know.

Thanks for any help, and can provide pieces or the entire code if needed.
JAXBRG

I am using Arduino NANOs as controllers in model railroading and using unconnected digital pins to write and read states of phototransistors for detection.

It's quite useless to put this functionality to unconnected pins, at least if we agree on the term "unconnected" to mean "is has not connection to anything other than the processor".

My question is do I need to terminate these pins high or low with a resistor (<10K)? OR just use PULLUP as these pins are defined as OUTPUT.

If they are defined as outputs they have a defined state and need no pull-ups.

Thanks for the reply. I would also prefer not to have to define output pins (especially unconnected ones) as points to write and read detector states. Perhaps I should have asked why I could not successfully create a variable that could be used to store the state of the detector and then read from it, or branch on it.

I may be asking in the wrong question. your reply has helped to clarify that.

Thanks

JAXBRG:
Hi,

I am using Arduino NANOs as controllers in model railroading and using unconnected digital pins to write and read states of phototransistors for detection.

My question is do I need to terminate these pins high or low with a resistor (<10K)? OR just use PULLUP as these pins are defined as OUTPUT.

How can you write the state of a phototransistor?

How can you use INPUT_PULLUP on an OUTPUT pin?

I don't understand a lot of your posting, and there's not circuit diagram to clarify things... Post your
circuit and your code if you want meaningful answers please...

I realize that I need to clarify and provide more info. I will do a circuit diagram and provide the code.... tomorrow.

I am not writing to a phototransistor, but polling the state and writing the state to a digital output pin, then reading that state from the same pin and acting on that.

More detail to follow. Thanks for helping me to understand. This is my first postin.

The following diagram could be helpful for the OP to view the internal structure of 1-bit port line:

Thanks all for the comments..... to clarify, this is a circuit for a grade crossing on a model railroad club layout. I have found various pieces of code and help on other model railroad forums.

The phototransistors are used as optical detectors for trains entering and exiting the grade crossings. They are constantly polled by the code and when one set (differential detectors) is covered, that state is saved in a sensor (unconnected pin) to be read later in the code to start the flashers and gates. After a train has exited the crossing, the gates are raised and the flashers stop.

The code below is generic as I have embellished for multiple tracks and graceful resets/initialization in the event that the crossing never deactivates.

NOTE: THE CODE IS TRUNCATED - to fit in the post, but shows how the phototransistors are polled and used.

I hope this helps...

Crossing Circuit:

/*
 This code is adapted from Example Level Crossing Control
 *  Version 1.3 by Geoff Bunza 2014 For Gate Crossing Logic
 *  
  *  Code may be made simpler, but works for my use - Bernie Giancola 4/24/2018
*/
#include <VarSpeedServo.h>
 
VarSpeedServo gate1servo;  // create servo object to control crossing gate 1
VarSpeedServo gate2servo;  // create servo object to control crossing gate 2
                   
 int PhotoT1 = A0; // phototransistor - pin A0  //NORTH DETECTOR SET - Southbound Route 1
 int PhotoT2 = A1; // phototransistor - pin A1
 int PhotoT3 = A2; // phototransistor - pin A2  //SOUTH DETECTOR SET - Northbound diecction 2
 int PhotoT4 = A3; // phototransistor - pin A3

 int Route;         // use for checking Route 1=Southbound, 2=Northbound

 int led1 = 9;      // Led 1 pin first alternating flasher OR RELAY
 int led2 = 10;      // Led 2 pin first alternating flasher OR RELAY
 int led3 = 11;      // Led 3 pin second alternating flasher
 int led4 = 12;     // Led 4 pin second alternating flasher

 int OffCount1 = 0; // keeps LED on for a time after hit
 int OffCount2 = 0;

 int PTRead1;
 int PTRead2;
 int PTRead3;
 int PTRead4;

 int Trigger1;
 int Trigger2;
 // Sensor1 and Sensor2 have pins identified, but not connected to anything
 int Sensor1=7;            // North Detector, southbound train
 int Sensor2=8;            // South Detector, northbound train

int leaving_sensor = 0;        // this indicates which sensor shows train leaving
int gates_started = 0;         // this indicates the crossing is active
int flash_state = 0;
long flash_time = 0;
long  flash_interval = 900;    // time in milliseconds between alternating flashes
int sensor_count = 0;

void setup() {

  pinMode(PhotoT1, INPUT);
  pinMode(PhotoT2, INPUT);
  pinMode(PhotoT3, INPUT);
  pinMode(PhotoT4, INPUT);

  pinMode (Sensor1, OUTPUT);
  pinMode (Sensor2, OUTPUT);
  digitalWrite(Sensor1,HIGH);
  digitalWrite(Sensor2,HIGH);

 int Route = 0;    // Route 1=Southbound (north detector), 2=Northbound (south detector)

 gate1servo.attach(3);  // attaches the servo on pin 3 to the servo object
 gate2servo.attach(4);  // attaches the servo on pin 4 to the servo object
 gate1servo.slowmove(90, 15);  //start assuming no train - gates raised
 gate2servo.slowmove(90, 15);  //start assuming no train - gates raised

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(led1, HIGH);  // Start with all flashers off
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  flash_time = millis();
  Serial.begin (9600);  
  Serial.println("Differential Light Test v0.1 and crossing circuit");
  delay(50);
}

void loop() {

  PTRead1 = analogRead(PhotoT1);
  PTRead2 = analogRead(PhotoT2);
  PTRead3 = analogRead(PhotoT3);
  PTRead4 = analogRead(PhotoT4);

  Trigger1 = (PTRead1 + PTRead2) / 2;
  Trigger1 = Trigger1 / 5;
  Trigger2 = (PTRead3 + PTRead4) / 2;
  Trigger2 = Trigger2 / 5;

  if (abs(PTRead2 - PTRead1) > Trigger1) {
     digitalWrite(Sensor1,LOW);
    OffCount1 = 0;
  }
  else {
    OffCount1++;
    if (OffCount1 >= 350) { // counter keeps LED on for a time after detection
     digitalWrite(Sensor1,HIGH);
          OffCount1 = 0;
    }
  }

  if (abs(PTRead3 - PTRead4) > Trigger2) {
    digitalWrite(Sensor2, LOW);
    OffCount2 = 0;
  }
  else {
    OffCount2++;
    if (OffCount2 >= 350) {
      digitalWrite(Sensor2, HIGH);
      OffCount2 = 0;
    }
  }
 
  if ((digitalRead (Sensor1)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = (Sensor2);
    Route = 1;
Serial.println (" START1");
    starting_sequence();
  }
  
  if ((digitalRead (Sensor2)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = (Sensor1);
    Route = 2;
Serial.println (" START2");
    starting_sequence();
  }

   if (gates_started) flash_leds();   //gates are down continue, start flashing

   if ((digitalRead(leaving_sensor)==LOW)&&(gates_started==1)) {   //train is starting to leave
   //as long as the leaving sensor is active the train is still in the crossing
   while (gates_started==1)  {   //now check if train is REALLY gone
   checking2();
     sensor_count = 0;
     for (int i=1; i<40; i++)  {
       if (digitalRead(leaving_sensor)==LOW) sensor_count++;
           delay (40);
      flash_leds();
                   }
     if (sensor_count==0) gates_started=0;
     flash_leds();     
     }
     // we only get here if the train has really left the crossing
                 ending_sequence();
  }
}
    // keep flashing leds
    delay(40);                       // waits 40ms to slow servo
  }
}
 
void ending_sequence()  {
  Serial.println("Ending_sequence  ");
    for (int i=1; i<120; i++)  {  // Allow time between gates raise and flashers stop
    gate1servo.slowmove(90, 15);  // move gates to go raised position
    gate2servo.slowmove(90, 15);  // move gates to go raised position
    flash_leds();    // keep flashing leds
    delay(40);                       // waits 40ms to slow servo
    } 
  digitalWrite(led1, HIGH);  //  flashers completely off
  digitalWrite(led3, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led4, HIGH);

  digitalWrite(Sensor1,HIGH);
  digitalWrite(Sensor2,HIGH);
  Route=0;
  Serial.print("END  Route  ");
  Serial.println (Route);
}
void flash_leds()  {
  Serial.println (" FLASH LEDS  ");
  if (flash_time > millis()) return;
  flash_state = ~flash_state;
  digitalWrite(led1, flash_state);  // Alternate flashers
  digitalWrite(led3, flash_state);
  digitalWrite(led2, ~flash_state);
  digitalWrite(led4, ~flash_state);
  flash_time = millis()+flash_interval;
}

If all you want to do is store a LOW/HIGH value and use it later it seems like using a couple of boolean variables would be much simpler.

bool sensor1;
bool sensor2;

Then replace digitalWrite(sensorPin1) = LOW or HIGH with sensor1 = LOW or HIGH

and replace digitalRead(sensorPin1) with sensor1 etc.

Or perhaps I'm missing something important.

Steve

Thanks,

That was the really the answer to the question I should have asked. I am a novice to programming didn’t understand Boolean variables. I will rewrite my test code and try.

Thanks again...