介绍
Arduino Nano RP2040 Connect,基于树莓派RP2040双核MCU(ARM Cortex-M0+ 133MHz) + NINA-W102 WiFi/BLE模块,16MB外部Flash,264KB SRAM,内置6轴IMU+麦克风+RGB LED,支持MicroPython和Arduino IDE。3.3V逻辑,Micro-USB,尺寸18×45mm。结合RP2040高性能与WiFi/BLE连接,适合边缘AI和IoT应用
Arduino Nano RP2040 Connect,基于树莓派RP2040双核MCU(ARM Cortex-M0+ 133MHz) + NINA-W102 WiFi/BLE模块,16MB外部Flash,264KB SRAM,内置6轴IMU+麦克风+RGB LED,支持MicroPython和Arduino IDE。3.3V逻辑,Micro-USB,尺寸18×45mm。结合RP2040高性能与WiFi/BLE连接,适合边缘AI和IoT应用
| 参数 | 值 |
|---|---|
| I2C | 1路 |
| PWM | 11路 |
| SPI | 1路 |
| SRAM | 264KB |
| Flash | 16MB(外部QSPI) |
| 串口 | 1路硬UART |
| 内核 | 双核ARM Cortex-M0+ 133MHz |
| 尺寸 | 18mm×45mm |
| 无线 | NINA-W102: WiFi 802.11 b/g/n + BLE 4.2 |
| 重量 | 约6g |
| RGB LED | WS2812(NeoPixel) 1颗 |
| 数字IO | 14路 |
| USB接口 | Micro-USB(Native USB) |
| 麦克风 | MP34DT05 数字MEMS麦克风 |
| 主控芯片 | RP2040 (树莓派) |
| 工作电压 | 3.3V(逻辑) |
| 模拟输入 | 8路(12位ADC) |
| 输入电压 | 5V(USB)/4.5-21V(VIN) |
| 内置传感器 | ST LSM6DSOX(6轴IMU+机器学习内核) |
# Arduino Nano RP2040 Connect (RP2040) 代码例程
## 例程 1:双核并行 — Core0 + Core1
```cpp
// Nano RP2040 Connect - 双核并行示例
// Core0: 主循环 + WiFi
// Core1: 传感器采集
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Dual-Core Demo");
}
void setup1() {
// Core1 初始化(运行在第二核心)
// 注意:delay() 在每个核心独立计时
}
void loop() {
// === Core0:主任务 ===
static unsigned long last = 0;
if (millis() - last > 1000) {
last = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
// 从 Core1 接收数据(FIFO)
if (rp2040.fifo.available()) {
int sensorValue = rp2040.fifo.pop();
Serial.print("Core0 received from Core1: ");
Serial.println(sensorValue);
}
}
}
void loop1() {
// === Core1:传感器采集 ===
int adc = analogRead(A0);
// 通过 FIFO 发送给 Core0(非阻塞)
rp2040.fifo.push_nb(adc);
delay(500); // Core1 独立的 delay
}
```
---
## 例程 2:RGB LED (WS2812) 控制
```cpp
// Nano RP2040 Connect - WS2812 RGB LED(D3 控制)
#include <Adafruit_NeoPixel.h>
#define LED_PIN 3
#define NUM_LEDS 1
Adafruit_NeoPixel rgb(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
rgb.begin();
rgb.setBrightness(30); // 0-255
rgb.clear();
rgb.show();
}
void loop() {
// 彩虹渐变
static unsigned long hue = 0;
rgb.setPixelColor(0, rgb.ColorHSV(hue));
rgb.show();
hue += 256; // 16位色相
delay(20);
}
```
---
## 例程 3:MEMS 麦克风 声级检测
```cpp
// Nano RP2040 Connect - MP34DT05 PDM 麦克风
// 使用 PDM 库读取数字麦克风
#include <PDM.h>
#define PDM_CLK 2
#define PDM_DIN 3
#define SAMPLE_RATE 16000
#define BUFFER_SIZE 512
short sampleBuffer[BUFFER_SIZE];
volatile int samplesRead = 0;
void onPDMdata() {
samplesRead = PDM.read(sampleBuffer, BUFFER_SIZE);
}
void setup() {
Serial.begin(115200);
while (!Serial);
PDM.onReceive(onPDMdata);
if (!PDM.begin(1, SAMPLE_RATE)) { // 单声道
Serial.println("PDM init failed!");
while (1);
}
Serial.println("Microphone ready. Reading audio levels...");
}
void loop() {
if (samplesRead) {
// 计算 RMS(声级)
float sum = 0;
for (int i = 0; i < samplesRead; i++) {
sum += (float)sampleBuffer[i] * sampleBuffer[i];
}
float rms = sqrt(sum / samplesRead);
float db = 20.0f * log10(rms / 32768.0f) + 94.0f; // 近似 dB SPL
Serial.print("RMS: "); Serial.print(rms, 0);
Serial.print(" Level: "); Serial.print(db, 1);
Serial.println(" dB");
samplesRead = 0;
}
delay(100);
}
```
---
## 例程 4:LittleFS 文件系统(16MB Flash)
```cpp
// Nano RP2040 Connect - LittleFS 文件读写
// 利用16MB Flash存储数据
#include <LittleFS.h>
void setup() {
Serial.begin(115200);
while (!Serial);
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed!");
// 首次使用需格式化
if (!LittleFS.format()) {
Serial.println("Format failed!");
while (1);
}
LittleFS.begin();
}
// 列出已有文件
Serial.println("Files on LittleFS:");
File dir = LittleFS.open("/");
File file = dir.openNextFile();
while (file) {
Serial.print(" ");
Serial.print(file.name());
Serial.print(" ");
Serial.print(file.size());
Serial.println(" bytes");
file = dir.openNextFile();
}
// 写入文件
File f = LittleFS.open("/data.csv", "a");
if (f) {
f.println("timestamp,adc_value");
for (int i = 0; i < 10; i++) {
f.print(millis()); f.print(",");
f.println(analogRead(A0));
}
f.close();
Serial.println("Data written to /data.csv");
}
// 读取并打印
f = LittleFS.open("/data.csv", "r");
if (f) {
Serial.println("\n--- /data.csv ---");
while (f.available()) {
Serial.write(f.read());
}
f.close();
}
// 检查剩余空间
Serial.printf("\nFree space: %d bytes\n", LittleFS.remaining());
}
void loop() {
// 主循环
}
```
---
## 例程 5:WiFi + MQTT 物联网节点
```cpp
// Nano RP2040 Connect - WiFi MQTT 客户端
#include <WiFiNINA.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_SSID";
const char* pass = "YOUR_PASSWORD";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient wifiClient;
PubSubClient mqtt(wifiClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message [");
Serial.print(topic);
Serial.print("]: ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
while (!Serial);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi OK");
mqtt.setServer(mqtt_server, 1883);
mqtt.setCallback(callback);
}
void reconnectMQTT() {
while (!mqtt.connected()) {
Serial.print("MQTT connecting...");
String clientId = "RP2040-";
clientId += String(random(0xffff), HEX);
if (mqtt.connect(clientId.c_str())) {
Serial.println("connected!");
mqtt.subscribe("rp2040/command");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
delay(2000);
}
}
}
void loop() {
if (!mqtt.connected()) reconnectMQTT();
mqtt.loop();
static unsigned long lastPub = 0;
if (millis() - lastPub > 5000) {
lastPub = millis();
int adc = analogRead(A0);
float voltage = adc * (3.3f / 4095.0f);
String payload = "{\"adc\":" + String(adc) +
",\"voltage\":" + String(voltage, 3) + "}";
mqtt.publish("rp2040/sensors", payload.c_str());
Serial.println("Published: " + payload);
}
}
```
> **编译提示**:Arduino IDE → `Tools → Board → Arduino Nano RP2040 Connect`,需安装 `Arduino Mbed OS RP2040 Boards`。WS2812 需安装 `Adafruit_NeoPixel` 库。MQTT 需 `PubSubClient` 库。
暂无参考文献