0

I am using 4 Pololu VL6180X ToF sensors on the Arduino UNO for a school project but just can't seem to get it working correctly. I understand that I need to hold them in reset and change the I2C address of each sensor after waking them up, but the code I have doesn't work. Is the problem some flawed understanding of the C language?

This is the .h file I wrote (distSensors.h):

/*
This module is used to interface with the distance sensors.
*/
#ifndef DISTSENSORS_FILE
#define DISTSENSORS_FILE

#include "vl6180x/VL6180X.h"
#include "globals.h"

// pins connected to VL6180 GPIO used to hold sensor in reset
#define NORTH_SHUTDOWN 13
#define EAST_SHUTDOWN 1
#define WEST_SHUTDOWN 0

// threshold distance for detectWall (mm)
double const WALL = 70;

// initializes sensor
void initSensor(VL6180X* sensor) {
  (*sensor).init();
  (*sensor).configureDefault();
  (*sensor).setTimeout(500);
}

void changeAddress(VL6180X* sensor, int address) {
  (*sensor).setAddress(address);  
}

// sensors
VL6180X sensorNorth;
VL6180X sensorSouth;
VL6180X sensorWest;
VL6180X sensorEast;

void setupSensors() {
  // shutdown pins
  pinMode(NORTH_SHUTDOWN, OUTPUT);
  pinMode(WEST_SHUTDOWN, OUTPUT);
  pinMode(EAST_SHUTDOWN, OUTPUT);

  // hold each sensor in shutdown initially
  digitalWrite(NORTH_SHUTDOWN, LOW);
  digitalWrite(WEST_SHUTDOWN, LOW);
  digitalWrite(EAST_SHUTDOWN, LOW);
  delay(10);

  digitalWrite(NORTH_SHUTDOWN, HIGH);
  digitalWrite(WEST_SHUTDOWN, HIGH);
  digitalWrite(EAST_SHUTDOWN, HIGH);

  digitalWrite(NORTH_SHUTDOWN, LOW);
  digitalWrite(WEST_SHUTDOWN, LOW);
  digitalWrite(EAST_SHUTDOWN, LOW);

  // wake sensors and change address one by one
  // south is never shutdown, and its address is changed first
  changeAddress(&sensorSouth, 0x23);
  delay(10);

  digitalWrite(WEST_SHUTDOWN, HIGH);
  changeAddress(&sensorWest,0x25);
  delay(10);

  digitalWrite(EAST_SHUTDOWN, HIGH);
  changeAddress(&sensorEast, 0x27);
  delay(10);

  digitalWrite(NORTH_SHUTDOWN, HIGH); // NORTH maintains default address

  // init all sensors
  initSensor(&sensorNorth);
  initSensor(&sensorSouth);
  initSensor(&sensorWest);
  initSensor(&sensorEast);
}

// Return the distance reading from the given direction
double getDist(int direction) {
  switch (direction) {
  case NORTH:
    return sensorNorth.readRangeSingleMillimeters();
  case SOUTH:
    return sensorSouth.readRangeSingleMillimeters();
  case WEST:
    return sensorWest.readRangeSingleMillimeters();
  case EAST:
    return sensorEast.readRangeSingleMillimeters();
  }
}

// return 1 if there is a wall, 0 if there is not a wall
int detectWall(int direction) {
  if (getDist(direction) <= WALL) { return 1; }
  else { return 0; }
}

#endif

And this is the test code I'm running:

#include "distSensors.h"
#include "Wire.h"
void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  setupSensors();
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("NORTH: "); Serial.println(getDist(NORTH)); 
  Serial.print("SOUTH: "); Serial.println(getDist(SOUTH));
  Serial.print("WEST: "); Serial.println(getDist(WEST));
  Serial.print("EAST: "); Serial.println(getDist(EAST));
  delay(100);
}

getDist returns 0.0 for every sensor except for sensorSouth.

Any help would be greatly appreciated, thanks!

  • When coming out of hardware standby (reset) you need to provide time for the internal MCU in the VL6180X to boot up. Add a short delay between your HIGH and your changeAddress. Only a couple of milliseconds should do it. – Majenko Apr 4 '18 at 23:15
  • After changing the i2c address, is that address stored in the sensor? Should it be set every time at startup? The pololu module has the recommended 47k resistor for gpio0/ce. When you put a 5v signal on that line the sensor might be damaged. That means at this moment all of those sensors might be broken. The module has a level shifter for sda and scl, but not the gpio0/ce pin. You should change the Arduino pin between output/low and input. – Jot Apr 4 '18 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.