Skip to content

Commit 80fb2e1

Browse files
authored
Merge pull request #26 from arduino/marqdevx/tutorial/NiclaVision/proximity
Nicla Vision: Proximity sensor documentation [PC-848]
2 parents 4ceb90e + 96b4f26 commit 80fb2e1

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: Proximity Detection with Arduino Nicla Vision
3+
difficulty: easy
4+
tags: [Proximity, Time Of Flight, Blink]
5+
description: Learn how to use the proximity sensor to vary the speed of the LED's blink.
6+
author: Pablo Marquínez
7+
libraries:
8+
- name: VL53L1X
9+
url: https://github.com/pololu/vl53l1x-arduino
10+
hardware:
11+
- hardware/05.nicla/boards/nicla-vision
12+
software:
13+
- ide-v1
14+
- ide-v2
15+
- web-editor
16+
- cli
17+
---
18+
19+
## Overview
20+
21+
In this tutorial you will use the Nicla Vision to detect proximity, thanks to the Time of Flight (ToF) sensor **VL53L1X**.
22+
23+
This tutorial goes through how to create a sketch that will blink the built-in RGB LED and control the speed of its blink with the proximity values. It can be useful for future projects where there is a need to control the camera only when something is detected in front of the sensor.
24+
25+
***The Arduino sketch shown is available inside the `Arduino_Pro_Tutorials` library by going to Examples > Nicla Vision > Proximity_Blink***
26+
27+
## Goals
28+
The goals of this project are:
29+
- Set up the needed libraries
30+
- Learn how to interact with the proximity readings
31+
- Change the RGB values of the LED
32+
33+
### Required Hardware and Software
34+
35+
* Arduino Nicla Vision
36+
* VL53L1X library (Available in the Library Manager)
37+
38+
## Instructions
39+
40+
### Time of Flight Sensor
41+
42+
![Arduino Nicla Vision - Time of Flight sensor](assets/nicla-vision-tof.png)
43+
44+
To make sure that the sketch works properly, the latest versions of the **Arduino mbed Core** and the **VL53L1X library** needs to be installed. The **Arduino mbed Core** can be found in the **boards manager...** and the **VL53L1X library** can be found in the **Library manager**, both can be found inside the Arduino IDE.
45+
46+
### Include the Needed Libraries and Objects Declaration
47+
48+
First of all declare the sensor's class so you can access it later on in your sketch. We use variables to control the time elements in the sketch. This will make sure that the readings stay accurate over time.
49+
50+
```cpp
51+
#include "VL53L1X.h"
52+
VL53L1X proximity;
53+
54+
bool blinkState = false;
55+
int reading = 0;
56+
int timeStart = 0;
57+
int blinkTime = 2000;
58+
```
59+
60+
### Initialize the Proximity Sensor and the LED
61+
62+
Inside the setup you need to initialize and configure the proximity sensor. Also the RGB LED needs to be set as an output to make it light up and enable us to change its behavior.
63+
64+
***The LEDs are accessed in the same way as on the Portenta H7: LEDR, LEDG and LEDB***
65+
66+
```cpp
67+
void setup(){
68+
Serial.begin(115200);
69+
Wire1.begin();
70+
Wire1.setClock(400000); // use 400 kHz I2C
71+
proximity.setBus(&Wire1);
72+
73+
pinMode(LEDB,OUTPUT);
74+
digitalWrite(LEDB, blinkState);
75+
76+
if (!proximity.init()){
77+
Serial.println("Failed to detect and initialize sensor!");
78+
while (1);
79+
}
80+
81+
proximity.setDistanceMode(VL53L1X::Long);
82+
proximity.setMeasurementTimingBudget(10000);
83+
proximity.startContinuous(10);
84+
}
85+
```
86+
87+
***Make sure you initialize `Wire1`, set the clock speed to 400kHz and set the bus pointer to `Wire1`, it won't work if you don't add these setting***
88+
89+
### Control the Speed of the Blink
90+
91+
The sketch is going to get the reading on every loop, store it and then the state of the LED will change, until the time is up and another proximity reading is taken.
92+
93+
```cpp
94+
void loop(){
95+
reading = proximity.read();
96+
Serial.println(reading);
97+
98+
if (millis() - timeStart >= reading){
99+
digitalWrite(LEDB, blinkState);
100+
timeStart = millis();
101+
102+
blinkState = !blinkState;
103+
}
104+
}
105+
```
106+
107+
### Complete Sketch
108+
109+
```cpp
110+
#include "VL53L1X.h"
111+
VL53L1X proximity;
112+
113+
bool blinkState = false;
114+
int reading = 0;
115+
int timeStart = 0;
116+
int blinkTime = 2000;
117+
118+
void setup() {
119+
Serial.begin(115200);
120+
Wire1.begin();
121+
Wire1.setClock(400000); // use 400 kHz I2C
122+
proximity.setBus(&Wire1);
123+
124+
125+
pinMode(LEDB, OUTPUT);
126+
digitalWrite(LEDB, blinkState);
127+
128+
if (!proximity.init()) {
129+
Serial.println("Failed to detect and initialize sensor!");
130+
while (1);
131+
}
132+
133+
proximity.setDistanceMode(VL53L1X::Long);
134+
proximity.setMeasurementTimingBudget(10000);
135+
proximity.startContinuous(10);
136+
}
137+
138+
void loop() {
139+
reading = proximity.read();
140+
Serial.println(reading);
141+
142+
if (millis() - timeStart >= reading) {
143+
digitalWrite(LEDB, blinkState);
144+
timeStart = millis();
145+
146+
blinkState = !blinkState;
147+
}
148+
}
149+
```
150+
151+
## API
152+
| Command | Details | type |
153+
| :----------------------------------- | :----------------------------------------------------------: | :---------------- |
154+
| setAddress(newAddress) | Change the I2C sensor's address (Mandatory to set it to `Wire1`) | `void` |
155+
| getAddress() | Get the Sensor's I2C address | `uint8_t` |
156+
| init() | Configures the sensor and needed data. Like the usual begin()| `void` |
157+
| setDistanceMode(mode) | Set the distance mode (check the datasheet). Available modes `VL53L1X::Short`, `VL53L1X::Medium`, `VL53L1X::Long`, `VL53L1X::Unknown` | `void` |
158+
| getDistanceMode() | Returns the mode that has been set. Available modes `VL53L1X::Short`, `VL53L1X::Medium`, `VL53L1X::Long`, `VL53L1X::Unknown`| `enum DistanceMode ` |
159+
| setMeasurementTimingBudget(uSeconds) | Set the time to get the measure, greater the value, better precision. In micro seconds. | `void` |
160+
| getMeasurementTimingBudget() | Get the measure timing value in micro seconds. | `uint32_t` |
161+
| startContinuous() | Start the non stop readings, set the period inside the parameter, after that time you will get the reading. | `void` |
162+
| stopContinuous() | Stop the non stop measurements. | `void` |
163+
| read() | Get the last reading from the continuous mode. | `void` |
164+
| readSingle() | Trigger one reading and get its result. | `uint16_t` |
165+
| dataReady() | Returns if the sensor has new data available. | `bool` |
166+
| setTimeout(mSeconds) | Configure the milliseconds the sensor will wait in case it is not getting the proper reading to abort, and continue with a new one, 0 disables it. | `void` |
167+
| getTimeout() | Get the configured timeout value. | `uint16_t` |
168+
| timeoutOccurred() | Returns true whenever the sensor had a timeout. | `bool` |
169+
170+
171+
## Conclusion
172+
173+
In this tutorial we went through how to get readings from the ToF sensor. And how use these readings to change how the built-in LED behaves. At the end of the tutorial you can also find a reference list for the ToF library.

0 commit comments

Comments
 (0)