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

Hello friends,

I am going to comment on my experience with the great extension of the user @rkl099, which you can find in this topic:

  • With this extension we can connect a mobile phone (or tablet) to the Arduino with a CH340 chip by cable.

  • It has been tested with CdcAcm, PL2303, PL2303HX, FTDI, CP210x, CH341, CH341 (“fake”) and CH340.

  • Get com.SerialOTG.aix:

To carry out these examples I will use a mobile with Android 9, OTG cable, USB cable and an Arduino UNO with CH340.

IMPORTANT!!!: In the mobile Settings. Additional Settings. We must activate OTG.

- Settings / Additional settings / OTG / Enable OTG.

otg26

Here this guide in Spanish:
http://kio4.com/appinventor/10A_OTG_LCD.htm

0.- App sends the characters "a" and "b" to the Arduino to turn LED13 on/off.

p10A_OTG_print_LED.aia (186.0 KB)

otg17b

char caracter;

void setup() { 
  Serial.begin(9600);
  pinMode(13, OUTPUT); 
}

void loop() { 
  if(Serial.available()) {
  caracter = Serial.read(); 
  if(caracter == 'a'){ digitalWrite(13, HIGH);}
  if(caracter == 'b'){ digitalWrite(13, LOW);}
}
}

1 Like

1.- PrintSerial block. App sends a text to the Arduino via OTG cable, Arduino returns the same text and displays it on the LCD Screen.

p10A_OTG_print_LCD.aia (186.7 KB)

  • The LCD-I2C is not necessary, you can observe the return of the information in app.

  • The print block sends an end of line (code 10) each time the message ends.

// Juan A. Villalpando
// kio4.com

#include <Wire.h>
// Pantalla LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

char caracter;
String palabra;

 void setup(){  
  Serial.begin(9600);  
  lcd.begin(16,2);// Columnas y filas de LCD.
  lcd.clear();
 } 
  
 void loop() { 
 if(Serial.available()) {
  caracter = Serial.read();
  Serial.print(caracter);
  palabra = palabra + caracter;
    if(caracter == 10) {
      palabra = palabra.substring(0, palabra.length() - 1); // Delete last char 
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(palabra);
      palabra="";
    }
    delay(10); 
  }
}  


2.- WriteSerial blocks. App sends different types of data to the Arduino, Arduino returns that data.

p10A_OTG_write_LCD.aia (187.8 KB)

#include <Wire.h>
// Pantalla LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

char caracter;
String palabra;

 void setup(){  
  Serial.begin(9600);  
  lcd.begin(16,2);// Columnas y filas de LCD.
  lcd.clear();
 } 
  
 void loop() { 
 if(Serial.available()) {
  caracter = Serial.read();
  palabra = palabra + caracter;
    if(caracter == '*') {    
      palabra = palabra.substring(0, palabra.length() - 1); // Quita último caracter * 
      // Serial.println(palabra.toInt()* 2); // Si es un número, devuelve el doble.
      Serial.print(palabra);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(palabra);
      palabra="";
    }
    delay(10); 
  }
} 
  • I have used the asteric as the end of the message.
  • The asteric codes are: *, 2A, 42 (check ASCII)


3.- LCD and Keypad 4x4. print block.

p10A_OTG_LCD_Teclado.aia (186.7 KB)

  • App sends text by print block to Arduino + LCD.
  • Arduino + Keypad sends numbers to App.


4.- OTG and FTDI.

p10A_OTG_FTDI.aia (186.6 KB)

When we connect the Arduino to the mobile, we cannot see the information in the Serial Monitor of the Arduino IDE in PC.

We are going to use an FTDI module (USB <> RX/TX converter). We will connect FTDI module to the mobile.
We will connect the Arduino to the PC to see the information in the Serial Monitor. (Look image)

  • For the FTDI connection we will use the SoftwareSerial library. We will use terminals 10 and 11 of the Arduino.
    In this example Arduino will create a random number and send it to the Arduino IDE on the PC via its USB cable and to the mobile via the FTDI module.
#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

char caracter;
int aleatorio;

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

void loop (){
// Lee del Android y escribe Monitor Serie.
  if (FTDI.available() > 0) {
    caracter = FTDI.read();
    Serial.print(caracter);
  }
  
// Crea un número aleatorio y lo envía a Android
      aleatorio = random(33,126);
      FTDI.write(aleatorio);
      
  delay(500);   
}


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;
    }
  }
}

Where is the aix file?

That is the third request for the same thing.
Please do not spam the forum like this.

https://community.appinventor.mit.edu/t/serial-usb-connection/3162/28
https://community.appinventor.mit.edu/t/using-serial-with-ftdi/14136/76

Im sorry sir

6.- A potentiometer in Arduino sends values ​​to the App. Dynamic graph. Shift left.

p10A_OTG_potenciometro.aia (198.8 KB)

In this topic we saw how to send information via Bluetooth and display it in a dynamic graph:
otg28

Now we are going to carry out the same practice but sending the data by a module UART:

I will use CP2102 and an OTG cable.

The version of the SerialOTG extension used is: Data Built: 2021-02-03

- Arduino code:

// Juan A. Villalpando
// http://kio4.com/appinventor/10A_OTG_LCD.htm

#include <SoftwareSerial.h> 
#include <Wire.h> 

SoftwareSerial UART(10, 11) ; // UART(TX,RX)

int value_pot0;

void setup() { 
  Serial.begin(9600);
  UART.begin(9600); 
  Wire.begin();
}

void loop() { 
    value_pot0 = analogRead(A0);
    Serial.println(value_pot0);
    UART.println(value_pot0);
    delay(100); // Debe ser más lento que el Reloj.
} 

Why don't you just answer the request. Where is the .aix file ?

Get com.SerialOTG.aix: