Lesson 5. VL53L0X Time-of-Flight Distance Sensor

Dowland VL53L0X  library:

https://github.com/pololu/vl53l0x-arduino

 

/* This example shows how to get single-shot range
measurements from the VL53L0X. The sensor can optionally be
configured with different ranging profiles, as described in
the VL53L0X API user manual, to get better performance for
a certain application. This code is based on the four
"SingleRanging" examples in the VL53L0X API.

The range readings are in units of mm. */

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

VL53L0X sensor;

 

// Uncomment this line to use long range mode. This
// increases the sensitivity of the sensor and extends its
// potential range, but increases the likelihood of getting
// an inaccurate reading because of reflections from objects
// other than the intended target. It works best in dark
// conditions.

//#define LONG_RANGE

 

// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed

//#define HIGH_SPEED
//#define HIGH_ACCURACY

 

void setup()
{
Serial.begin(9600);
Wire.begin();

sensor.init();
sensor.setTimeout(500);

#if defined LONG_RANGE
// lower the return signal rate limit (default is 0.25 MCPS)
sensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif

#if defined HIGH_SPEED
// reduce timing budget to 20 ms (default is about 33 ms)
sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
// increase timing budget to 200 ms
sensor.setMeasurementTimingBudget(200000);
#endif
}

void loop()
{
Serial.print(sensor.readRangeSingleMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

Serial.println();
}

 

 

 


Lesson 3. 9250 Accelerometer and Gyroscope Sensor

9250 Accelerometer and Gyroscope Sensor

 

#include <Wire.h>

#define MPU9250_ADDRESS 0x68
#define MAG_ADDRESS 0x0C

#define GYRO_FULL_SCALE_250_DPS 0x00
#define GYRO_FULL_SCALE_500_DPS 0x08
#define GYRO_FULL_SCALE_1000_DPS 0x10
#define GYRO_FULL_SCALE_2000_DPS 0x18

#define ACC_FULL_SCALE_2_G 0x00
#define ACC_FULL_SCALE_4_G 0x08
#define ACC_FULL_SCALE_8_G 0x10
#define ACC_FULL_SCALE_16_G 0x18

// This function read Nbytes bytes from I2C device at address Address.
// Put read bytes starting at register Register in the Data array.

uint8_t ST1;
int16_t mag_int[3] = {0};

void writeBIT(int addr, int data, int adrrTrans){
Wire.beginTransmission(adrrTrans);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission();
}
void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data){
// Set register address
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.endTransmission();
// Read Nbytes
Wire.requestFrom(Address, Nbytes);
uint8_t index=0;
while (Wire.available())
Data[index++]=Wire.read();
}

// Initializations
void setup()
{
// Arduino initializations
Wire.begin();
Serial.begin(115200);

Wire.setClock(400000L);
// Set accelerometers low pass filter at 5Hz
writeBIT(29, 0x06,MPU9250_ADDRESS);
// Set gyroscope low pass filter at 5Hz
writeBIT(26, 0x06,MPU9250_ADDRESS);
// Configure gyroscope range
writeBIT(27, GYRO_FULL_SCALE_2000_DPS,MPU9250_ADDRESS);
// Configure accelerometers range
writeBIT(28, ACC_FULL_SCALE_2_G,MPU9250_ADDRESS);
// Set by pass mode for the magnetometers
writeBIT(0x37, 0x02,MPU9250_ADDRESS);
// Request continuous magnetometer measurements in 16 bits
//writeBIT(0x0A, 0x16,MAG_ADDRESS);
delay(200);
// writeBIT(0x0A, mRes << 4 | 0x06 ,MAG_ADDRESS);

writeBIT(0x0A, 0x00 , MAG_ADDRESS); // Power down magnetometer
delay(20);
writeBIT(0x0A , 0x0F, MAG_ADDRESS); // Enter Fuse ROM access mode
delay(20);

}

// Main loop, read and display data
void loop()
{
// ____________________________________
// ::: accelerometer and gyroscope :::

// Read accelerometer and gyroscope
uint8_t Buf[14];
int16_t accel[3] = {0}, gyro_int[3] = {0}, MEAN_GYRO[3] = {0};;

I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);

accel[0]=(Buf[0]<<8 | Buf[1]);
accel[1]=(Buf[2]<<8 | Buf[3]);
accel[2]=Buf[4]<<8 | Buf[5];

// Gyroscope
gyro_int[0]=(Buf[8]<<8 | Buf[9]);
gyro_int[1]=(Buf[10]<<8 | Buf[11]) ;
gyro_int[2]=Buf[12]<<8 | Buf[13] ;

// Display values

// Accelerometer
Serial.print (accel[0],DEC);
Serial.print ("\t");
Serial.print (accel[1],DEC);
Serial.print ("\t");
Serial.print (accel[2],DEC);
Serial.print ("\t");

// Gyroscope
Serial.print (gyro_int[0],DEC);
Serial.print ("\t");
Serial.print (gyro_int[1],DEC);
Serial.print ("\t");
Serial.print (gyro_int[2],DEC);
Serial.println ("\t");

}