如何在Arduino框架下实现ESP32 ADC稳定采样并通过经典蓝牙传输至安卓设备?
ESP32 Arduino框架下实现稳定10kHz多通道ADC采样并蓝牙传输的方法?
背景
- 需求:将自制电路的数据传输至安卓手机,需对多个GPIO引脚进行多通道ADC采样
- 现有状态:基于蓝牙串口终端开发的自有安卓接收APP功能正常,无问题
遇到的问题
ESP32的ADC采样率无法稳定维持10kHz,受CPU中断等因素影响波动较大。
已尝试方案
- 在Arduino框架下,通过循环调用
adc1_get_raw实现了约10kHz采样,但采样率不稳定,无法满足需求 - 尝试切换至ESP-IDF框架使用连续ADC模式以保证采样稳定性,但作为嵌入式新手,在该框架下难以实现经典蓝牙(SPP)功能
请问是否有在ESP32的Arduino框架下,实现稳定10kHz多通道ADC采样并通过经典蓝牙传输数据的可行方法?
附上当前测试代码
//This example code is in the Public Domain (or CC0 licensed, at your option.) //By Evandro Copercini - 2018 // //This example creates a bridge between Serial and Classical Bluetooth (SPP) //and also demonstrate that SerialBT have the same functionalities of a normal Serial #include "BluetoothSerial.h" #include <iostream> #include <string.h> #include <driver/adc.h> using namespace std; #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif BluetoothSerial SerialBT; const int WIN_SIZE = 100; const int NUM_CHANNELS = 1; int data[NUM_CHANNELS][WIN_SIZE];// this variable should contain everything needed to send to client. Can bool state = false; void setup() { SerialBT.begin("ESP32test"); //Bluetooth device name Serial.begin(115200); pinMode(12, OUTPUT); uint8_t clockDiv = 127; adcAttachPin(36); } void loop() { for (int i = 0; i < WIN_SIZE; i++){ data[i][0] = adc1_get_raw((adc1_channel_t) 36); } for(int i = 0; i < NUM_CHANNELS; i++){ for(int j = 0; j < WIN_SIZE; j++){ uint8_t byte1 = data[i][j] & 0x000000ff; uint8_t byte2 = (data[i][j] & 0x0000ff00) >> 8; uint8_t byte3 = (data[i][j] & 0x00ff0000) >> 16; uint8_t byte4 = (data[i][j] & 0xff000000) >> 24; SerialBT.write(byte4); SerialBT.write(byte3); SerialBT.write(byte2); SerialBT.write(byte1); state = !state; digitalWrite(12, state);//measure sampling frequency with oscope } } /* if (SerialBT.available()) {//reciever Serial.write(SerialBT.read()); Serial.println("That ran"); } */ }
内容的提问来源于stack exchange,提问作者Caden H
相关产品推荐
相关产品推荐

