Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have a problem with the function Serial.write() I was testing my connection with python, and I can send numbers, words, or letters, but when I try to send the value of variable the function Serial. write gives me an error. So could you tell me what is happening?

#define Pin A1

void setup() {

    pinMode(Pin, INPUT); 

    Serial.begin(9600); 

}

void loop() {

    String a = String(13);

    Serial.write(a);   

}

ERROR

C:\Users\ADMIN\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\cores\arduino/Print.h:52:12: note:   no known conversion for argument 1 from 'String' to 'const char*'

exit status 1

No matching function for call to 'HardwareSerial:: write(String&)'

1 Answer

0 votes
by (36.8k points)
edited by

If you want to send string means you directly enter the string in serial.write(). while giving the string directly into the serial.write() then it will take that string as const char* not as a string type

Serial.write("hello");

Else you cannot send the string as directly in serial.write.you can use serial.print.use char array to send

Serial.write(MyString, sizeof(MyString));

Else you can send every char

void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)

  {

    Serial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass

  }

}// end writeString

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...