Arduino UNO - Serial.println() fluctation

Hi everyone,

I would like to state my speculation first: my UNO board has a hardware problem.

I have got an UNO board. Now I am learn how to act as an output source to a Processing project. I started by the simpliest example in Processing, reads like this:

On Arduino IDE side:

/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

On Processing IDE side:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(255,0,144);                 // set fill to light gray
  }
  println(val);
  rect(50, 50, 100, 100);
}

I soon realize the program doesn't work properly. 1. The TX led on UNO blink very fast (I think few times per second). 2. When I println on Processing, reads something like: 49 13 10 10 10 1 49 13 10 10 10 1 49 13 ...blahblahblah... (which is not 0 or 1 as per expected) 3. I disconnect from Processing, check Arduino serial console, when I short the input pin (no. 4 in this case) to 5V (HIGH), the console changes from 0 to 1; However, when I disconnect it, the "1" last for few more seconds. 0 0 0 0 (let pin start HIGH) 1 1 1 1 1 1(stop HIGH) 1(but still keep coming "1"s) 1 1 1 0 0 0 0 ...blahblahblah...

I think I have carefully write my codes. Anyone can tell me what it should be on a GOOD Arduino board? And how should I rule out the possibilities of the hardware failure of the board? (I have tried changing wires, using different ports, different HIGH sources...)

Last but not least, I think my board is NOT a genuine Arduino board. Does anyone know if Arduino officially authorized manufacturer in China (or other countries)? Is it good to buy one outside from official website?

Thank you very much!

Oopps... sorry seems I posted to a wrong discussion group...looking for the way to delete it...

Ask a moderator to move it ;) Use the report button under your opening post.

The TX will flash about 10 times a second as that is how often you send data.

I have slight doubts that the Arduino code that you posted is the one that actually resulted in the output you gave. A Serial.println(1) would give 49 13 10 or 48 13 10.

// Edit To elaborate: the print family prints text so it prints the character '1' or '0'; the println adds the CR/LF. Look at the ascii table to understand the results that you get.

Thread moved.

@lowbpro, thank you for using report-to-moderator.

How IS your switch wired? Since you are not using the internal pullup resistor, you MUST have an external resistor, as a pullup or pulldown resistor, which complicates the wiring.

Using the internal pullup resistor is so much simpler.

You should also look at the state change detection example. Send data when the switch BECOMES pressed, NOT every time through loop() when the switch IS pressed. Send a different value when the switch BECOMES released, NOT every time through loop() when the switch IS released.

Hi PaulS,

You are the man! It is due to my silly wiring. I simply connect the INPUT pin to 5V pin. Forgive my poor electronics knowledge. After re-wiring, now my little project seems working!

I also learned from below site, that: https://www.arduino.cc/en/Tutorial/Button

If you disconnect the digital I/O pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

For now I am going to study what pull-up/down resistors are. Why are they affecting the stability?

Take the chance to thank all who helped out!