Making Serial.print() to echo back

I uploaded the code below to Arduino and the purpose of this is to echo back what has just been sent to it through Serial.
Problem is: it is not echoing back.

Do you guys know why?

Thanks

P.S: I'm using the Serial Monitor provided by the Arduino IDE.

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

void loop() {
  if (Serial.available()) {
    Serial.print((char) Serial.read());
    delay(10);
  }
}

What happens when you hit 'send?

try using a var inbetween

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

void loop() 
{
  if (Serial.available() > 0) 
  {
    char c = Serial.read();
    Serial.print( c );
  }
  delay(10);
}

@AWOL It just sends but returns nothing. Doesn't even show up what I've just sent.

@robtillaart Tried that before. Still doesn't work.

(deleted)

Does this work?

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

void loop() 
{
  Serial.print("Hello world");
  delay(10);
}

Yes it does.

Seems like something with the Serial.available() condition.

@spycatcher2k Yes!

char inByte;  // incoming serial byte

void setup() {
// put your setup code here, to run once:
  Serial.begin(9600);          // serial communication baud rate
  }


void loop() {
// while loop begins here, continous loop:
  if (Serial.available()) {    // check for incoming data --> if available
  inByte = Serial.read();      // store incoming data
  Serial.print(inByte);        // echo back the data
}

}

This code should get you going....

Can also be a hardware failure,

change cable e.g. with your printers cable

robtillaart:
Can also be a hardware failure,

change cable e.g. with your printers cable

Either I caught rob trying to be a hardware guy (I have an impression that he is software guy) or the OP didn't even upload the sketch to the arduino. :grin:

If I assume the sketch was loaded to arduino, then the hardware was able to do both read and write over serial.

Either I caught rob trying to be a hardware guy (I have an impression that he is software guy)

Yes, you caught me :wink: indeed software guy by profession and hobby, but learned a lot about HW here

If I assume the sketch was loaded to arduino, then the hardware was able to do both read and write over serial.

and If the read failed during upload ... the sketch wasn't uploaded and it would not echo back .....

I suppose I should try this scenario, disconnect arduino tx and see what error message (if at all) pops up during a sketch upload. I assumed there would be an error I don't actually have any experience.