I have the following situation:

One Arduino Uno with a 433mhz transmitter wired to the laptop to have serial communication. Another Arduino Uno with a 9V battery wired with a 433mhz receiver and an LCD screen (16x2, I2C).

I have the following code to transmit:

#include <VirtualWire.h>

void setup() {
  //Force low when enabled
  //Set pin 12 for transmitter
  //Setup with speed of the data transfer.
  //Set serial communication at 9600 baud
  Serial.begin(9600);//set Serial
  //Set pin 13 to light up when transmitting

void loop() {
  //If message send via serial monitor
  if (Serial.available()) {
    //wait a bit for the entire message to arrive

    while (Serial.available() > 0) {
      //Read serial into string
      String message = Serial.readString();
      //Initialize char array
      char msg[message.length()];
      //Write string to char array
      message.toCharArray(msg, message.length());
      //send char array as unsigned int (bits)
      vw_send((uint8_t *)msg, strlen(msg));
      //Wait until the complete message has been send
      //Turn on light when finished
    //Turn off light
    digitalWrite(13, LOW);  

And the following code to receive a message and print on a LCD screen:

#include <LiquidCrystal_I2C.h>
#include <VirtualWire.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup() {
  //start up LCD with 16 chars with 2 lines
  //Turn on backlight
  //Write initialising message
  //Force low when enabled
  //Set pin 12 to receive
  //Setup with speed of the data transfer.
  //set pin 13 for light
  pinMode(13, OUTPUT);
  //Start receiver

  //Set message on LCD that setup has completed

void loop() {
  //initialize array for message
  uint8_t msg[VW_MAX_MESSAGE_LEN];
  uint8_t msgLength = VW_MAX_MESSAGE_LEN;

  //If message received
  if (vw_get_message(msg, &msgLength)){
    //Turn on light
    digitalWrite(13, HIGH);

    //Print on lcd
    lcd.write(msg, msgLength);
    //Else turn of light
    digitalWrite(13, LOW);

My problem is that the message that is being transmitted and received by the other arduino always is short 1 character. For example if I send "test", it receives and displays "tes". What am I missing?

(My short solution was to just add a space to the message, then the space got cut off. But I want to understand what is going wrong.)


1 Answer 1


I haven't found the source of the problem by reading through the code, and will limit my comments to advice about debugging the problem.

With the code as it stands, there are half a dozen points where the missing character might have dropped out. With just a final result, the problem location is difficult to narrow down. Modify your code to display intermediate data.

For example, in the sending code, print the read-in string and its length to Serial. Eg, if you #include <Streaming.h> you can say:

Serial << message << ' ' << message.length() << endl; Serial << msg << ' ' << strlen(msg) << endl;

and so forth.

[Note, msg actually occupies strlen(msg)+1 bytes, the last byte being a null-byte terminator, which your code doesn't send or receive.]

In the receiving code, besides the lcd.write of msg, do an lcd.print with msgLength.

You can also do some code clean-up in the sender. Remove the line

while (Serial.available() > 0) {, and its corresponding }, because Serial.readString() already contains a character-wait loop with timeout.

  • Thank you for the tips. Im not that familiar with serial/arduino, just started. So these comments about the code are nice!
    – Revils
    Dec 12, 2015 at 6:16

Your Answer

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

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