Serial OTG. Arduino CH340. FTDI. rkl099's Extension

5.- Other USB <> UART Rx/Tx converters.

- Cable OTG.
cable_otg

- Settings / Additional settings / OTG / Enable OTG.
otg26

5B.- Example with CP2102.

  • Android sends a text to Arduino, it is displayed in the Serial Monitor.

  • Arduino Serial Monitor sends a text to Android.

// Juan A. Villalpando
// kio4.com

#include <SoftwareSerial.h>
SoftwareSerial FTDI(10,11);
// El TX del módulo FTDI va al pin 10 del Arduino
// El RX del módulo FTDI va al pin 11 del Arduino
// El GND del módulo FTDI va al GND del Arduino

String de_android;
String a_arduino;

void setup(){
  FTDI.begin(9600);
  Serial.begin(9600);
}

void loop (){
// Lee del Android y escribe Monitor Serie.
  if(FTDI.available()) {
    de_android = FTDI.readStringUntil('\n');
    Serial.println(de_android);
  }
  
// Lee Monitor Serie y envía a Android.
  if(Serial.available()) {
     a_arduino = Serial.readStringUntil('\n');
     FTDI.print(a_arduino);
     Serial.println(a_arduino);
    }
  delay(5);   
}

p10A_OTG_CP2102.aia (186.6 KB)

5C.- Similar example with Serialevent.

About Serialevent:
https://docs.arduino.cc/built-in-examples/communication/SerialEvent

#include <SoftwareSerial.h>
SoftwareSerial FTDI(10,11);
// El TX del módulo FTDI va al pin 10 del Arduino
// El RX del módulo FTDI va al pin 11 del Arduino
// El GND del módulo FTDI va al GND del Arduino

String de_android;
String a_arduino;

String inputString = "";       // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void setup() {
   FTDI.begin(9600);
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // Lee del Android y escribe Monitor Serie.
  if(FTDI.available()) {
    de_android = FTDI.readStringUntil('\n');
    Serial.println(de_android);
  }
  
  // print the string when a newline arrives:
  if (stringComplete) {
    FTDI.print(inputString);
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
1 Like