# USB Serial Library for Processing for Android?

**URL:** <https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750>\
**Category:** Processing for Android\
**Created:** [February 12, 2025, 8:21pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750 "2025-02-12T20:21:10Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 14, 2025, 4:04pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/21 "2025-02-14T16:04:09Z")

</div>

Thank you. When you have time, please try:  
1 - Use the back button to leave the app while serial is connected.  
2 - Remove the USB cable when serial is connected.

These two situations lead to the app crashing. If I use the other buttons (circle and square) to hide the app, it won’t crash and it will work after I launch it again.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 15, 2025, 3:34am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/22 "2025-02-15T03:34:57Z")

</div>

Hello @jhsa,

Can you describe in detail the hardware connections you are using?  
What is the receiving side? Code for receiving side?

If you can provide a MCVE it helps:  
_[How to create a Minimal, Reproducible Example - Help Center - Stack Overflow](https://stackoverflow.com/help/minimal-reproducible-example)_

These are helpful: and also references MCVE:  
_[Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147)_

`:)`

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 15, 2025, 9:41pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/23 "2025-02-15T21:41:51Z")

</div>

Sure, here is the Arduino Test Code. The board I am using is a ESP32-C3 Super Mini, and it connects via USB OTG cable to my phone. Basically just USB serial happening between phone and ESP32. The board is also powered by the phone via the USB connection:

```Processing
#define LED_1_PIN 9
#define LED_2_PIN 10

uint8_t Volume = 0; 
uint8_t Gain = 0; 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  analogWrite(LED_1_PIN, 0);
  analogWrite(LED_2_PIN, 0);
}

void loop() {
 String inData;
  bool Get_Data = false;
  // bool CheckConnection = false;

  if (Serial.available() > 0) {
    inData = Serial.readStringUntil('\n');

    if (inData.equals("Volume")) {
      Get_Data = true;
      while (Get_Data) {
        if (Serial.available() > 0) {
          inData = Serial.readStringUntil('\n');
          Volume = inData.toInt();
          analogWrite(LED_1_PIN, Volume);
          Get_Data = false;
          inData = "";
        }
      }
    }

    if (inData.equals("Gain")) {
      Get_Data = true;
      while (Get_Data) {
        if (Serial.available() > 0) {
          inData = Serial.readStringUntil('\n');
          Gain = inData.toInt();
          analogWrite(LED_2_PIN, Gain);
          Get_Data = false;
          inData = "";
        }
      }
    }

  }
}

```

[Previous page](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750.md?page=1)
