Pin Problems

So I am trying to do a very simple circuit to solve a problem with a larger project. I have a 12v source to my arduino through a pc900 sharp optocoupler. The problem is that in my code I only want for the LED to come on once the 12v source sends a signal to my pin 7. No matter what, as long as I have a jumper connected to pin 7, it will read high, unless of course i connect it to ground from pin 7.

   //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int thermPin = 7;
int thermState = 0;

/////////////////////////////
//SETUP
void setup(){
  
  
  pinMode(ledPin, OUTPUT);
  
pinMode(thermPin, INPUT);

 
   
}
////////////////////////////
//LOOP
void loop()
{
 thermState = digitalRead(thermPin);
 if(thermState == HIGH)
 {
          //the led visualizes the sensors output pin state
       digitalWrite(ledPin, HIGH);
 }
         else{
          
          digitalWrite(ledPin, LOW);
         }
       


      
      }

A quick perusal of the state change detection example is in order.

Try this ;
change

pinMode(thermPin, INPUT);
to

pinMode(thermPin, INPUT_PULLUP);
and wire the optocoupler to pull the output low when 12V is present.
Then
if (digitalRead(thermPin) == LOW){
// 12V present, do something
}

So you're saying that I need to use a pull down resistor with my optocoupler and to change my code so that when the pin does not receive any sort of voltage from my 12v source, then it will turn on the LED?

I also tried this code and am still got the same result, the led stays on but is kinda flickering.

   //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int thermPin = 7;
int thermState = 0;

/////////////////////////////
//SETUP
void setup(){
  
  
  pinMode(ledPin, OUTPUT);
  
pinMode(thermPin, INPUT);


 
   
}
////////////////////////////
//LOOP
void loop()
{
 thermState = digitalRead(thermPin);
 
digitalWrite(ledPin, thermState);
}

the led stays on but is kinda flickering.

That indicates a floating pin. No amount of code is going to fix that. You need an external resistor, pulling the pin consistently HIGH or LOW, OR you need to enable the internal pullup resistor AND rewire the switch.

harrinja:
So you're saying that I need to use a pull down resistor with my optocoupler and to change my code so that when the pin does not receive any sort of voltage from my 12v source, then it will turn on the LED?

Every input pin should have a stable default (disconnected) state. This can be achieved by connecting a pull up resistor to Vcc (default HIGH), or a pull down resistor to Gnd (default LOW). Then connect a switch or optocoupler to the other supply voltage (Gnd/Vcc), that inverts the input state when activated. The mode INPUT_PULLUP can be used to activate the internal pull up resistor (to Vcc), so that no external resistor is required in this case.

See attached is any clearer as to the intent of the suggestions: