I am trying to do a simple serial communication control via sending a char and using it as an if condition, the arduino part is below

char incomingchar;
void setup() {
    Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
//        Serial.write("g"); // does not work when I explicitly write here either as opposed to through python.

void loop() {
    if (Serial.available()) {
            // read the incoming char:
            incomingchar = Serial.read();
               if (incomingchar=="g"){
              Serial.println("hello world!");}

And here is the Python code:

import serial;

When I run this, the python program returns a '1', and so does the arduino program in the serial monitor.

Note, if I directly set Serial.write('g'), it simply types out 'g' in the monitor and does nothing else from the loop.

  • With Char types you want to use the single-quote mark instead of double quotes.
    – SDsolar
    Apr 27, 2017 at 5:34
  • Welcome to Arduino Stack Exchange. Be sure to take the tour at arduino.stackexchange.com
    – SDsolar
    Apr 27, 2017 at 5:35
  • 1
    I think @Mazaryk's answer is correct, but may a comment regarding your comment: the reason writing "g" to serial in your setup doesn't work is because that would be written to the output wire, and you're reading from the input wire.
    – Mark Smith
    Apr 27, 2017 at 5:36
  • hi Mark, thanks for your insight, could you explain a little more about output wire? I thought what I was doing was sending the 'g' to the arduino, what is the way to 'input' to the arduino then?
    – ha554an
    Apr 28, 2017 at 0:06

1 Answer 1


You declare incomingchar as a char. In your if you compare that char to a string (double quotes) incomingchar=="g". I think you want to compare it to a char (single quotes) like this:

if ( incomingchar == 'g' ) {
  // do something
  • Hi Mazaryk, I made the change, it works now (albeit in a different paradigm where serial monitor or Arduino IDE is not open) I think the issue was also that python can't access the port if you have arduino or serial monitor open, so it was preventing me from really testing it. If someone knows how to get over that, would be much appreciated.
    – ha554an
    Apr 28, 2017 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.