The objective of this week is to learn how to interact with a microcontroller via a Bluetooth connection through a mobile application. Its interface and functionality will be designed using online assistants.
It is necessary to compare different app design assistants, so after a selection process, MIT App Inventor is chosen due to its extensive documentation, free access, and ease of programming. An alternative option, such as Teta, is also reviewed.
MIT App Inventor is a free, web-based platform that allows users to create mobile applications using a visual, block-based programming language. It is designed to be accessible for beginners and widely used in education.
Teta is a visual app builder that allows users to design and develop mobile applications without coding. It offers real-time collaboration, custom components, and integration with databases, making it suitable for designers and developers.
This week’s workflow will consist of a simple exercise: controlling an SG90 servo motor through an Android app designed using an assistant. An ESP32 microcontroller will be used due to its built-in Bluetooth capability.
1. As part of the research, a comparison will be made between Blynk, MIT App Inventor, and Teta, focusing on their advantages, disadvantages, and connectivity with microcontrollers. MIT App Inventor is selected due to its free access, versatility, ease of installation, and Bluetooth compatibility with the XIAO Seeed and ESP-32.
2. The exercise consists of creating an interface that allows control of a low-torque servo motor by rotating it 5 degrees to the left or right using two buttons located on the application interface. To achieve this, it is necessary to become familiar with the two environments of MIT App Inventor. First, the user interface will be defined by dragging and configuring both visible and invisible elements.
3. The next step is to understand the visual programming system, which is composed of blocks where actions are nested. Most of the programming focuses on recognizing Bluetooth-connected devices and selecting the appropriate one.
4. After saving the file, the project is compiled, presenting two installation options for the mobile device: via QR code or numeric code. The QR code option is selected.
5. In parallel, a basic movement code for the servomotor is designed. The Bluetooth network is named "ESP32_Servo", and the movement is controlled using conditional statements that check if the received character from the labels is "L" (left) or "R" (right).
#include // Objeto para Bluetooth clásico BluetoothSerial SerialBT; // Definiciones para el servo #define SERVO_PIN 13 // Pin GPIO13 para la señal del servo #define MIN_PULSE_WIDTH 500 // Ancho de pulso mínimo (0 grados, µs) #define MAX_PULSE_WIDTH 2500 // Ancho de pulso máximo (180 grados, µs) #define PWM_PERIOD 20000 // Período PWM (20 ms, 50 Hz) #define PULSES_PER_MOVE 5 // Pulsos por movimiento para estabilidad // Variable global para el ángulo actual int currentAngle = 90; // Ángulo inicial del servo (90 grados) // Función para convertir ángulo a ancho de pulso int angleToPulseWidth(int angle) { // Mapea el ángulo (0-180) al ancho de pulso (500-2500 µs) return map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); } // Función para enviar un pulso PWM al servo void sendPulse(int pulseWidth) { digitalWrite(SERVO_PIN, HIGH); delayMicroseconds(pulseWidth); digitalWrite(SERVO_PIN, LOW); delayMicroseconds(PWM_PERIOD - pulseWidth); } void setup() { // Configurar el pin del servo pinMode(SERVO_PIN, OUTPUT); // Iniciar comunicación serial para depuración Serial.begin(115200); Serial.println("Iniciando ESP32 Bluetooth Servo Control"); // Iniciar Bluetooth clásico SerialBT.begin("ESP32_Servo"); // Nombre del dispositivo Bluetooth Serial.println("Bluetooth iniciado, esperando conexión..."); // Inicializar el servo en 90 grados int pulseWidth = angleToPulseWidth(currentAngle); for (int i = 0; i < 50; i++) { // Enviar pulsos iniciales para estabilizar sendPulse(pulseWidth); } Serial.println("Servo inicializado en 90 grados"); } void loop() { // Verificar si hay datos disponibles en Bluetooth if (SerialBT.available()) { char command = SerialBT.read(); Serial.print("Comando recibido: "); Serial.println(command); // Procesar comandos L o R if (command == 'L' && currentAngle > 0) { currentAngle -= 5; // Mover 5 grados a la izquierda Serial.print("Nuevo ángulo: "); Serial.println(currentAngle); } else if (command == 'R' && currentAngle < 180) { currentAngle += 5; // Mover 5 grados a la derecha Serial.print("Nuevo ángulo: "); Serial.println(currentAngle); } // Mover el servo al nuevo ángulo int pulseWidth = angleToPulseWidth(currentAngle); for (int i = 0; i < PULSES_PER_MOVE; i++) { sendPulse(pulseWidth); } } // Mantener la posición del servo int pulseWidth = angleToPulseWidth(currentAngle); sendPulse(pulseWidth); delay(20); // Aproximadamente 50 Hz }
6. We evaluate the connection functionality between the Android application and the microcontroller, as reflected in the label message.
7. We created a preliminary circuit design in Wokwi and prepared the hardware for testing.
8. Finally, the code is tested together with the hardware to verify proper operation.
9. The ESP32 is assembled with the custom-designed board for the final project, and the servo motor is also connected.
Arduino Script App .apk file