stm32duino / VL53L1X

Arduino library to support the VL53L1X Time-of-Flight and gesture-detection sensor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VL53L1X sensor "Timeout" when using stepper motor and the sensor simultaneously.

mluthfialhadi opened this issue · comments

Hi, I am working on STM32F1 Bluepill with VL53L1X and Stepper Motor NEMA 17 (DRV8825).
Has anyone know why my VL53L1X became "Timeout" when I use both stepper motor and sensor?
I could use the sensor or the stepper separately.

Sorry for my bad English.

Here is my code and screenshot of the circuit.
skematik fritzing
ss_serial_monitor

#define DIR PA8
#define STEP PB15
#include <Wire.h>
#include <VL53L1X.h>
#include <SparkFunIMU.h>
#include <SparkFunLSM303C.h>
#include <LSM303CTypes.h>

LSM303C myIMU;
VL53L1X sensor;
long jarak = 0;
int sudut = 0, angle = 0;
float totalx = 0, totaly = 0, totalz = 0, xmag = 0, ymag = 0, zmag = 0;
int steps = 0, stepmaks = 800;
unsigned long tbefore = 0;
boolean cond = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(DIR, OUTPUT);
  pinMode(STEP, OUTPUT);
  //while (!Serial) {}
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C
  sensor.setTimeout(1000);
  if (myIMU.begin() != IMU_SUCCESS) {
    while (1) {
      Serial.println("Failed to detect and initialize sensor!");
    }
  }
  if (sensor.init() == false) {
    Serial.println("TIMEOUT");
  } else {
    sensor.setDistanceMode(VL53L1X::Long);
    sensor.setMeasurementTimingBudget(200000);
    sensor.startContinuous(50);
  }
  int langkah = 0;
  //float angle=0;
  boolean kon = 0;
  while (xmag > 0.002) {
    kon = !kon;
    digitalWrite(STEP, kon);
    if (kon == 0) {
      langkah = langkah + 1;
      angle = map(langkah, 0, stepmaks, 0, 360);
    }
    xmag = myIMU.readMagX();
    // Serial.print("\nMagnetometer:\n");
    // Serial.print(" X = ");
    // Serial.println(xmag, 4);
    delay(10);
  }
}
void loop() {
  if (sensor.timeoutOccurred()) {
    Serial.print(" TIMEOUT");
  }
  xmag = myIMU.readMagX();
  if (micros() - tbefore >= 50000) {
    tbefore = micros();
    steps += 1;
    cond = !cond;
    digitalWrite(STEP, cond);
    if (cond == 0) {
      jarak = sensor.read();
      sudut = map(steps, 0, stepmaks, 0, 359);
      if (sudut > 359) sudut = 0;
      String dt = "<";
      dt += String(jarak);
      dt += "+";
      dt += String(sudut);
      dt += ">";
      Serial.println(dt);
    }
  }
  if (steps > stepmaks) {
    steps = 0;
  }
}

Hello @mluthfialhadi ,
it seems that you are not using correctly the STM32duino VL53L1X library. If you give a look at the VL53L1X constructor, you need to pass the I2C instance and the shutdown pin as parameters. In your sketch I cannot see it, because you write:

VL53L1X sensor;

Instead, I expect something like:

VL53L1X sensor(&Wire, <your_shutdown_pin>);

I cannot see in your code also the "sensor.begin()" after the "Wire.setClock(400000);"

Best Regards,
Carlo