Help Needed: ESP32 with ADS9500 Optical Mouse Sensor - Movement Detection Issues

Hi all,

I am extremely new to programming. When I say new, I mean I have no experience at all. I have what I believed was a simple project, which is to use an ESP32 in conjunction with an ADS9500 optical mouse sensor. I would simply like the ESP32 to detect movement from the mouse sensor.

To do this, I have been using ChatGPT for assistance, but I am having problems. Please see my code below. Is there anyone out there who can offer some advice or assistance as to what might be going wrong? The sensor, i.e., the ADS9500, is detected by the ESP32, but it doesn't detect movement. Any help would be appreciated.

#include <SPI.h>

// Define the pins for the ADNS 9500 sensor
#define ADNS_SCLK   18
#define ADNS_MISO   19
#define ADNS_MOSI   23
#define ADNS_NCS    5
#define ADNS_MOTION 4

// ADNS-9500 registers
#define ADNS9500_PRODUCT_ID        0x00
#define ADNS9500_MOTION            0x02
#define ADNS9500_DELTA_X           0x03
#define ADNS9500_DELTA_Y           0x04
#define ADNS9500_LASER_CTRL0       0x20

// Motion threshold to interpret as a "click"
#define MOTION_THRESHOLD 10

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  
  // Setup the SPI
  SPI.begin(ADNS_SCLK, ADNS_MISO, ADNS_MOSI, ADNS_NCS);
  
  // Set the chip select pin as output
  pinMode(ADNS_NCS, OUTPUT);
  digitalWrite(ADNS_NCS, HIGH);

  // Set the motion pin as input
  pinMode(ADNS_MOTION, INPUT);

  // Give time for the sensor to initialize
  delay(100);
  
  // Check the sensor ID
  uint8_t productID = readRegister(ADNS9500_PRODUCT_ID);
  Serial.print("ADNS-9500 Product ID: 0x");
  Serial.println(productID, HEX);
  
  if (productID != 0x33) {
    Serial.println("Failed to connect to ADNS-9500 sensor");
    while (1); // Stay here if sensor initialization fails
  } else {
    Serial.println("Successfully connected to ADNS-9500 sensor");
  }

  // Enable the laser
  enableLaser();
}

void loop() {
  // Check if the motion pin is active
  if (digitalRead(ADNS_MOTION) == HIGH) {
    // Read motion data
    int8_t deltaX = (int8_t)readRegister(ADNS9500_DELTA_X);
    int8_t deltaY = (int8_t)readRegister(ADNS9500_DELTA_Y);

    // Interpret motion as clicks
    if (abs(deltaX) > MOTION_THRESHOLD || abs(deltaY) > MOTION_THRESHOLD) {
      Serial.println("Click detected!");
    }
  } else {
    // Read the motion register to see if motion is detected
    uint8_t motion = readRegister(ADNS9500_MOTION);
    if (motion & 0x80) { // Check if the MOT bit is set
      int8_t deltaX = (int8_t)readRegister(ADNS9500_DELTA_X);
      int8_t deltaY = (int8_t)readRegister(ADNS9500_DELTA_Y);

      // Interpret motion as clicks
      if (abs(deltaX) > MOTION_THRESHOLD || abs(deltaY) > MOTION_THRESHOLD) {
        Serial.println("Click detected!");
      }
    }
  }

  delay(100);
}

uint8_t readRegister(uint8_t reg) {
  digitalWrite(ADNS_NCS, LOW);
  SPI.beginTransaction(SPISettings(50000, MSBFIRST, SPI_MODE3)); // Reduced speed and changed mode
  SPI.transfer(reg & 0x7F);  // MSB = 0 for read operation
  delayMicroseconds(50);     // T_SRAD
  uint8_t value = SPI.transfer(0x00);
  digitalWrite(ADNS_NCS, HIGH);
  SPI.endTransaction();
  delayMicroseconds(5);      // T_SCLK-NCS for read operation
  return value;
}

void writeRegister(uint8_t reg, uint8_t value) {
  digitalWrite(ADNS_NCS, LOW);
  SPI.beginTransaction(SPISettings(50000, MSBFIRST, SPI_MODE3)); // Reduced speed and changed mode
  SPI.transfer(reg | 0x80);  // MSB = 1 for write operation
  SPI.transfer(value);
  digitalWrite(ADNS_NCS, HIGH);
  SPI.endTransaction();
  delayMicroseconds(20);     // T_SCLK-NCS for write operation
}

void enableLaser() {
  uint8_t laserCtrl0 = readRegister(ADNS9500_LASER_CTRL0);
  // Clear the Force_Disabled bit to enable the laser
  laserCtrl0 &= ~0x01;
  writeRegister(ADNS9500_LASER_CTRL0, laserCtrl0);
}

Hi!
You should check this library:

Thank you for your reply. I did download the file from GitHub. I’m really new to this. I was able to upload it to the ESP 32. WROOM Using Arduino IDE. The ADNS-9500 Sensor is detected, however, it’s not able to detect movement for some strange reason.