Simple demo of VL6180x explorer shield with Nucleo F401 based on work by SparkFun.

Dependencies:   VL6180x mbed

Simple demo program for Nucleo VL6180 Explorer shield from ST mounted on Nucleo F401. Based on work from SparkFun Electronics.

Shows basic function of Ambient Light Sensor and Time of Flight Distance sensor. Values readable over standard serial port.

Committer:
highroads
Date:
Fri Jun 17 17:13:35 2016 +0000
Revision:
2:3de053b7541f
Parent:
1:126b6cd0f4f5
Child:
3:0ac797e55e49
Change address should now work. Try it in the program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
highroads 0:47f8714fe967 1 /******************************************************************************
highroads 0:47f8714fe967 2 * Developed from
highroads 0:47f8714fe967 3 * VL6180X_demo.ino
highroads 0:47f8714fe967 4 * Example Sketch for VL6180x time of flight range finder.
highroads 0:47f8714fe967 5 * Casey Kuhns @ SparkFun Electronics
highroads 0:47f8714fe967 6 * 10/29/2014
highroads 0:47f8714fe967 7 * https://github.com/sparkfun/ToF_Range_Finder-VL6180_Library
highroads 0:47f8714fe967 8 *
highroads 0:47f8714fe967 9 * The VL6180x by ST micro is a time of flight range finder that
highroads 0:47f8714fe967 10 * uses pulsed IR light to determine distances from object at close
highroads 0:47f8714fe967 11 * range. The average range of a sensor is between 0-200mm
highroads 0:47f8714fe967 12 *
highroads 0:47f8714fe967 13 * Resources:
highroads 0:47f8714fe967 14 * This library uses the Arduino Wire.h to complete I2C transactions.
highroads 0:47f8714fe967 15 *
highroads 0:47f8714fe967 16 * Development environment specifics:
highroads 0:47f8714fe967 17 * IDE: Arduino 1.0.5
highroads 0:47f8714fe967 18 * Hardware Platform: Arduino Pro 3.3V/8MHz
highroads 0:47f8714fe967 19 * VL6180x Breakout Version: 1.0
highroads 0:47f8714fe967 20 *
highroads 0:47f8714fe967 21 *
highroads 0:47f8714fe967 22 * This code is beerware. If you see me (or any other SparkFun employee) at the
highroads 0:47f8714fe967 23 * local pub, and you've found our code helpful, please buy us a round!
highroads 0:47f8714fe967 24 *
highroads 0:47f8714fe967 25 * Distributed as-is; no warranty is given.
highroads 0:47f8714fe967 26 ******************************************************************************/
highroads 0:47f8714fe967 27 #include "mbed.h"
highroads 0:47f8714fe967 28 #include <VL6180x.h>
highroads 0:47f8714fe967 29
highroads 0:47f8714fe967 30 /*const float GAIN_1 = 1.01; // Actual ALS Gain of 1.01
highroads 0:47f8714fe967 31 const float GAIN_1_25 = 1.28; // Actual ALS Gain of 1.28
highroads 0:47f8714fe967 32 const float GAIN_1_67 = 1.72; // Actual ALS Gain of 1.72
highroads 0:47f8714fe967 33 const float GAIN_2_5 = 2.6; // Actual ALS Gain of 2.60
highroads 0:47f8714fe967 34 const float GAIN_5 = 5.21; // Actual ALS Gain of 5.21
highroads 0:47f8714fe967 35 const float GAIN_10 = 10.32; // Actual ALS Gain of 10.32
highroads 0:47f8714fe967 36 const float GAIN_20 = 20; // Actual ALS Gain of 20
highroads 0:47f8714fe967 37 const float GAIN_40 = 40; // Actual ALS Gain of 40
highroads 0:47f8714fe967 38 */
highroads 0:47f8714fe967 39
highroads 0:47f8714fe967 40 #define VL6180X_ADDRESS 0x29
highroads 0:47f8714fe967 41
highroads 0:47f8714fe967 42 VL6180xIdentification identification;
highroads 0:47f8714fe967 43 // mbed uses 8bit addresses shift address by 1 bit left
highroads 0:47f8714fe967 44 VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);
highroads 0:47f8714fe967 45 void printIdentification(struct VL6180xIdentification *temp){
highroads 0:47f8714fe967 46 printf("Model ID = ");
highroads 0:47f8714fe967 47 printf("%d\n",temp->idModel);
highroads 0:47f8714fe967 48
highroads 0:47f8714fe967 49 printf("Model Rev = ");
highroads 0:47f8714fe967 50 printf("%d",temp->idModelRevMajor);
highroads 0:47f8714fe967 51 printf(".");
highroads 0:47f8714fe967 52 printf("%d\n",temp->idModelRevMinor);
highroads 0:47f8714fe967 53
highroads 0:47f8714fe967 54 printf("Module Rev = ");
highroads 0:47f8714fe967 55 printf("%d",temp->idModuleRevMajor);
highroads 0:47f8714fe967 56 printf(".");
highroads 0:47f8714fe967 57 printf("%d\n",temp->idModuleRevMinor);
highroads 0:47f8714fe967 58
highroads 0:47f8714fe967 59 printf("Manufacture Date = ");
highroads 0:47f8714fe967 60 printf("%d",((temp->idDate >> 3) & 0x001F));
highroads 0:47f8714fe967 61 printf("/");
highroads 0:47f8714fe967 62 printf("%d",((temp->idDate >> 8) & 0x000F));
highroads 0:47f8714fe967 63 printf("/1");
highroads 0:47f8714fe967 64 printf("%d\n",((temp->idDate >> 12) & 0x000F));
highroads 0:47f8714fe967 65 printf(" Phase: ");
highroads 0:47f8714fe967 66 printf("%d\n",(temp->idDate & 0x0007));
highroads 0:47f8714fe967 67
highroads 0:47f8714fe967 68 printf("Manufacture Time (s)= ");
highroads 0:47f8714fe967 69 printf("%d\n",(temp->idTime * 2));
highroads 0:47f8714fe967 70 printf("\n\n");
highroads 0:47f8714fe967 71 }
highroads 2:3de053b7541f 72
highroads 0:47f8714fe967 73 int main() {
highroads 0:47f8714fe967 74
highroads 0:47f8714fe967 75 wait_ms(100); // delay .1s
highroads 0:47f8714fe967 76
highroads 0:47f8714fe967 77 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
highroads 0:47f8714fe967 78 printIdentification(&identification); // Helper function to print all the Module information
highroads 0:47f8714fe967 79
highroads 0:47f8714fe967 80 if(sensor.VL6180xInit() != 0){
highroads 0:47f8714fe967 81 printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
highroads 0:47f8714fe967 82 };
highroads 0:47f8714fe967 83
highroads 0:47f8714fe967 84 sensor.VL6180xDefautSettings(); //Load default settings to get started.
highroads 0:47f8714fe967 85
highroads 2:3de053b7541f 86 wait_ms(1000); // delay 1s
highroads 2:3de053b7541f 87
highroads 2:3de053b7541f 88 printf("Ambient Light Level (Lux) = ");
highroads 2:3de053b7541f 89 printf("%f\n",sensor.getAmbientLight(GAIN_1) );
highroads 2:3de053b7541f 90
highroads 2:3de053b7541f 91 wait_ms(1000); // delay 1s
highroads 2:3de053b7541f 92 //
highroads 2:3de053b7541f 93 printf("Change sensor address to 0x27\n");
highroads 2:3de053b7541f 94 sensor.changeAddress(0x29,0x27);
highroads 2:3de053b7541f 95 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
highroads 2:3de053b7541f 96 printIdentification(&identification); // Helper function to print all the Module information
highroads 2:3de053b7541f 97 printf("Ambient Light Level (Lux) = ");
highroads 2:3de053b7541f 98 printf("%f\n",sensor.getAmbientLight(GAIN_1) );
highroads 2:3de053b7541f 99
highroads 2:3de053b7541f 100 wait_ms(1000); // delay 1s
highroads 2:3de053b7541f 101 printf("Change sensor address back to 0x29\n");
highroads 2:3de053b7541f 102 sensor.changeAddress(0x27,0x29);
highroads 2:3de053b7541f 103 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
highroads 2:3de053b7541f 104 printIdentification(&identification); // Helper function to print all the Module information
highroads 2:3de053b7541f 105 printf("Ambient Light Level (Lux) = ");
highroads 2:3de053b7541f 106 printf("%f\n",sensor.getAmbientLight(GAIN_1) );
highroads 2:3de053b7541f 107
highroads 0:47f8714fe967 108 while(1) {
highroads 0:47f8714fe967 109 //Get Ambient Light level and report in LUX
highroads 0:47f8714fe967 110 printf("Ambient Light Level (Lux) = ");
highroads 0:47f8714fe967 111
highroads 0:47f8714fe967 112 //Input GAIN for light levels,
highroads 0:47f8714fe967 113 // GAIN_20 // Actual ALS Gain of 20
highroads 0:47f8714fe967 114 // GAIN_10 // Actual ALS Gain of 10.32
highroads 0:47f8714fe967 115 // GAIN_5 // Actual ALS Gain of 5.21
highroads 0:47f8714fe967 116 // GAIN_2_5 // Actual ALS Gain of 2.60
highroads 0:47f8714fe967 117 // GAIN_1_67 // Actual ALS Gain of 1.72
highroads 0:47f8714fe967 118 // GAIN_1_25 // Actual ALS Gain of 1.28
highroads 0:47f8714fe967 119 // GAIN_1 // Actual ALS Gain of 1.01
highroads 0:47f8714fe967 120 // GAIN_40 // Actual ALS Gain of 40
highroads 0:47f8714fe967 121
highroads 0:47f8714fe967 122 printf("%f\n",sensor.getAmbientLight(GAIN_1) );
highroads 0:47f8714fe967 123
highroads 0:47f8714fe967 124 //Get Distance and report in mm
highroads 0:47f8714fe967 125 printf("Distance measured (mm) = ");
highroads 0:47f8714fe967 126 printf("%d\n", sensor.getDistance() );
highroads 2:3de053b7541f 127 //Get Distance and report in mm
highroads 2:3de053b7541f 128 printf("Distance measured (m) = ");
highroads 2:3de053b7541f 129 printf("%f\n", sensor.getDistance_m() );
highroads 0:47f8714fe967 130
highroads 2:3de053b7541f 131 wait_ms(500);
highroads 2:3de053b7541f 132
highroads 0:47f8714fe967 133 }
highroads 0:47f8714fe967 134 }
highroads 0:47f8714fe967 135
highroads 0:47f8714fe967 136
highroads 0:47f8714fe967 137
highroads 0:47f8714fe967 138
highroads 0:47f8714fe967 139