介绍
Arduino Nano 33 IoT,基于SAMD21G18主控(ARM Cortex-M0+ 48MHz) + NINA-W102 WiFi/BLE模块,256KB Flash,32KB SRAM,内置6轴IMU(LSM6DS3),安全加密芯片(ATECC608A),支持Arduino IoT Cloud。3.3V逻辑,Micro-USB,尺寸18×45mm。相比Nano 33 BLE,多了WiFi功能,适合物联网应用
Arduino Nano 33 IoT,基于SAMD21G18主控(ARM Cortex-M0+ 48MHz) + NINA-W102 WiFi/BLE模块,256KB Flash,32KB SRAM,内置6轴IMU(LSM6DS3),安全加密芯片(ATECC608A),支持Arduino IoT Cloud。3.3V逻辑,Micro-USB,尺寸18×45mm。相比Nano 33 BLE,多了WiFi功能,适合物联网应用
| 参数 | 值 |
|---|---|
| DAC | 1路10位(A0) |
| I2C | 1路 |
| PWM | 11路 |
| SPI | 1路 |
| SRAM | 32KB |
| Flash | 256KB |
| 串口 | 1路硬UART |
| 内核 | ARM Cortex-M0+ 48MHz |
| 尺寸 | 18mm×45mm |
| 无线 | NINA-W102: WiFi 802.11 b/g/n (2.4GHz) + BLE 4.2 |
| 重量 | 约5g |
| 数字IO | 14路(全部可中断) |
| USB接口 | Micro-USB(Native USB) |
| 主控芯片 | SAMD21G18A (Microchip) |
| 安全加密 | ATECC608A 安全认证芯片 |
| 工作电压 | 3.3V(逻辑) |
| 模拟输入 | 8路(12位ADC) |
| 输入电压 | 5V(USB)/4.5-21V(VIN) |
| 内置传感器 | LSM6DS3(6轴IMU: 加速度+陀螺仪) |
# Arduino Nano 33 IoT (SAMD21 + NINA-W102) 代码例程
## 例程 1:WiFi 连接 + HTTP 请求(GET)
```cpp
// Nano 33 IoT - WiFi 连接 + HTTP GET 请求
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
const char* ssid = "YOUR_SSID";
const char* pass = "YOUR_PASSWORD";
const char* server = "api.github.com";
WiFiClient wifi;
HttpClient client(wifi, server, 443); // HTTPS
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
// 连接 WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, pass);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.print(".");
delay(1000);
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("\nWiFi connected!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
} else {
Serial.println("\nWiFi connection failed!");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Making HTTPS GET request...");
client.get("/users/octocat/repos");
int statusCode = client.responseStatusCode();
Serial.print("Status: ");
Serial.println(statusCode);
String response = client.responseBody();
Serial.print("Response: ");
Serial.println(response.substring(0, 200)); // 前200字符
}
delay(30000); // 每30秒请求一次
}
```
---
## 例程 2:Arduino IoT Cloud 最小示例
```cpp
// Nano 33 IoT - Arduino IoT Cloud 连接示例
// 在 Arduino IoT Cloud (create.arduino.cc) 创建 Thing 后自动生成代码框架
#include "thingProperties.h"
void setup() {
Serial.begin(115200);
delay(1500); // 等待串口稳定
// 初始化属性(由 IoT Cloud 自动生成)
initProperties();
// 连接 WiFi 和 IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// 调试信息
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// 你的代码(读取传感器、控制执行器等)
// 示例:每隔一段时间更新云端变量
static unsigned long lastUpdate = 0;
if (millis() - lastUpdate > 5000) {
lastUpdate = millis();
// cloudVariable = analogRead(A0); // 自动同步到云端
}
}
// IoT Cloud 回调(当云端变量变化时触发)
void onCloudVariableChange() {
Serial.println("Cloud variable changed!");
// 响应云端指令
}
```
---
## 例程 3:DAC 输出 — A0 模拟电压输出
```cpp
// Nano 33 IoT - A0 DAC 输出(10位,0-3.3V)
// 此功能为 Nano 33 IoT 独有(Nano 33 BLE 无DAC)
void setup() {
Serial.begin(115200);
analogWriteResolution(10); // 10位DAC
}
void loop() {
// 锯齿波
for (int i = 0; i < 1024; i += 4) {
analogWrite(A0, i);
delayMicroseconds(100);
}
// 正弦波(查找表)
static const int sineTable[] = {
512,530,548,565,583,600,617,634,650,666,
681,696,710,723,736,748,759,769,778,786,
794,800,805,809,813,815,817,817,817,815,
813,809,805,800,794,786,778,769,759,748,
736,723,710,696,681,666,650,634,617,600,
583,565,548,530,512,493,475,458,440,423,
406,389,373,357,342,327,313,300,287,275,
264,254,245,237,229,223,218,214,210,208,
206,206,206,208,210,214,218,223,229,237,
245,254,264,275,287,300,313,327,342,357,
373,389,406,423,440,458,475,493
};
for (int i = 0; i < 100; i++) {
analogWrite(A0, sineTable[i]);
delayMicroseconds(200);
}
}
```
---
## 例程 4:6轴IMU 数据读取
```cpp
// Nano 33 IoT - LSM6DS3 加速度计 + 陀螺仪
#include <Arduino_LSM6DS3.h>
void setup() {
Serial.begin(115200);
while (!Serial);
if (!IMU.begin()) {
Serial.println("IMU init failed!");
while (1);
}
Serial.println("IMU ready.");
Serial.println("AccelX,AccelY,AccelZ,GyroX,GyroY,GyroZ");
}
void loop() {
float ax, ay, az, gx, gy, gz;
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
IMU.readAcceleration(ax, ay, az);
IMU.readGyroscope(gx, gy, gz);
// CSV 格式输出(便于串口绘图器)
Serial.print(ax); Serial.print(",");
Serial.print(ay); Serial.print(",");
Serial.print(az); Serial.print(",");
Serial.print(gx); Serial.print(",");
Serial.print(gy); Serial.print(",");
Serial.println(gz);
}
delay(10);
}
```
---
## 例程 5:WiFi Web 服务器(控制 GPIO)
```cpp
// Nano 33 IoT - 简易 Web 服务器控制 LED
#include <WiFiNINA.h>
const char* ssid = "YOUR_SSID";
const char* pass = "YOUR_PASSWORD";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("Web server at: http://");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (!client) return;
Serial.println("New client.");
String request = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n') break;
}
}
// 解析请求
int ledState = LOW;
if (request.indexOf("GET /on") >= 0) {
digitalWrite(LED_BUILTIN, HIGH);
ledState = HIGH;
} else if (request.indexOf("GET /off") >= 0) {
digitalWrite(LED_BUILTIN, LOW);
ledState = LOW;
}
// 发送 HTML 页面
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html; charset=utf-8");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html><head><meta charset='UTF-8'>");
client.println("<meta name='viewport' content='width=device-width,initial-scale=1'>");
client.println("<title>Nano 33 IoT</title></head><body>");
client.println("<h1>Nano 33 IoT Web Server</h1>");
client.printf("<p>LED: <strong>%s</strong></p>", ledState ? "ON" : "OFF");
client.println("<p><a href='/on'><button>ON</button></a> ");
client.println("<a href='/off'><button>OFF</button></a></p>");
client.println("</body></html>");
delay(10);
client.stop();
Serial.println("Client disconnected.");
}
```
> **编译提示**:Arduino IDE → `Tools → Board → Arduino Nano 33 IoT`,需安装 `Arduino SAMD Boards (32-bits ARM Cortex-M0+)`。WiFi 相关需安装 `WiFiNINA` 库。
暂无参考文献