Digital pin set to INPUT oscillates

I have the following code.

Regardless of whether I apply 5V or GND to pin 35, digitalRead(…) oscillates between 0 and 1 with each iteration of loop()

What am I doing wrong?

CODE

int nLEDPin = 13, nPhotoTransistorPin = 35, nMaxPin = 53;

void setup()
{
/*
*digitalPinToPCMSK(A0) |= bit(digitalPinToPCMSKbit(A0)); // Enable pin
PCIFR |= bit(digitalPinToPCICRbit(A0)); // Clear any outstanding interrupt
PCICR |= bit(digitalPinToPCICRbit(A0)); // Enable interrupt for the group
*/
Serial.begin(9600);

for (int nI = 0; nI < nMaxPin; nI++)
{
if (nI == nPhotoTransistorPin)
{
pinMode(nI, INPUT);
}
else
{
pinMode(nI, OUTPUT);
digitalWrite(nI, LOW);
}
}
}

/*
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
digitalWrite(nLEDPin, digitalRead(A0));
}
*/

void loop()
{
/*
for (int nI = 0; nI < nMaxPin; nI++)
{
Serial.print("Pin “);
Serial.print(nI);
Serial.print(” = ");
Serial.println(digitalRead(nI));
}
Serial.println();
Serial.println();
delay(1000);
*/
Serial.print("Pin “);
Serial.print(nPhotoTransistorPin);
Serial.print(” = ");
Serial.println(digitalRead(nPhotoTransistorPin));
delay(1000);
}

SERIAL MONITOR

Pin 35 = 1
Pin 35 = 1
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 0
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 1
Pin 35 = 0

Please use pin names rather than number as we then know what it does.

D1 means more than pin? as pin? numbers change with model.

Weedpharma

I found what the problem was.

The expansion board I made was not pushed into the arduino properly so some of its pins were not making contact with the corresponding arduino pins.

But I am still struggling to get the arduino digital pin to detect the phototransistor is tuned on.

I seem to get constant 1 from digital pin 35 or constant 0.

I am using L-51P3C.

Can anyone point me in the direction of a tutorial of using these devices with an arduino digital pin.

I was able to get this device to turn a LED on an off on my bread board:

But this doesn’t seem to work with the arduino digital pin if I just replace the LED anode with the digital pin.

Nor does it seem to work if I connect the phototransistor emitter to the digital pin and a 10k resistor from 5V to the phototransistor emitter.

Phototransistor L-51P3C.pdf (72.8 KB)

Why do,you have two transistors in series?

Try wiring it with 10k from Vcc to digital input pin, then the transistor C to the input and E to gnd.

The program should expect to read a HIGH normally and LOW when the IR is shone onto it. Then use the Arduino to turn on the LED.

Weedpharma