Skip to content

Lesson 06: USB 2.0 Touch Mouse

1. Course Introduction

Convert touchscreen displacement into USB HID mouse movement, allowing the computer to recognize the development board as a mouse and output coordinates in sync.

2. Learning Objectives

After completing this lesson, you should be able to:

  • Understand the difference between the USB 2.0 data port and the UART0 flashing port.
  • Understand Mouse.begin(), USB.begin(), and touch coordinate differencing.
  • Verify HID mouse behavior by touch dragging.

3. What You Need

  • Hardware: one CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display development board (with onboard GT911 touchscreen); two USB data cables that support data transfer (one for flashing and monitoring, one for USB HID connection to the computer); or one data cable that supports simultaneous flashing and HID.
  • Compatibility note: the 5-inch development boards share identical hardware and software code, differing only in board size; please select the appropriate model based on display size and use case.

Code reference link: https://github.com/Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/tree/master/example/V1.0

4. Software Operation Steps

Double-click to open the Lesson 6 code (.ino file).

Open USB2.0 Project

Configure the options as described below.

  • Board: ESP32P4 Dev Module
  • Core Debug Level: Info
  • Flash Frequency / Mode / Size: 80MHz / QIO / 16MB (128Mb)
  • Partition Scheme: 16M Flash (3MB APP/9.9MB FATFS)
  • PSRAM: Enabled
  • USB Mode: Hardware CDC and JTAG
  • Port: After connecting the USB data cable to the development board's UART0, select the newly appeared COM port under "Tools → Port".

Lesson 1 Standard Upload Configuration Screenshot

Follow the library import steps explained in detail in Lesson 1 to import the library files required by this project into the development environment, ensuring that the relevant dependencies can be correctly located during compilation, so that the program runs properly.

P4_Arduino_01_Images_15

5. Hardware Operation Steps

Connect the first USB-C cable that supports data transfer to the development board's UART0 port and the computer, for Arduino program uploading and serial monitoring.

Connect the Development Board to the Computer via UART0

Connect the second USB cable to the development board's USB 2.0 port and the computer. The USB 2.0 port is used to enumerate the HID mouse device; do not confuse it with the UART0 upload port.

Connect the USB 2.0 Port to the Computer

Click Upload in the Arduino IDE. After the program finishes uploading and the board resets, the computer should prompt that a new device has been found, and an HID Mouse should appear in Device Manager.

Confirm the Advance-P4 HID Mouse Device

Slide your finger across the development board screen; the computer mouse pointer should follow the movement, and the Arduino Serial Monitor should simultaneously output the corresponding coordinates.

For details on how to open the Serial Monitor and set the baud rate, please refer to Lesson 1.

Slide the Screen to Control the Computer Mouse

image-20260730101916605

If the computer does not recognize the HID device, check whether the USB 2.0 data cable supports data transfer, and confirm that the program has been uploaded to the COM port corresponding to UART0.

6. Key Code Explanation

6.1 Starting the USB HID

Serial.begin(115200);
Mouse.begin();
USB.begin();

setup() starts the serial monitor, calls Mouse.begin() to initialize the HID mouse class, and calls USB.begin() to start the USB device stack. After enumeration, the host can receive mouse reports from the board.

6.2 Coordinate Differencing and Button State

 int xDistance = (point[0].x - prev_x);
 int yDistance = (point[0].y - prev_y);
 // if X or Y is non-zero, move:
 if ((xDistance!=0) || (yDistance!=0)) {
    Mouse.move(xDistance, yDistance, 0);
    Serial.printf("Mouse.move: x = %2d, y = %2d\r\n", xDistance, yDistance);
                }

The loop computes xDistance and yDistance by subtracting the previous touch coordinates from the current point. Non-zero differences are sent through Mouse.move(). While a touch is active the code presses MOUSE_LEFT, producing a drag; on release it resets the previous coordinates and calls Mouse.release().

7. Experimental Observations

  • The computer recognizes a new USB mouse; while the screen is held and dragged, the pointer moves, and the left button is released after release.

8. FAQ and Troubleshooting

  • If the computer does not recognize the device, use a USB cable that supports data transfer and check the USB 2.0 port.
  • If the pointer jumps, check the touch reading frequency and the initialization of prev_x/prev_y.