Mouseproject with bluepill

First post here,
I am currently trying to build my own mouse, it is my first Arduino project, I know, this is a silly starter project, but that is what I am currently interested in. So I bought an STM32F103C8T6 (bluepill), the Sensor, I’m using is the PMW3389 (, which I got from a broken mouse, but the sensor is still fine. As the first step, I wanted to make receive the dato from the sensor on my bluepill and then send it to my computer (just a simple USB HID). However, I don’t know how to approach this. The hardware part is not a problem for me, but I simply don’t know what to do about the code.


#include <USBComposite.h> 

const uint8_t reportDescription[] = {

HIDAbsMouse mouse(HID); //see assets

int b1; //button1...
int b2;
int b3;
int x = 0; //delta x
int y = 0; //delta y

byte receivedVal; //SPI...
byte val;
byte Mastereceive;
byte Mastersend;
byte incoming; // = shiftIn(dataPin, clockPin, bitOrder)

PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x02,                    // USAGE (Mouse)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x09, 0x01,                    //   USAGE (Pointer)
    0xA1, 0x00,                    //   COLLECTION (Physical)
    0x05, 0x09,                    //     USAGE_PAGE (Button)
    0x19, 0x01,                    //     USAGE_MINIMUM
    0x29, 0x03,                    //     USAGE_MAXIMUM
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    0x95, 0x03,                    //     REPORT_COUNT (3)
    0x75, 0x01,                    //     REPORT_SIZE (1)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    0x95, 0x01,                    //     REPORT_COUNT (1)
    0x75, 0x05,                    //     REPORT_SIZE (5)
    0x81, 0x03,                    //     INPUT (Const,Var,Abs)
    0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
    0x09, 0x30,                    //     USAGE (X)
    0x09, 0x31,                    //     USAGE (Y)
    0x09, 0x38,                    //     USAGE (Wheel)
    0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
    0x25, 0x7F,                    //     LOGICAL_MAXIMUM (127)
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x03,                    //     REPORT_COUNT (3)
    0x81, 0x06,                    //     INPUT (Data,Var,Rel)
    0xC0,                          //   END_COLLECTION
    0xC0,                          // END COLLECTION

void setup() {
  Serial.begin(115200); // nötig?
  pinMode(PA7,OUTPUT); //MOSI
  pinMode(PA6, INPUT); //MISO
  pinMode(PA5, OUTPUT); //SCK
  pinMode(PA4, OUTPUT); //SS
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SCK, OUTPUT);
  pinMode(SS, OUTPUT);
  pinMode(PB3,INPUT); //Button1
  pinMode(PB4,INPUT); //Button2
  pinMode(PB5,INPUT); //Button3 (mitte)
  digitalWrite(SS, HIGH);

  SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
  HID.begin(reportDescription, sizeof(reportDescription));

  digitalWrite(SS, LOW); 

void loop() { 
  char COMMAND;
  receivedVal = SPI.transfer(val);             //one of them is sufficient?
  Mastereceive = SPI.transfer(Mastersend); 
  incoming = shiftIn(MISO, SCK, MSBFIRST);

  digitalWrite(SS, LOW);  // Select the Slave device
  byte returnedValue = SPI.transfer(0);

  mouse.move(x, y);
  if (PB3){;
    if (!PB3){


As one might see, this is a mess and for sure doesn’t work, these are only snippets from the internet, but I have no idea how to put them together.
Any help with the coding would be appreciated!

Kind Regards Lukas

First thing you need to do is determine whether you want to use SPI.transfer() OR shiftOut()/shiftIn() to send/receive information from the SPI device. You do NOT use both.

It is useless to pretend to be a mouse until you are absolutely certain that you are getting data from the device that means something useful.