2

(sharon) #1

Make an infrared rangefinder with ESP8266


TUTORIAL


The IR rangefinder instances and functions are set
out. A function to switch the sensor between low
power mode is included using D5/GPIO14. The GPIO
is set to an output using the pinMode instruction and the
output set with the digitalWrite instruction. Debug
information is sent to the serial monitor in the Arduino
IDE (Tools > Serial Monitor) using Serial.print and
Serial.println instructions.
VL53L0X sensor;
#define maxDistance 2000
#define sensorXSHUT 14
const boolean ON = HIGH;
const boolean OFF = LOW;
void switchLaserRangefinder(boolean state){
pinMode(sensorXSHUT, OUTPUT);
digitalWrite(sensorXSHUT, state);
if (state == ON) {
Serial.println(“Laser Rangefinder powered up”);
}
else {
Serial.println(“Laser Rangefinder powered down”);
}
delay(300);
}

Setting up the IR rangefinder is performed in a
separate function to keep the main setup function
uncluttered. The sensor is switched on, initialised
and various parameters set. Further details of the
instructions to set up the IR rangefinder are given in the
VL53L0X datasheet and in the examples which come
with the Pololu library.
void setupLaserRangefinder() {
switchLaserRangefinder(ON);
sensor.init();

sensor.setTimeout(500);
// 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);
// increase timing budget to 200 ms
sensor.setMeasurementTimingBudget(200000);
Serial.println(“Laser range finder configured”);
}
Now attention is given to the OLED display. Again, it
is set up in its own function for clarity.
#define I2Caddress 0x3c
SSD1306 display(I2Caddress, SDA, SCL);
void setupOLEDdisplay() {
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}

The main setup function is streamlined with the use
of the two module-specific setup functions. The Serial
Monitor communications are also initialised.
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(“Starting”);
delay(20);
Wire.begin(SDA, SCL);
setupLaserRangefinder();
delay(200);
setupOLEDdisplay();
}

The OLED display has many graphics capabilities,
and these are available in the SSD1306 library. Only
a few are demonstrated here. First of all, a function
is declared to draw a bar display using the draw
rectangle, fill rectangle and draw string methods.
The length of the bar to be filled in is calculated each
time the function is called, depending on the current
distance reading in variable value. A full bar of width,
width, corresponds to the maxdistance, set out earlier.
The calculated length is first found as a real value,
Posreal, e.g. 43.2 pixels, and then changed to an
integer value, Posint, like 43, as the methods drawing
the image require integer arguments.
int width=80;

The VL53L0X infrared laser rangefinder is more
accurate and has a much smaller sensing element
than the popular ultrasonic rangefinder, the
HR‑SC04, and is almost as cheap



Figure 1
Keeping the different
modules on different
breadboards is a little
cumbersome, but
does make it easy to
keep different bits of
the circuit separate
Free download pdf