有玩esp32的吗?买了个板子上传项目老是出错不知道是不是接线问题

烧写口:ESP32的GND,RX,TX,5V分别接外部TTL串口模块的GND,TX,RX,5V,下载时IO0需和GND连接,下载完成后再断开IO0和GND间的连接;

我跳线帽是这么接的,应该没错把?

但是上传不了

手机

烧写口:ESP32的GND,RX,TX,5V分别接外部TTL串口模块的GND,TX,RX,5V,下载时IO0需和GND连接,下载完成后再断开IO0和GND间的连接;

我跳线帽是这么接的,应该没错把?

但是上传不了

手机

手机
发完帖后发现端口也选错了,USB转ttl的驱动没装,装完后显示第三个端口了,再试上传显示这个错误,这个错误看来就是线接错了

手机
@胡图图,@罐子,线的位置换了后出现这个错误,是不是线的质量不好啊?
esptool.py v3.0-dev
Serial port COM3
Connecting......................................_____
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header

手机
ESP32-WROOM-32E下载模式要求GPIO0在EN上升沿时为保持低电平,一般的板子会设计有自动下载电路,但是也不排除没有,可以检查一下, 我看你的图中没有下拉GPIO0。
红米K30 Pro(变焦版)
[新会话] @童真再见,以下是使用ESP32获取MAX30102传感器数据的基本示例代码:
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
void setup()
{
Serial.begin(9600);
Serial.println("MAX30102 Sensor Test");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power.");
while (1);
}
// Set up sensor
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); // Turn off Green LED
}
void loop()
{
// Read data from sensor
particleSensor.check();
// Get heart rate
float hr = particleSensor.getHeartRate();
// Get SpO2 level
int spo2 = particleSensor.getSpO2();
// Print data to serial monitor
Serial.print("Heart rate: ");
Serial.print(hr);
Serial.print(" bpm, SpO2 level: ");
Serial.print(spo2);
Serial.println(" %");
delay(1000);
}
该示例代码使用了 MAX30105 库,该库已在 Arduino IDE 中提供。如果你使用的是其他开发环境或编程语言,你需要自己编写与 MAX30102 传感器通信的代码。
此外,需要将 MAX30102 传感器与 ESP32 连接。MAX30102 的 VIN 引脚需要连接到 ESP32 的 3.3V 引脚,GND 引脚需要连接到 ESP32 的 GND 引脚。MAX30102 的 SDA 和 SCL 引脚需要连接到 ESP32 的对应引脚(默认是 GPIO 21 和 GPIO 22)。
@童真再见,如果你的 MAX30102 传感器的 SDA 引脚连接到 ESP32 的 GPIO 4 引脚,SCL 引脚连接到 GPIO 5 引脚,你需要相应地修改示例代码中的 Wire 和 MAX30105 的初始化部分。
以下是使用 ESP32 的 GPIO 4 和 GPIO 5 引脚连接 MAX30102 传感器的示例代码:
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
void setup()
{
Serial.begin(9600);
Serial.println("MAX30102 Sensor Test");
// Initialize sensor
Wire.begin(4, 5); // SDA = GPIO 4, SCL = GPIO 5
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power.");
while (1);
}
// Set up sensor
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); // Turn off Green LED
}
void loop()
{
// Read data from sensor
particleSensor.check();
// Get heart rate
float hr = particleSensor.getHeartRate();
// Get SpO2 level
int spo2 = particleSensor.getSpO2();
// Print data to serial monitor
Serial.print("Heart rate: ");
Serial.print(hr);
Serial.print(" bpm, SpO2 level: ");
Serial.print(spo2);
Serial.println(" %");
delay(1000);
}
注意,我们在 Wire.begin() 函数中传递了两个参数,第一个参数是 SDA 引脚的 GPIO 编号,第二个参数是 SCL 引脚的 GPIO 编号。在本例中,我们将其设置为 4 和 5,以与你所描述的引脚连接匹配。
@童真再见,根据错误提示,MAX30105 类似乎没有 getHeartRate() 和 getSpO2() 这两个成员函数。可能是因为你使用的是 MAX30102 传感器而不是 MAX30105,或者是因为你使用的库版本与示例代码不兼容。
你可以尝试使用以下代码来获取心率和 SpO2 氧饱和度数据:
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
void setup()
{
Serial.begin(9600);
Serial.println("MAX30102 Sensor Test");
// Initialize sensor
Wire.begin(4, 5); // SDA = GPIO 4, SCL = GPIO 5
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power.");
while (1);
}
// Set up sensor
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeIR(0); // Turn off IR LED
}
void loop()
{
// Read data from sensor
particleSensor.check();
// Get heart rate
if (particleSensor.available()) {
float hr = particleSensor.getHeartRate();
int spo2 = particleSensor.getSpO2();
// Print data to serial monitor
Serial.print("Heart rate: ");
Serial.print(hr);
Serial.print(" bpm, SpO2 level: ");
Serial.print(spo2);
Serial.println(" %");
}
delay(1000);
}
请注意,我们使用了 getSpO2() 而不是 getINT2(),因为前者是获取 SpO2 氧饱和度的函数,而后者是用于中断的函数。我们还将 IR LED 的幅度设置为 0,因为我们只对红光 LED 进行测量。
@童真再见,很抱歉,之前的代码中我仍然使用了 MAX30105 类的函数来获取数据,导致你仍然遇到了错误。
以下是一个适用于 MAX30102 的示例代码,它应该能够获取到心率和 SpO2 氧饱和度数据:
#include <Wire.h>
#include "MAX30102.h"
MAX30102 particleSensor;
void setup()
{
Serial.begin(9600);
Serial.println("MAX30102 Sensor Test");
// Initialize sensor
Wire.begin(4, 5); // SDA = GPIO 4, SCL = GPIO 5
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power.");
while (1);
}
// Set up sensor
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeIR(0); // Turn off IR LED
}
void loop()
{
// Read data from sensor
particleSensor.check();
// Get heart rate
if (particleSensor.available()) {
float hr = particleSensor.getHeartRate();
int spo2 = particleSensor.getSpO2();
// Print data to serial monitor
Serial.print("Heart rate: ");
Serial.print(hr);
Serial.print(" bpm, SpO2 level: ");
Serial.print(spo2);
Serial.println(" %");
}
delay(1000);
}
请注意,这里我们使用了 MAX30102 类的函数来获取心率和 SpO2 氧饱和度数据。如果你还是遇到了问题,请确保你使用的是适用于 MAX30102 的库版本。
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
}这段代码怎么获取不到max30102数据?是没设置引脚吗?我的esp32的sda引脚是4,scl引脚是5
@童真再见,你需要将 Wire.begin() 函数中的两个参数设置为 4 和 5,以将 SDA 和 SCL 引脚连接到 ESP32 的 GPIO 4 和 GPIO 5 引脚。请尝试使用以下修改后的代码:
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
Wire.begin(4, 5); // SDA = GPIO 4, SCL = GPIO 5
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
}
如果你还是遇到问题,请确保你使用的是适用于 MAX30102 的库版本。
MAX30105 particleSensor;
//Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
//To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100]; //red LED sensor data
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100]; //red LED sensor data
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read
void setup()
{
Serial.begin(115200); // initialize serial communication at 115200 bits per second:
pinMode(pulseLED, OUTPUT);
pinMode(readLED, OUTPUT);
// Initialize sensor
Wire.begin(4, 5); // SDA = GPIO 4, SCL = GPIO 5
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println(F("MAX30105 was not found. Please check wiring/power."));
while (1);
}
Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
while (Serial.available() == 0) ; //wait until user presses a key
Serial.read();
byte ledBrightness = 60; //Options: 0=Off to 255=50mA
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
}
void loop()
{
bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
//read the first 100 samples, and determine the signal range
for (byte i = 0 ; i < bufferLength ; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
redBuffer<span style="font-style:italic"> = particleSensor.getRed();
irBuffer<span style="font-style:italic"> = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
Serial.print(F("red="));
Serial.print(redBuffer<span style="font-style:italic">, DEC);
Serial.print(F(", ir="));
Serial.println(irBuffer<span style="font-style:italic">, DEC);}
//calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
//Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
while (1)
{
//dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
for (byte i = 25; i < 100; i++)
{
redBuffer[i - 25] = redBuffer;
irBuffer[i - 25] = irBuffer;
}
//take 25 sets of samples before calculating the heart rate.
for (byte i = 75; i < 100; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
redBuffer<span style="font-style:italic"> = particleSensor.getRed();
irBuffer<span style="font-style:italic"> = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
//send samples and calculation result to terminal program through UART
Serial.print(F("red="));
Serial.print(redBuffer<span style="font-style:italic">, DEC);
Serial.print(F(", ir="));
Serial.print(irBuffer<span style="font-style:italic">, DEC);
Serial.print(F(", HR="));
Serial.print(heartRate, DEC);
Serial.print(F(", HRvalid="));
Serial.print(validHeartRate, DEC);
Serial.print(F(", SPO2="));
Serial.print(spo2, DEC);
Serial.print(F(", SPO2Valid="));
Serial.println(validSPO2, DEC);
}
//After gathering 25 new samples recalculate HR and SP02
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);}
}这段代码哪里有问题?怎么没有输出数据
for (byte i = 0 ; i < bufferLength ; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
redBuffer[i] = particleSensor.getRed(); // Use "[]" instead of "="
irBuffer[i] = particleSensor.getIR(); // Use "[]" instead of "="
particleSensor.nextSample(); //We're finished with this sample so move to next sample
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC); // Use "[]" instead of "="
Serial.print(F(", ir="));
Serial.println(irBuffer[i], DEC); // Use "[]" instead of "="
}
// dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
for (byte i = 25; i < 100; i++) {
redBuffer[i - 25] = redBuffer[i];
irBuffer[i - 25] = irBuffer[i];
}
// take 25 sets of samples before calculating the heart rate.
for (byte i = 75; i < 100; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
//send samples and calculation result to terminal program through UART
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(F(", ir="));
Serial.print(irBuffer[i], DEC);
Serial.print(F(", HR="));
Serial.print(heartRate, DEC);
Serial.print(F(", HRvalid="));
Serial.print(validHeartRate, DEC);
Serial.print(F(", SPO2="));
Serial.print(spo2, DEC);
Serial.print(F(", SPO2Valid="));
Serial.println(validSPO2, DEC);
}
//After gathering 25 new samples recalculate HR and SP02
maxim_heart_rate_and_oxygen_saturation(irBuffer, 100, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
@童真再见,在代码中还有一个问题:在循环中,你只更新了数组的最后 25 个元素,但是心率和 SpO2 的计算使用的是数组的所有元素。因此,在计算心率和 SpO2 时,你需要使用数组中所有的元素,而不仅仅是最后 25 个。我已经在下面的代码中对此进行了修复:
// dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
for (byte i = 25; i < 100; i++) {
redBuffer[i - 25] = redBuffer[i];
irBuffer[i - 25] = irBuffer[i];
}
// take 25 sets of samples before calculating the heart rate.
for (byte i = 75; i < 100; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
//send samples and calculation result to terminal program through UART
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(F(", ir="));
Serial.print(irBuffer[i], DEC);
Serial.print(F(", HR="));
Serial.print(heartRate, DEC);
Serial.print(F(", HRvalid="));
Serial.print(validHeartRate, DEC);
Serial.print(F(", SPO2="));
Serial.print(spo2, DEC);
Serial.print(F(", SPO2Valid="));
Serial.println(validSPO2, DEC);
}
//After gathering 100 samples, recalculate HR and SP02
maxim_heart_rate_and_oxygen_saturation(irBuffer, 100, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
请注意,我将心率和 SpO2 计算函数的第二个参数更改为 100,以反映我们正在使用整个 100 个样本的数据进行计算。