VL53L0X ToF sensor - Low memory problem

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
lyehezkel
 
Posts: 1
Joined: Thu Sep 22, 2022 9:13 am

VL53L0X ToF sensor - Low memory problem

Post by lyehezkel »

hi
Im using 3 VL53L0X sensors with the arduino uno R3, and the code that i use is the following:

Code: Select all

#include "Adafruit_VL53L0X.h"

// address we will assign for all 4 sensor
#define LOX1_ADDRESS 0x30
#define LOX2_ADDRESS 0x31
#define LOX3_ADDRESS 0x32

// set the pins to shutdown for all 4 sensors
#define SHT_LOX1 2
#define SHT_LOX2 3
#define SHT_LOX3 4


// objects for the vl53l0x
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox3 = Adafruit_VL53L0X();

void setID() {

  //Serial.print(F("brake 1, "));
  
  // all unreset
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, HIGH);
  digitalWrite(SHT_LOX3, HIGH); 
  delay(10);
  //Serial.print(F("brake 2, "));
  
  // activating LOX1 and reseting LOX2,LOX3
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, LOW);
  digitalWrite(SHT_LOX3, LOW);
  delay(10);
  Serial.print(F("brake 3, "));
  delay(1000);
  Serial.print(F("brake 3, "));
  // initing LOX1
  if(!lox1.begin(LOX1_ADDRESS)) {
    Serial.print(F("brake 3.1, "));
    Serial.println(F("Failed to boot first VL53L0X"));
    delay(500);
    while(1);
  }
  delay(10);
  Serial.print(F("brake 4, "));
  
  ///************************* sensor 2 activation 
  // activating LOX2
  digitalWrite(SHT_LOX2, HIGH);
  delay(10);
  Serial.print(F("brake 5, "));

  //initing LOX2
  if(!lox2.begin(LOX2_ADDRESS)) {
    Serial.println(F("Failed to boot second VL53L0X"));
    while(1);
  }
  delay(10);
  Serial.print(F("brake 6, "));
   ///************************* sensor3 activation  
  // activating LOX3
  digitalWrite(SHT_LOX3, HIGH);
  delay(10);

  //initing LOX3
  if(!lox3.begin(LOX3_ADDRESS)) {
    Serial.println(F("Failed to boot second VL53L0X"));
    while(1);
  }  
  
}

void read_quad_sensors() {
  
  // this holds the measurement
  VL53L0X_RangingMeasurementData_t measure1;
  VL53L0X_RangingMeasurementData_t measure2;
  VL53L0X_RangingMeasurementData_t measure3;
  int sensor1,sensor2,sensor3;
  
  lox1.rangingTest(&measure1, false); // pass in 'true' to get debug data printout!
  lox2.rangingTest(&measure2, false); // pass in 'true' to get debug data printout!
  lox3.rangingTest(&measure3, false); // pass in 'true' to get debug data printout!
  
  // print sensor one reading
  Serial.print(F("1: "));
  if(measure1.RangeStatus != 4) {     // if not out of range
    sensor1 = measure1.RangeMilliMeter;    
    Serial.print(sensor1);
    Serial.print(F("mm"));    
  } else {
    Serial.print(F("Out of range"));
  }
  
  Serial.print(F(" "));

  // print sensor two reading
  Serial.print(F("2: "));
  if(measure2.RangeStatus != 4) {
    sensor2 = measure2.RangeMilliMeter;
    Serial.print(sensor2);
    Serial.print(F("mm"));
  } else {
    Serial.print(F("Out of range"));
  }
 
   Serial.print(F(" "));

  Serial.print(F("3: "));
  if(measure3.RangeStatus != 4) {
    sensor3 = measure3.RangeMilliMeter;
    Serial.print(sensor3);
    Serial.print(F("mm"));
  } else {
    Serial.print(F("Out of range"));
  }

  Serial.println();
}

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

  // wait until serial port opens for native USB devices
  while (! Serial) { delay(1); }

  pinMode(SHT_LOX1, OUTPUT);
  pinMode(SHT_LOX2, OUTPUT);
  pinMode(SHT_LOX3, OUTPUT);

  Serial.print(F("Shutdown pins inited..."));

  digitalWrite(SHT_LOX1, LOW);
  digitalWrite(SHT_LOX2, LOW);
  digitalWrite(SHT_LOX3, LOW);

  Serial.print(F("All three in reset mode...(pins are low) "));
  
  Serial.println("Starting...");
  setID();
}

void loop() {
   
  read_quad_sensors();
  delay(100);
}
the global memory is nearly at his maximum (99%). and i have tried to use this code without setID() and got to 81% use of global memory.
my problem is that when im using 2 sensors it works just fine. and when im using 3 sensors it doesnt print "brake 4" or even "brake 3.1" and starting the setup all over again.
i would much appreciate if someone can please help me.
and i would to thank Robojax.com for the template of this code
Last edited by dastels on Thu Sep 22, 2022 9:40 am, edited 1 time in total.
Reason: Add code tags

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: VL53L0X ToF sensor - Low memory problem

Post by dastels »

It would appear that you don't have enough RAM to do the required dynamic allocations. The ATmega328 on the UNO only has 2K of RAM. I suggest you upgrade to something more capable. I.e. something with more RAM. If you want to keep the UNO footprint, one of the Metro boards would be a natural upgrade...just not one of the "328" models. The Metro M0 Express would be a good choice https://www.adafruit.com/product/3505. The code would work as-is.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”