Activating step motor with VL53L0X sensor

I am learning to creat a device that can push the toothpaste. My idea is to use VL53L0X sensor and step motor. When the sensor detect something within 2cm, then it will activate the step motor to rotate for once. But I have went through some problem:

Compilation error: 'VL53L0X_RangingMeasurementData_t' does not name a type

I have already install all the things I can find in the Library for VL53L0X. I restart the computer and reopen the program. And it doesn't work.

#include <Wire.h>
#include <VL53L0X.h>
#include <Stepper.h>

VL53L0X sensor;
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution of your stepper motor
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // initialize the stepper motor

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  sensor.init();
  sensor.setTimeout(500);
  myStepper.setSpeed(60); // set the speed of the stepper motor to 60 RPM
}

void loop()
{
  VL53L0X_RangingMeasurementData_t measure;
  sensor.rangingTest(&measure, false);
  if (measure.RangeMilliMeter <= 20) // if an object is detected within 2cm
  {
    myStepper.step(stepsPerRevolution); // rotate the stepper motor once
    delay(1000); // wait for 1 second before taking another measurement
  }
}

Where did you find the library that you have installed for the VL53L0x. Please provide a link. Did you install the library using the IDE library manager?

Please include the entire error message. If you use the Arduino IDE, it is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information. If not using the IDE, copy and paste the entire error message.

Here is a quote from the Pololu site and their VL53L0X nodule page.

  • The datasheet does not specify a minimum range, but in our experience, the effective limit is about 3 cm.

The 3cm figure is backed up with a search for "vl53l0x minimum distance"

Thanks, I have fix the problem now. This is the new code:

#include <Wire.h>
#include <VL53L0X.h>
#include <Stepper.h>

VL53L0X mySensor;  // Create a named VL53L0X object
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution of your stepper motor
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // initialize the stepper motor

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mySensor.init();  // Use the named object to initialize the sensor
  mySensor.setTimeout(500);  // Use the named object to set the timeout for the sensor
  myStepper.setSpeed(60); // set the speed of the stepper motor to 60 RPM
}

void loop() {
  uint16_t distance = mySensor.readRangeSingleMillimeters();  // Use the named object to get the distance measurement
  if (distance <= 20) // if an object is detected within 2cm
  {
    myStepper.step(stepsPerRevolution); // rotate the stepper motor once
    delay(1000); // wait for 1 second before taking another measurement
  }
}

The error message doesn't come out and I upload successfully on the arduino board. But it doesn't work.

uint16_t distance = mySensor.readRangeSingleMillimeters(); 
if (distance <= 20)

Did you see what I posted about the minimum measurement distance of 3cm (30mm)?

Add some serial prints to observe what the measurement function is actually returning.

I still do not know what, exact, library that you are using.

I got the code to work. I used an Uno, 24BYJ stepper with ULN2003 driver and a VL53L0X sensor. I used the Pololu library installed from the IDE library manager.
The main changes are:
You had the speed set way too fast. Those 2048 steps per revolution motors will not go more than about 15RPM.
I set to the continuous measurement mode.
The minimum measuring distance is 30mm. Set to 40 for test.
I inserted serial prints to see the measurement values.

#include <Wire.h>
#include <VL53L0X.h>
#include <Stepper.h>

VL53L0X mySensor;  // Create a named VL53L0X object
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution of your stepper motor
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // initialize the stepper motor

void setup()
{
   Serial.begin(115200);
   Wire.begin();
   mySensor.init();  // Use the named object to initialize the sensor
   mySensor.setTimeout(500);  // Use the named object to set the timeout for the sensor
   myStepper.setSpeed(10); // ********* 60RPM way too fast
   // ********* set the mode 100 measurements per second
   mySensor.startContinuous(100); 
}

void loop()
{
   uint16_t distance = mySensor.readRangeSingleMillimeters();  // Use the named object to get the distance measurement
   Serial.print("distance = ");  // ********** added
   Serial.println(distance); // ********** added
   if (distance <= 40) // ****** 20mm is too close 30 is minimum
   {
      myStepper.step(stepsPerRevolution); // rotate the stepper motor once
      //delay(1000); // wait for 1 second before taking another measurement
   }
}

The Pololu VL53L0X library has examples that will show more about the sensor and how to use it.

Thank you so much!

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