user8060: Code Snippet:
String a;

const int red_led_pin = 13;

void setup() 
{
  Serial.begin(115200);
  Serial.println("Hello, ESP32-S2!");
  pinMode(red_led_pin, OUTPUT); 
}

void loop() {

    while (Serial.available()) 
    {
        a = Serial.readString();// read the incoming data as string
        Serial.print(" First: ");
        Serial.println(a);
    

        if (a.equals("on"))
        {
          Serial.println("LED ON");
          digitalWrite(red_led_pin, HIGH);

        }


        else  if (a == "off" || a == "OFF")
        {
          Serial.println("LED OFF");
          digitalWrite(red_led_pin, LOW);
        }

        Serial.print("Second: ");
        Serial.println(a);
    }
}

Serial.print out:

Circuitry:

Issue:
The code can't capture the on string or any other string (on, off or OFF) that I pass. However it does pick it up and Serial.print it.
What is going wrong?
Things I have tried:
I have tried as a comparison:

if (a.equals("on")){<>}
if (a == "on")){<>}
if (a.equalsIgnoreCase("on")){<>}

Due Diligence / Prior Research:

[ Documentation ] String Comparison Operators
[ Documentation ] String()
[ Forum ] Compare characters (or string) in “if” function


user42015: Your problem is the line ending. The Serial Monitor has an option to send a line ending after the data, that you input. This line ending can be different depending on this setting, though mostly in the Arduino world a simple newline character '\n' is used.
This line ending will be in your string, when using Serial.readString(), so the string is not equal to "on", but to something like "on\n".
You can solve this issue by two ways:

Set the Serial Monitor to no line ending.
Or better: Use something like Serial.readStringUntil() and pass the line ending character to it as parameter (for example Serial.readStringUntil('\n')). This will read from the Serial interface until the specified character is reached. The specified character will be discarded and you are left with only the data, which you can then do comparisons on. This also has the advantage, that your code will react much faster, since it doesn't wait for 1s for new data (as long as you actually send the needed line ending).


8060: <p><strong>Code Snippet:</strong></p>
<pre><code>String a;

const int red_led_pin = 13;

void setup() 
{
  Serial.begin(115200);
  Serial.println(&quot;Hello, ESP32-S2!&quot;);
  pinMode(red_led_pin, OUTPUT); 
}

void loop() {

    while (Serial.available()) 
    {
        a = Serial.readString();// read the incoming data as string
        Serial.print(&quot; First: &quot;);
        Serial.println(a);
    

        if (a.equals(&quot;on&quot;))
        {
          Serial.println(&quot;LED ON&quot;);
          digitalWrite(red_led_pin, HIGH);

        }


        else  if (a == &quot;off&quot; || a == &quot;OFF&quot;)
        {
          Serial.println(&quot;LED OFF&quot;);
          digitalWrite(red_led_pin, LOW);
        }

        Serial.print(&quot;Second: &quot;);
        Serial.println(a);
    }
}
</code></pre>
<p><strong><code>Serial.print</code> out:</strong></p>
<p><a href="https://i.stack.imgur.com/kJPdF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kJPdF.png" alt="enter image description here" /></a></p>
<p><strong>Circuitry:</strong></p>
<p><a href="https://i.stack.imgur.com/x3e3r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x3e3r.png" alt="enter image description here" /></a></p>
<p><strong>Issue:</strong></p>
<p>The code can't capture the <code>on</code> string or any other string (<code>on</code>, <code>off</code> or <code>OFF</code>) that I pass. However it does pick it up and <code>Serial.print</code> it.</p>
<p>What is going wrong?</p>
<p><strong>Things I have tried:</strong></p>
<p>I have tried as a comparison:</p>
<ul>
<li><code>if (a.equals(&quot;on&quot;)){&lt;&gt;}</code></li>
<li><code>if (a == &quot;on&quot;)){&lt;&gt;}</code></li>
<li><code>if (a.equalsIgnoreCase(&quot;on&quot;)){&lt;&gt;}</code></li>
</ul>
<p><strong>Due Diligence / Prior Research:</strong></p>
<ul>
<li><a href="https://docs.arduino.cc/built-in-examples/strings/StringComparisonOperators" rel="nofollow noreferrer">[ Documentation ] String Comparison Operators</a></li>
<li><a href="https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/" rel="nofollow noreferrer">[ Documentation ] String()</a></li>
<li><a href="https://forum.arduino.cc/t/compare-characters-or-string-in-if-function/692192/20" rel="nofollow noreferrer">[ Forum ] Compare characters (or string) in “if” function</a></li>
</ul>
 42015: <p>Your problem is the line ending. The Serial Monitor has an option to send a line ending after the data, that you input. This line ending can be different depending on this setting, though mostly in the Arduino world a simple newline character <code>'\n'</code> is used.</p>
<p>This line ending will be in your string, when using <code>Serial.readString()</code>, so the string is not equal to <code>&quot;on&quot;</code>, but to something like <code>&quot;on\n&quot;</code>.</p>
<p>You can solve this issue by two ways:</p>
<ul>
<li>Set the Serial Monitor to no line ending.</li>
<li>Or better: Use something like <code>Serial.readStringUntil()</code> and pass the line ending character to it as parameter (for example <code>Serial.readStringUntil('\n')</code>). This will read from the Serial interface until the specified character is reached. The specified character will be discarded and you are left with only the data, which you can then do comparisons on. This also has the advantage, that your code will react much faster, since it doesn't wait for 1s for new data (as long as you actually send the needed line ending).</li>
</ul>
