STM32 Bluepill I2C error when using both stepper motor and I2C device

Hi, I am working on STM32F103 Bluepill with STM32duino Official Core trying to read data from VL53L1X (communication using I2C) and Stepper Motor NEMA 17 (DRV8825).
When I tried to run both stepper motor and I2C sensor, the sensor became "Timeout". This is not happened when I run stepper motor and I2C sensor separately.
Has anyone ever happened to this?
Here is my program and some screenshot of the circuit.

Sorry for my bad English.

#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;
  }
}


According to the image you use PB8 PB9 as SCL & SDA.
if I'm not mistaken, the bluepil has a standard location i2c on PB6 & PB7

I have also tried to move the i2c to PB6 & PB7, but the error still happens.

I am using two I2C sensor, VL53L1X and LSM303C.
I tried to use each sensor with stepper motor, but the sensor still "timeout" after a few second.

I have a thought that this is all about the I2C on the microcontroller itself. But I am still hesitant about it.

Sorry for my bad English.

I think you should first managed to work your sensors without stepper. if the sensors will work, at least you will be sure that the connection is correct.
Have you tried running an i2c scanner?

I have also try the sensor without stepper. It worked smoothly.
When I connect the 12V from the battery to the stepper motor driver (DRV8825), the sensor became "timeout".

Yes, i have tried modified i2c scanner with stepper program. The bluepill could not detect any I2C device when stepper was running.

So you probably has issue with power lines. Your schematic is not clear about it. How is your STM board powered? Are the motors and board has power sources, related to each other?

And what about the sensors - do you power it from 3.3v or 5v? Are you use pullup resistors on SCL SDA lines? Again, to which voltage - 3.3 or 5v?

The bluepill board is powered using usb and the stepper motor is powered using 12v battery.
The circuit has separate line for power, but has common ground.

I don't use any pullup resistor on I2C line.
Let me try it first, then i would inform you.
Thank you.

If you have a multimeter, it will useful to measure a VCC voltage on the sensor with and without motor power connection.

The voltage on VCC pin is 3.306v when the stepper didn't connected to the microcontroller/battery didn't connected. The voltage is still at 3.306v when the system run for a few second untill timeout happened.
While the voltage is 3.310v when the "timeout" is occured.

You have the same text in two different debug statements - not useful. But one accidentally is preceded by a space, it is in loop(). The other is in the setup(), it has no space and is the one that is printed. Please change one of them and re-run the sketch to see which one is being printed. I suspect it is the first one since the space is not printed. That means the board is resetting itself constantly. Your code never reaches loop(). Please change the debug statement and confirm:

  if (sensor.init() == false) {
    Serial.println("INIT FAILED");
 ...
void loop() {
  if (sensor.timeoutOccurred()) {
    Serial.print("LOOP TIMEOUT");

Actually the first one has no timeout so it's badly named!

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