Ps2 mouse library doesn't work on esp32

Hi, I am trying to use the optical sensor on a cheap mouse to track movement. I found this ps2 mouse library, and my esp32 only spits out gibberish through Serial. Below is the example code. I tested it on my Arduino Uno and it worked fine. What's the problem here?

Some additional things I found:

  • The mouse LED only lights up if I use the pins 5 and 6.
  • Baud rate doesn't matter. It always works on Uno and always doesn't work on esp32.
  • PS2 mouse(CLOCK_PIN, DATA_PIN); seems to be causing the problem. With this line, any serial output is gibberish. Without it, serial works.
  • Mouse LED brightens slightly if it detects any movement(Both on Uno and esp32)
#include "PS2Mouse.h"
#define DATA_PIN 5
#define CLOCK_PIN 6

PS2Mouse mouse(CLOCK_PIN, DATA_PIN);

void setup() {
  Serial.begin(38400);
  mouse.initialize();
}

void loop() {
    MouseData data = mouse.readData();
    Serial.print(data.status, BIN);
    Serial.print("\tx=");
    Serial.print(data.position.x);
    Serial.print("\ty=");
    Serial.print(data.position.y);
    Serial.print("\twheel=");
    Serial.print(data.wheel);
    Serial.println();
    delay(20);
}

Try pins 0 & 19.

Didn't work. 19 & 0 didn't work too. Although the LED did light up.

What mouse are you using?

I'm using the Amazon Basics 3-button mouse. Apparently the sensor in this thing supports both USB and PS/2

Do you have usb to ps2 adapter?

So you can try a keyoard. :thinking:

Unfortunately I don't. I also doubt that the library supports keyboards

Can you take a photo of the mouse so I can see?

Does the mouse work on your Uno?

Use 2 & 3 for these with the Uno.

It did work on my Uno with pins 5 and 6. So I don't think it's a problem with the mouse. The library also recommends using 5 and 6 for Arduinos.

Won't work on esp32 as it can't handle interrupts on this pin.
Try any other pins except for 6 to 11.

It definitely did something because now I am getting some signals regularly. But it still looks like this

If you set baud rate in serial monitor to 9600, what happens?

The same thing happens. But each individual output seems to be shorter

Try adding a 100 ohm to 1k resistor on the data line.

My esp32 works with the 100 ohm with a ps2 keyboard.

I don't have any resistors right now so I will try it next week and give you the results.

I will be, and waiting thanks for your patience!
:wink:

Thanks.

The resistor didn't work. What specific function from pin 5 is being used? The other pins don't seem to do the job. The sensor doesn't even light up.