Serial monitor output on web editor shows mixed and doubled characters

Hello there!

I am fairly new to Arduino and currently working on the third Starter project, which uses a temperature sensor as an input and activates LEDs depending on analog values. I am running the sketch on the Web Editor and printing some values recorded using Serial.print(), with a baud of 9600 (same in sketch and Serial Monitor platform).

The problem is, the values in each printing session are mixed with each other, and some characters are doubled, shown in the examples below:

Example 1: Sensor value: 1or value: 145, Volts: 0.71,45, Volts: 0.71, Temperature: 20 Temperature: 20.80
Example 2: Sensor value: 146, Volts: e: 146, Volts: 0.71, Temperatur0.71, Temperature: 21.29
Example 3: Sensor value: 144, Vol value: 144, Volts: 0.70, Tempets: 0.70, Temperature: 20.31

Instead of (example):

Sensor value: 145, Volts: 0.71, Temperature: 20.8

I tried different baud values but the characters do not make sense. The sketch is as follows:

const int TempSensor = A0; // analog input pin connected to temp sensor
const float baseLineTemp = 18.0; // 18 degrees

void setup() {
// open up connection between Arduino and PC to send analog values
Serial.begin(9600); // 9600 bits per second

// set output pins (default state: LOW)
for (int pinNumber = 2; pinNumber < 5; pinNumber++){
pinMode(pinNumber, OUTPUT);
}

}

void loop() {
// read temperature analog value and print on monitor
int sensorVal = analogRead(TempSensor);

Serial.print("Sensor value: ");
Serial.print(sensorVal);

// convert analog reading to voltage and print
float Voltage = (sensorVal/1024.0) * 5.0;

Serial.print(", Volts: ");
Serial.print(Voltage);

// convert voltage to temperature and print
float Temperature = (Voltage - 0.5) * 100;

Serial.print(", Temperature: ");
Serial.println(Temperature);

// activate LEDs depending on temperature

if (Temperature < baseLineTemp){ // if temperature less than baseline
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}

else if (baseLineTemp <= Temperature && Temperature < baseLineTemp + 2){ // if temperature between baseline and baseline +2
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}

else if (Temperature >= baseLineTemp + 2){ // if temperature more than baseline +2
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}

delay(1); // a small delay of 1 ms is advised for ADC
}

Any idea what might be the case?

Thank you!

try adding a delay at the end, to see if the loop is running so fast the serial monitor can't keep up

or change the Serial.begin from 9600 to 115,200 to see if making Serial faster solves the problem

Thank you for your response!

I tried adding a delay of three seconds at the end of the loop, but the result was the same. I also tried adding a delay of 0.5 seconds after each Serial.print() with no effect. I finally tried all possible baud values, but the characters were unreadable.

Although the issue seemed to dissapear in my latest runs (with my original sketch), it has appeared again.

Looks like buffer overrun somewhere.
Try a much slower baudrate say 4800

Also have look at my Arduino Serial I/O for the Real World for how to add extra non-blocking buffering to your Serial output.

These are the values I am getting for baud rate 4800:

ff~~ff~~fffxf~`f~fffxfffffxf~fffxf

Try this in setup() and get that working with 10 9 etc being printed

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
. . .
}

Thank you for your response! The above shows the numbers perfectly.

In general, I have noticed the following pattern - on every initial opening of the editor in my browser, the problem appears. Upon a refresh however, it suddenly goes away.

If this is a standard pattern, it is not too much of a problem as the issue is resolved quite easily. I am quite curious however as to why this happens and if other people are experiencing the same issue.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.