Skip to content

Commit da506ef

Browse files
committed
2 examples added
1 parent 60062d0 commit da506ef

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <Arduino.h>
2+
3+
#include <ComponentObject.h>
4+
#include <RangeSensor.h>
5+
#include <SparkFun_VL53L1X.h>
6+
#include <vl53l1x_class.h>
7+
#include <vl53l1_error_codes.h>
8+
9+
/*
10+
Reading distance from the laser based VL53L1X
11+
By: Armin Joachimsmeyer
12+
for SparkFun Electronics
13+
Date: February 20th, 2020
14+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
15+
16+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
17+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
18+
19+
This example prints the distance to an object to the Arduino Serial Plotter and generates a tone depending on distance.
20+
21+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
22+
*/
23+
24+
#include <Wire.h>
25+
#include "SparkFun_VL53L1X.h"
26+
27+
//Optional interrupt and shutdown pins.
28+
#define SHUTDOWN_PIN 2
29+
#define INTERRUPT_PIN 3
30+
#define TONE_PIN 11
31+
32+
SFEVL53L1X distanceSensor;
33+
//Uncomment the following line to use the optional shutdown and interrupt pins.
34+
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
35+
36+
void setup(void) {
37+
Wire.begin();
38+
39+
// initialize the digital pin as an output.
40+
pinMode(LED_BUILTIN, OUTPUT);
41+
Serial.begin(9600);
42+
while (!Serial)
43+
; //delay for Leonardo
44+
// Just to know which program is running on my Arduino
45+
Serial.println(F("START " __FILE__));
46+
47+
// test beep for the connected speaker
48+
tone(TONE_PIN,1000,100);
49+
delay(200);
50+
51+
if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init
52+
Serial.println("Sensor online!");
53+
}
54+
55+
// Short mode max distance is limited to 1.3 m but has a better ambient immunity.
56+
// Above 1.3 meter error 4 is thrown (wrap around).
57+
distanceSensor.setDistanceModeShort();
58+
//distanceSensor.setDistanceModeLong(); // default
59+
60+
// Print Legend
61+
Serial.println("Distance Signal-Rate/100 Ambient-Rate/100");
62+
63+
/*
64+
* The minimum timing budget is 20 ms for the short distance mode and 33 ms for the medium and long distance modes.
65+
* Predefined values = 15, 20, 33, 50, 100(default), 200, 500.
66+
* This function must be called after SetDistanceMode.
67+
*/
68+
distanceSensor.setTimingBudgetInMs(50);
69+
70+
// measure periodically. Intermeasurement period must be >/= timing budget.
71+
distanceSensor.setIntermeasurementPeriod(100);
72+
distanceSensor.startRanging(); // Start once
73+
74+
}
75+
76+
void loop(void) {
77+
while (!distanceSensor.checkForDataReady()) {
78+
delay(1);
79+
}
80+
byte rangeStatus = distanceSensor.getRangeStatus();
81+
unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
82+
distanceSensor.clearInterrupt();
83+
84+
/*
85+
* With signed int we get overflow at short distances.
86+
* With unsigned we get an overflow below around 2.5 cm.
87+
*/
88+
unsigned int tSignalRate = distanceSensor.getSignalRate();
89+
unsigned int tAmbientRate = distanceSensor.getAmbientRate();
90+
91+
if (rangeStatus == 0) {
92+
tone(11, distance + 500);
93+
} else {
94+
// if tAmbientRate > tSignalRate we likely get a signal fail error condition
95+
// in Distance mode short error 4 (out of bounds) or 7 (wrap around) is thrown if the distance is greater than 1.3 meter.
96+
distance = rangeStatus;
97+
noTone(11);
98+
}
99+
100+
Serial.print(distance);
101+
Serial.print(' ');
102+
Serial.print(tSignalRate / 100);
103+
Serial.print(' ');
104+
Serial.println(tAmbientRate / 100);
105+
106+
107+
}
108+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <Arduino.h>
2+
3+
#include <ComponentObject.h>
4+
#include <RangeSensor.h>
5+
#include <SparkFun_VL53L1X.h>
6+
#include <vl53l1x_class.h>
7+
#include <vl53l1_error_codes.h>
8+
9+
/*
10+
Calling distance offset calibration for the laser based VL53L1X
11+
By: Armin Joachimsmeyer
12+
for SparkFun Electronics
13+
Date: February 20th, 2020
14+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
15+
16+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
17+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
18+
19+
This example calls the offset calibration for a VL53L1X sensor and stores the value to EEPROM.
20+
21+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
22+
*/
23+
24+
#include <Wire.h>
25+
#include "SparkFun_VL53L1X.h"
26+
27+
#define VERSION_EXAMPLE "1.0"
28+
29+
#define EEPROM_ADDRESS_FOR_OFFSET (E2END - 2) // last 2 bytes of EEPROM
30+
31+
//Optional interrupt and shutdown pins.
32+
#define SHUTDOWN_PIN 2
33+
#define INTERRUPT_PIN 3
34+
#define TONE_PIN 11
35+
36+
SFEVL53L1X distanceSensor;
37+
//Uncomment the following line to use the optional shutdown and interrupt pins.
38+
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
39+
40+
void setup(void) {
41+
Wire.begin();
42+
43+
// initialize the digital pin as an output.
44+
pinMode(LED_BUILTIN, OUTPUT);
45+
Serial.begin(9600);
46+
#if defined(__AVR_ATmega32U4__)
47+
while (!Serial); //delay for Leonardo, but this loops forever for Maple Serial
48+
#endif
49+
#if defined(SERIAL_USB)
50+
delay(2000); // To be able to connect Serial monitor after reset and before first printout
51+
#endif
52+
// Just to know which program is running on my Arduino
53+
Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__));
54+
55+
tone(TONE_PIN, 1000, 100);
56+
delay(200);
57+
58+
Serial.println();
59+
Serial.println("*****************************************************************************************************");
60+
Serial.println(" Offset calibration");
61+
Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor.");
62+
Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second.");
63+
Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin().");
64+
Serial.println("*****************************************************************************************************");
65+
Serial.println();
66+
67+
#ifdef AVR
68+
Serial.print("Old offset value read from EEPROM=");
69+
Serial.print((int16_t) eeprom_read_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET));
70+
Serial.println(" mm");
71+
#endif
72+
73+
if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init
74+
Serial.println("Sensor online!");
75+
}
76+
77+
// Short mode max distance is limited to 1.3 m but has a better ambient immunity.
78+
// Above 1.3 meter error 4 is thrown (wrap around).
79+
distanceSensor.setDistanceModeShort();
80+
//distanceSensor.setDistanceModeLong(); // default
81+
distanceSensor.setTimingBudgetInMs(50);
82+
83+
int tLowDistanceCount = 0;
84+
while (tLowDistanceCount < 20) {
85+
while (!distanceSensor.checkForDataReady()) {
86+
delay(1);
87+
}
88+
if (distanceSensor.getDistance() < 100) {
89+
tLowDistanceCount++;
90+
} else {
91+
tLowDistanceCount = 0;
92+
}
93+
distanceSensor.clearInterrupt();
94+
}
95+
96+
Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds");
97+
tone(TONE_PIN, 1000, 100);
98+
delay(1000);
99+
tone(TONE_PIN, 1000, 100);
100+
delay(1000);
101+
tone(TONE_PIN, 1000, 100);
102+
delay(1000);
103+
tone(TONE_PIN, 1000, 100);
104+
delay(1000);
105+
tone(TONE_PIN, 1000, 900);
106+
delay(1000);
107+
108+
/*
109+
* Place a target, 17 % gray, at a distance of 140 mm from the sensor and call the VL53L1X_CalibrateOffset (dev, 140, &offset) function.
110+
* The calibration may take a few seconds. The offset correction is applied to the sensor at the end of calibration.
111+
*
112+
* The calibration function takes 50 measurements and then takes the difference between the target distance
113+
* and the average distance and then calls setOffset() with this value. Thats all. No magic.
114+
*/
115+
distanceSensor.calibrateOffset(140);
116+
Serial.print("Result of offset calibration. RealDistance - MeasuredDistance=");
117+
Serial.print(distanceSensor.getOffset());
118+
Serial.print(" mm");
119+
Serial.println();
120+
121+
#ifdef AVR
122+
eeprom_write_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset());
123+
Serial.println("Offset value written to end of EEPROM.");
124+
#endif
125+
126+
// measure periodically. Intermeasurement period must be >/= timing budget.
127+
distanceSensor.setIntermeasurementPeriod(100);
128+
distanceSensor.startRanging(); // Start once
129+
130+
tone(TONE_PIN, 1000, 900);
131+
}
132+
133+
void loop(void) {
134+
135+
}
136+

0 commit comments

Comments
 (0)