VL53L0X激光测距传感器.ESP32使用篇
昨天对传感器的使用,还缺一个ESP32.这里补一下
首先把网站贴上
然后安装
注意这里的串口的打印波特率,错了会乱码
注意选对串口
https://github.com/espressif/arduino-esp32
项目使用了 221281 字节,占用了 (16%) 程序存储空间。最大为 1310720 字节。
全局变量使用了16092字节,(4%)的动态内存,余留311588字节局部变量。最大为327680字节。
esptool.py v2.6
Serial port COM4
Connecting.....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: f0:08:d1:d1:7d:c4
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 5461.4 kbit/s)...
Hash of data verified.
Compressed 15856 bytes to 10276...
Wrote 15856 bytes (10276 compressed) at 0x00001000 in 0.1 seconds (effective 991.0 kbit/s)...
Hash of data verified.
Compressed 221392 bytes to 114130...
Wrote 221392 bytes (114130 compressed) at 0x00010000 in 1.7 seconds (effective 1065.0 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.0 seconds (effective 1638.4 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(5000);
}
将ESP32与Arduino IDE搭配使用时,默认的I2C引脚为 GPIO 22 (SCL)和 GPIO 21 (SDA)
懂?
以上的程序是对打印i2C所在的地址
拔了i2c的器件,就没有了.插上就又可以打印出来.程序我就不分析了.以后分析
ESP32使用不同的I2C引脚(更改默认I2C引脚)
使用ESP32,你几乎可以将任何引脚设置为具有I2C功能,你只需要在代码中进行设置即可。
当将ESP32与Arduino IDE搭配使用时,请使用 wire库以使用I2C与设备通信。使用此库,你可以按以下方式初始化I2C:
Wire.begin(I2C_SDA, I2C_SCL);
因此,你只需要在驱动器上设置所需的SDA和SCL GPIO即可。 I2C_SDA 和 I2C_SCL 变量。具体驱动这个的方法看我下面的文章
这个地方,你需要将引脚地址指定
项目使用了 223345 字节,占用了 (17%) 程序存储空间。最大为 1310720 字节。
全局变量使用了16132字节,(4%)的动态内存,余留311548字节局部变量。最大为327680字节。
esptool.py v2.6
Serial port COM4
Connecting........_
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: f0:08:d1:d1:7d:c4
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 5957.9 kbit/s)...
Hash of data verified.
Compressed 15856 bytes to 10276...
Wrote 15856 bytes (10276 compressed) at 0x00001000 in 0.1 seconds (effective 998.8 kbit/s)...
Hash of data verified.
Compressed 223456 bytes to 115392...
Wrote 223456 bytes (115392 compressed) at 0x00010000 in 1.7 seconds (effective 1049.7 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.0 seconds (effective 2234.2 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
/*!
* @file DFRobot_VL53L0X.ino
* @brief DFRobot's Laser rangefinder library
* @n The example shows the usage of VL53L0X in a simple way.
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
* @author [LiXin]
* @version V1.0
* @date 2017-8-21
* @https://github.com/DFRobot/DFRobot_VL53L0X
timer*/
#include "Arduino.h"
#include "Wire.h"
#include "DFRobot_VL53L0X.h"
/*****************Keywords instruction*****************/
//Continuous--->Continuous measurement model
//Single------->Single measurement mode
//High--------->Accuracy of 0.25 mm
//Low---------->Accuracy of 1 mm
/*****************Function instruction*****************/
//setMode(ModeState mode, PrecisionState precision)
//*This function is used to set the VL53L0X mode
//*mode: Set measurement mode Continuous or Single
//*precision: Set the precision High or Low
//void start()
//*This function is used to enabled VL53L0X
//float getDistance()
//*This function is used to get the distance
//uint16_t getAmbientCount()
//*This function is used to get the ambient count
//uint16_t getSignalCount()
//*This function is used to get the signal count
//uint8_t getStatus();
//*This function is used to get the status
//void stop()
//*This function is used to stop measuring
DFRobotVL53L0X sensor;
void setup() {
//initialize serial communication at 9600 bits per second:
Serial.begin(115200);
//join i2c bus (address optional for master)
// Wire.begin();
Wire.begin(21, 22);
//Set I2C sub-device address
sensor.begin(0x50);
//Set to Back-to-back mode and high precision mode
sensor.setMode(Continuous,High);
//Laser rangefinder begins to work
sensor.start();
}
void loop()
{
//Get the distance
Serial.print("Distance: ");Serial.println(sensor.getDistance());
//The delay is added to demonstrate the effect, and if you do not add the delay,
//it will not affect the measurement accuracy
delay(500);
}
改正了引脚就好了
这个是默认的I2C的引脚,看连接
https://github.com/SmartArduino/SZDOITWiKi/wiki/Programming-with-Lua
放大的引脚位置
然后进行更改引脚
成功使用
这个地方没有看懂
注意看宏定义,两个引脚
这个地方是我把TT拿出来了,改了引脚.上传倒是成功了.
项目使用了 223345 字节,占用了 (17%) 程序存储空间。最大为 1310720 字节。
全局变量使用了16132字节,(4%)的动态内存,余留311548字节局部变量。最大为327680字节。
esptool.py v2.6
Serial port COM5
Connecting........_
Chip is ESP32D2WDQ5 (revision 1)
Features: WiFi, BT, Dual Core, Embedded Flash, VRef calibration in efuse, Coding Scheme None
MAC: b4:e6:2d:80:e8:45
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 2MB
Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 10922.6 kbit/s)...
Hash of data verified.
Flash params set to 0x021f
Compressed 15856 bytes to 10276...
Wrote 15856 bytes (10276 compressed) at 0x00001000 in 0.1 seconds (effective 1048.3 kbit/s)...
Hash of data verified.
Compressed 223456 bytes to 115390...
Wrote 223456 bytes (115390 compressed) at 0x00010000 in 1.6 seconds (effective 1128.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.0 seconds (effective 3510.9 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
但是输出的是乱码...
赞 (0)