Simple INPUT_PULLUP for a switch using only a wire

Hello,

I am very new to Arduino coding and I am trying to write some code that would require me to use a wire to create a simple switch. My wire is connected to the ground and to digital pin 2. When I pull the wire out of the pin, I want my code to print out "Off", which would signify that my set up should not be running and my value is 0 volts as opposed to 5v when it is connected to pin 2. The goal is to have the wire act as a switch without needing a button or resistor. I am a bit confused, though I assume it is a simple task. Here is what I have so far, which does not work. (Pin 13 is not connected to anything)

void setup() {
Serial.begin(9600);
Wire.begin();

pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);

void loop() {
int sensorVal = digitalRead(2);
Serial.println(sensorVal);

if (sensorVal == HIGH) {
digitalWrite(2, LOW);
} else {
digitalWrite(2,HIGH);
}

Thank you in advance!

  if (sensorVal == HIGH) {
    digitalWrite(2, LOW);

Why are you reading from pin 2 and then writing to pin 2 ?

Whoops, sorry I had it to pin 13 in my actual code.

So let's see your actual code in a new post, but please Read this before posting a programming question and follow the recommendations regarding posting code

1. The following diagram (Fig-1) shows the Internal Pull-up associated with DPin-2 and the external jumper wire.
pullUp.png
Figure-1:

2. When the internal pullup is connected and the jumper is also connected, the logic level at DPin-2 is LOW (0V)

pinMode(2, INPUT_PULLUP);
bool n = digitalRead(2);   // n = LOW ~= 0V

3. When the internal pullip is connectec and the external jumper is renoved, the logic level of DPin-2 is HIGH (5V).

pinMode(2, INPUT_PULLUP);
bool n = digitalRead(2);   // n = HIGH ~= 5V

4. Composite Code

pinMode(2, INPUT_PULLUP);
bool n = digitalRead(2);
if (n == LOW)
{
   Serial.print("Jumper is connected");
}
else   //n == HIGH
{
     Serial.print("Jumper is disconnected");
}

pullUp.png

Your program all sounds very complex for what is a simple routine. You don't need any libraries, WIRE is for sensors using the "one wire" protocol, not switches.

Input pullup is fine, that will give a logical high when the wire is removed.

All you need to do is look at the state of D2 with digitalRead and then print when the input reads logical low.