[solved] serial.print?

Hello forum, im new here and im new to arduino, so i hope im posting the right place :slight_smile:

i have a problem, i want to output the status of a button,
there is example code for it, where a led light up when the button is pressed.
But i want to output text to my pc and here the board behaves strangely, i hope you can help me!
The signal seems to get more and more unstable for every serial.print command i use.
Here is an example to show what i mean

The original code (from the website):

/* Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. */

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

When i measure I get a stable signal of 5V when the button is not pressed.

But when i now add the code to output it to a pc, the signal gets unstable (new lines marked with //NEW LINE):

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);  //NEW LINE!
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
  Serial.print(" leftButton: "); //NEW LINE

  // check if the pushbutton is pressed.
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
    Serial.print("0"); //NEW LINE
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Serial.print("1");  //NEW LINE
  }
  delay(250);  //NEW LINE
}

And it gets worse for every Serial.print i add, when I write:

Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");Serial.print(" leftButton: ");

instead of just

Serial.print(" leftButton: ");

i get a signal which varies from 3-5V instead of stable 5V when the button is not pressed

I dont think it will be able to measure input as digital if it varies so much.
what am i doing wrong?

When i measure I get a stable signal of 5V when the button is not pressed.

When you measure what? How are you measuring whatever it is you are measuring?

I hope this helps:

Im measuring between the DigitalPin2 and ground.

Two questions does not merit one answer.

well, thats right.

When: i am measuring all the time.
What: i am measuring voltage.
How: i connected my tenma 72-7735 to the two pins.

does this answer your question?

does this answer your question?

all except what a tenma 72-7735 is.

Im measuring from the DigitalPin2 to the 5V.

It is customary to measure voltages with respect to ground, not the supply rail.

PaulS:

does this answer your question?

all except what a tenma 72-7735 is.

its a multimeter

AWOL:

Im measuring from the DigitalPin2 to the 5V.

It is customary to measure voltages with respect to ground, not the supply rail.

you are right, i actually measured it in respect to ground.
thanks for pointing out, ive edited it :slight_smile:

i connected my tenma 72-7735 to the two pins.

Which two pins?

AWOL:

i connected my tenma 72-7735 to the two pins.

Which two pins?

The digitalPin2 (which measures the input) and ground.

Ahh I found the bug
thanks to all of you :smiley:

edit:
explanation:
i used some digital pins to read the input. I also used pin1 for input and couldnt understand its strange behaviour always when i started to serial.print something. I had overseen that its TX too, so my guess would be that it acted strange because it actually was sending data through it - doh :stuck_out_tongue:
I used another pin now and it works fine now (yay)

Ahh I found the bug

And it was?

PaulS:

Ahh I found the bug

And it was?

as in 9/10 of the cases the person in front of it :stuck_out_tongue:
the explanation is in my previous post

anyway thx for help =)