SPI problem with Bitcraze PMW3901 sensors and Arduino NANO

Hello everybody!

I am currently trying to connect four Bitcraze PMW3901 sensors to an Arduino NANO, but I seem to have issues with the SPI. When I connect two sensors using the code below, it works perfectly: I am able to retrieve the X and Y data from the serial port for both sensors.

include "Bitcraze_PMW3901.h"

 Bitcraze_PMW3901 flow(10);
 Bitcraze_PMW3901 flow2(9);

void setup() {
  Serial.begin(9600);

  if (!flow.begin()) {
    Serial.println("Initialization of the flow sensor 1 failed");
    while(1) { }
  }

  if (!flow2.begin()) {
    Serial.println("Initialization of the flow sensor 2 failed");
    while(1) { }
  }

}

int i =0;
int16_t deltaX,deltaY;
int16_t deltaX2,deltaY2;

void loop() {
  if ((i % 2) == 0) {
    digitalWrite(10, LOW); // sets the digital pin 10 on
    flow.readMotionCount(&deltaX, &deltaY);
    digitalWrite(10, HIGH);  // sets the digital pin 9 off
  }

  if ((i % 2) == 1) {
    digitalWrite(9, LOW); // sets the digital pin 10 on
    flow2.readMotionCount(&deltaX2, &deltaY2);
    digitalWrite(9, HIGH);  // sets the digital pin 9 off
  }

    i = i + 1;
    Serial.print("X1:");
    Serial.print(deltaX);
    Serial.print("\t");
    Serial.print("Y1:");
    Serial.print(deltaY);
    Serial.print("\t");
    Serial.print("X2: ");
    Serial.print(deltaX2);
    Serial.print("\t");
    Serial.print("Y2:");
    Serial.print(deltaY2);
    Serial.print("\n");

}

However, when I try to add a third Bitcraze PMW3901 sensor I cannot seem to be able to initilize it. Still, I declare a separete SS as

Bitcraze_PMW3901 flow3(6);

and add the initialization code in the setup() function as follows

if (!flow3.begin()) {
Serial.println("Initialization of the flow sensor 3 failed");
while(1) { }
}

What I get is the sentence "Initialization of the flow sensor 3 failed" or "Initialization of the flow sensor 1 failed" (it depends and I don't really understand why, since the code is always the same) printed on the serial port window.

I have already checked my sensors: they all work separetely and two-by-two. Do you have any suggestions? I hope I have managed to explain the problem clearly, otherwise please let me know if I can add or specify something more. Thank you in advance for your help!

1 Like

What results do you get if you connect the third sensor but use the code for two sensors (just pull the SS pin of the third sensor high)? Does that work? If that doesn't work correctly too, your wiring is wrong, probably too long bus wires. How long is the complete bus (all interconnecting wires together for p.e. MOSI)?

Hi, I have been facing the same problem with my sensors. I am using teensy 4.0 with teensyduino.
In my case, the sensors work fine separately but when I connect 2 or more sensors to the microcontroller, the initialisation fails.

@pylon The complete bus is (45+45+15+15+20) cm = 140 cm long. SPI connection limits are between 5-10 m so I don't think long bus wires should be a problem. But I am not sure.

This is the sensor that I am currently using

However, when I use pmw3901 form another manufacturer (https://www.tindie.com/products/onehorse/pmw3901-optical-flow-sensor/), the problem is solved. i.e this sensor which is smaller works fine separately and in a group of 4.

Please could someone suggest what might be causing the problematic sensors to behave in this way?

Wrong, for a 4MHz bus frequency the maximum wire length is about 50cm, so your wires are too long.

I found the schematics of the Bitcraze board and unfortunately the board designer doesn't know how to build a correct SPI level converter. The MISO line is constantly pulled low so you cannot connect any other device to the same SPI bus if this board is connected.

IIt would have been quite easy to change that: Just don't connect pin 12 of U1 not to pin 1 but by a voltage divider to pin 10.

Hello,
can you tell me, which library you use for this sensor shown in the image? Can I use the bitcraze library for this sensor too?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.