Connexion robot Android <--> Arduino Nano

Hello everyone,

I'm working on a project involving a robot controlled by an Android tablet. The robot tablet is running Android Pie 9 and uses a main application, which manages the robot's reactions. We have developed an Android application that overlays the main app with a transparent background, so that the main app still runs in the background. The tablet has multiple USB ports: a Type-A port at the back and mini/micro USB ports, to which we have connected an Arduino Nano board.

We are using Arduino Nano A000005 ATmega328, 16MHz. The application, developed with Android Studio is based on the robot's Android SDK.

The goal is to send a message from my app to the Arduino board via serial USB connexion.

For the USB communication with Arduino, we first tested the connection by listing the USB devices using the UsbManager in Android. The tablet correctly identifies the Arduino Nano. When we send data from the Android app to the Arduino using "connection.bulkTransfer", the RX LED on the Arduino lights up, indicating that data is being received.

On the Arduino, I made a quick code that lights the LED_BUILTIN when "Serial.available()>0". It works fine when I use the serial monitor on Arduino IDE, but it doesn't work when my robot is sending the message... even though the RX led lights up.

Also on my Android app logs, I check that the message has been sent and it works.

How is it possible that my Android app sends a message, my Arduino receives the message, but the condition "Serial.available()>0" is False ?
Also, is there an easier way of communicating between Android tablet and Arduino Nano ?

My arduino code is :

void setup() {
  Serial.begin(9600);           
  pinMode(LED_BUILTIN, OUTPUT);  
  Serial.println("Setup done");  
}

void loop() {
  if (Serial.available() > 0) {   
    digitalWrite(LED_BUILTIN, HIGH);  
    delay(2000);                     
    digitalWrite(LED_BUILTIN, LOW);   
    Serial.read();
    delay(2000);                   
  }
}

Your posted code works... (put something on the serial line and the LED lights for two seconds and goes out). Is there a valid connection? Is anything leaving the tablet? Can you verify these smaller steps in the full project?

Are you sending data and receiving data at the same speed? I see the Arduino is set to receive at 9600, so is the app sending at 9600?

Yes, it works when I use the Serial monitor, no problem.

I guess there is a valid connection since I can see the message leaving my Android code, and I see the RX LED on the Arduino lighting up.

Actually, I am not sure. How can I define the baud rate on my android app ?

Unfortunately I'm afraid I can't help much. I'm still learning Android Studio, and have only ever made one simple Android app using the MIT App Inventor web tool. Attached here is a picture of the baud setting in the MIT App Inventor.

Anyway, after browsing the web for a minute, it appears that the default baud rate for most serial devices is already set at 9600, so my suggestion might be way off base to begin with. I have seen mis-matched baud rates create the problem of not being able to see output in the Arduino Serial Monitor before. Other problems I've encountered had to do with disabling the New Line/Carriage Return symbols in Serial Monitor, but that is usually a problem when you are sending data from the Arduino, not receiving data on the Arduino.

Again, I'm afraid that I'm not going to be much help here, but I would suggest reviewing all of the settings in the Arduino Serial Monitor, as well as the COM port settings on the tablet and any relevant settings in Android Studio. I wish I could help more :frowning:

Actually I've had better results when changing the baud rate on the arduino size to 38400, 57600 and 115200. Thanks for suggesting the idea of mis-matched baud rates !

The problem I have now is that I can't read the data correctly. Here is my Arduino code :

void setup() {
  Serial.begin(115200);          // Initialisation du port série à 115200bauds
  pinMode(LED_BUILTIN, OUTPUT);  // Définition de la LED_BUILTIN comme sortie
}

void loop() {
  if (Serial.available() > 0) { // Vérifie si des données sont disponibles
    String receivedData = Serial.readStringUntil('\n'); // Lecture des données jusqu'au séparateur de ligne
    receivedData.trim(); // Supprime les espaces et les caractères invisibles
    receivedData = receivedData.charAt(0);
    Serial.println(receivedData);
    if (receivedData.equals("1")) {
      Serial.println("Case 1");
      digitalWrite(LED_BUILTIN, HIGH);  // Allume la LED_BUILTIN
      delay(2000);                      // Attend pendant 2 secondes
      digitalWrite(LED_BUILTIN, LOW);   // Éteint la LED_BUILTIN
    } else if (receivedData.equals("2")) {
      Serial.println("Case 2");
      digitalWrite(LED_BUILTIN, HIGH);  // Allume la LED_BUILTIN
      delay(2000);                      // Attend pendant 2 secondes
      digitalWrite(LED_BUILTIN, LOW);   // Éteint la LED_BUILTIN
      delay(1000);
      digitalWrite(LED_BUILTIN, HIGH);  // Allume la LED_BUILTIN
      delay(2000);                      // Attend pendant 2 secondes
      digitalWrite(LED_BUILTIN, LOW);   // Éteint la LED_BUILTIN
    } else if (receivedData.equals("3")) {
      // Code TODO
    } else if (receivedData.equals("4")) {
      // Code TODO
    } else {
      Serial.print("Case default : Received data : ");
      Serial.println(receivedData);
      digitalWrite(LED_BUILTIN, HIGH);  // Allume la LED_BUILTIN
      delay(200);                      // Attend pendant 2 secondes
      digitalWrite(LED_BUILTIN, LOW);   // Éteint la LED_BUILTIN
    }
  }
}

The goal is to have 4 different behaviors that can be launched from the Android by sending different messages.
Here I tried with Strings by sending "1\n", "2\n" and so on... But i've also tried with integers and bytes. And my code always executes the "else" part. I am not sure what my Arduino is reading, but it is clearly not reading "1" or "2" when i send it.

Once again, when I use the Serial monitor on Arduino IDE, it works fine, but it doean't work when I'm sending messages from the Android app.

Does it have something to do with the baud rate change ?
Do you know how I can fix that ?

I have had a problem similar to this as well. I think the problem is that you are mis-matching your data types. You are receiving your data as a STRING, which is an array of CHARs. Then, with the receivedData.equals statement, you are taking one of those CHARs and casting it as an INT. Because of this, your "1" is actually being interpreted as a "49."

To verify this, change your if statements to something like:
if (receivedData.equals("49")) { ...
and
else if (receivedData.equals("50")) { ...
and so on.

This was confusing to me as well, until I realized that the Number 1 = 1, but the Letter 1 = 49. In fact, every letter, number, and special character on the character map will turn in to a simple integer like this when you cast it as an INT. You can see what integer a particular character will be by opening the Windows Character Map, selecting a character, looking at the Character Code at the bottom, and converting the HEX value to a Decimal value. For example, the symbol "1" has a code of 0x31, which converts to 49 in decimal.

So the real solution to your problem here is to be consistent with the data types that you are sending and reading. Either send INTs and interpret them as INTs, or send CHARs and interpret them as STRINGs and CHARs. But don't send CHARs and them cast them as INTs.

Hope this helps!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.