|
视频合集:
视频切片可参考大学堂视频合集:
【Follow me第二季第4期】ARDUINO NANO RP2040 CONNECT-EEWORLD大学堂
代码下载合集:
Follow me第二季第4期代码合集ARDUINO NANO RP2040 CONNECT-嵌入式开发相关资料下载-EEWORLD下载中心
物料 | 作用 | 价格 |
ARDUINO NANO RP2040 CONNECT | Arduino主板 | ¥239.32 |
MCP9808 TEMP I2C BREAKOUT BRD | MCP9808温度模块 | ¥41.71 |
ARDUINO NANO STACKABLE HEADERS | Nano用双向长排针 | ¥14.25 |
物料 | 作用 | 价格 |
USB数据线 | 连接Arduino与电脑 | |
杜邦线若干 | 连接各个模块 | |
SSD1306模块 | OLED显示屏 | |
BMP085模块 | 测量气压强度 | |
继电器模组 | IO控制大电流设备 | |
百为语音播放模块 | 播放语音助手的提示音 | |
TTS模块 | 文字转语音模块 | |
RP2040 pin number |
RP2040 GPIO | pinMode | 功能 | 是否支持PWM | 排插 | 硬件原理图 | 备注 |
2 | GPIO0 | UART1 TX | JP3.1 | TX | |||
3 | GPIO1 | UART1 RX | JP3.2 | RX | |||
4 | GPIO2 | 直连+TP | NINA GPIO0 | ||||
5 | GPIO3 | 直连+TP | RSTN_NINA | ||||
6 | GPIO4 | 12 | JP3.15 | SPI RX? | |||
7 | GPIO5 | 10 | PWM | JP3.13 | D10 | ||
8 | GPIO6 | 13 | LED_BUILTIN | PWM | JP2.1 | SPI CLK | |
9 | GPIO7 | 11 | JP3.14 | SPI TX? | |||
10 | |||||||
11 | GPIO8 | UART1 TX | 直连+TP | SPI1 CIPO | |||
12 | GPIO9 | UART1 RX | 直连+TP | SPI1 CS | |||
13 | GPIO10 | 直连 | SPI1 ACK | 缺TP | |||
14 | GPIO11 | 直连 | SPI1 COPI | 缺TP | |||
15 | GPIO12 | 18 | I2C SDA | JP2.8 | A4 | ||
16 | GPIO13 | 19 | I2C SCL | JP2.9 | A5 | ||
17 | GPIO14 | ||||||
18 | GPIO15 | 3 | PWM | JP3.6 | D3 | ||
27 | GPIO16 | 4 | PWM | JP3.7 | D4 | ||
28 | GPIO17 | 5 | PWM | JP3.8 | D5 | ||
29 | GPIO18 | 6 | PWM | JP3.9 | D6 | ||
30 | GPIO19 | 7 | PWM | JP3.10 | D7 | ||
31 | GPIO20 | 8 | PWM | JP3.11 | D8 | ||
32 | GPIO21 | 9 | PWM | JP3.12 | D9 | ||
34 | GPIO22 | PDMDIN | 直连 | 缺TP | |||
35 | GPIO23 | PDMCLK | 直连 | 缺TP | |||
36 | GPIO24 | 直连 | INT1 | 缺TP | |||
37 | GPIO25 | 2 | PWM | JP3.5 | D2 | ||
38 | GPIO26 | 14 | ADC0 | JP2.4 | |||
39 | GPIO27 | 15 | ADC1 | JP2.5 | |||
40 | GPIO28 | 16 | ADC2 | JP2.6 | |||
41 | GPIO29 | 17 | ADC3 | JP2.7 | |||
56 | JP2.13 | SPI_CSn |
硬件 | Arduino IDE初始化代码 | RP2040 UART控制器 | ||
JP3.1 | Serial1.begin(115200); | UART0 | TX | GPIO0 |
JP3.2 | RX | GPIO1 | ||
USB CDC | Serial.begin(115200); | UART1 | TX | endpoint |
RX | endpoint | |||
或者 | ||||
硬件 | Arduino IDE初始化代码 | RP2040 UART控制器 | ||
JP3.1 | Serial1.begin(115200); | UART0 | TX | GPIO0 |
JP3.2 | RX | GPIO1 | ||
USB CDC | Serial.begin(115200); | ? | TX | endpoint |
RX | endpoint | |||
? | ? | UART1 | TX | ? |
? | RX | ? |
Index | R | G | B |
0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 |
2 | 0 | 1 | 0 |
3 | 0 | 1 | 1 |
4 | 1 | 0 | 0 |
5 | 1 | 0 | 1 |
6 | 1 | 1 | 0 |
7 | 1 | 1 | 1 |
// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>
#include "WiFiNINA.h"
#define led1 LEDR
#define led2 LEDG
#define led3 LEDB
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// Setup the 3 pins as OUTPUT
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Serial1.println("\n~~~~~~~~~~ RGB Blink Demo on Arduino Nano RP2040 ~~~~~~~~~~");
Serial.println("Hello DigiKey & EEWorld!");
Serial1.println("Hello DigiKey & EEWorld!");
Serial1.println("Please input the index number to blink the RGB LED:");
Serial1.println("IndexRGB");
Serial1.println("0:000");
Serial1.println("1:001");
Serial1.println("2:010");
Serial1.println("3:011");
Serial1.println("4:100");
Serial1.println("5:101");
Serial1.println("6:110");
Serial1.println("7:111");
Scheduler.startLoop(loop1);
Scheduler.startLoop(loop2);
}
/*
* Name: set_rgb
* Para: rgb index
* bit2: red led
* bit1: green led
* bit0: blue led
*/
void set_rgb(char c) {
int r, g, b;
r = (c >> 2) % 2;
g = (c >> 1) % 2;
b = (c % 2);
digitalWrite(led1, (PinStatus)r);
digitalWrite(led2, (PinStatus)g);
digitalWrite(led3, (PinStatus)b);
}
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
set_rgb(c);
}
// IMPORTANT:
// We must call 'yield' at a regular basis to pass
// control to other tasks.
yield();
}
void loop1() {
set_rgb(4);
delay(1000);
set_rgb(2);
delay(1000);
set_rgb(1);
delay(1000);
}
void loop2() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
#include <Arduino_LSM6DSOX.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1)
;
}
}
void loop() {
// put your main code here, to run repeatedly:
float accx, accy, accz;
float picth, roll, yaw;
float temperature;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(accx, accy, accz);
Serial.print(accx);
Serial.print('\t');
Serial.print(accy);
Serial.print('\t');
Serial.print(accz);
Serial.print('\t');
}
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(picth, roll, yaw);
Serial.print(picth);
Serial.print('\t');
Serial.print(roll);
Serial.print('\t');
Serial.print(yaw);
Serial.print('\t');
}
#if 1
if (IMU.temperatureAvailable()) {
IMU.readTemperatureFloat(temperature);
Serial.print(temperature);
}
#endif
Serial.println();
delay(100);
}
#include <WiFiNINA.h>
#include <PDM.h>
bool LED_SWITCH = false;
// default number of output channels
static const char channels = 1;
// default PCM output frequency
static const int frequency = 14000;
// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[1024];
#define PIN_LED (13u)
#define PIN_S (10u)
// Number of audio samples read
volatile int samplesRead;
void setup() {
Serial.begin(9600);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_S, OUTPUT);
//while (!Serial);
Serial.println("***************************************************************");
// Configure the data receive callback
PDM.onReceive(onPDMdata);
// Optionally set the gain
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shields
// PDM.setGain(30);
// Initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shields
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1)
;
}
}
void loop() {
// Wait for samples to be read
if (samplesRead) {
// Print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
if (channels == 2) {
Serial.print("L:");
Serial.print(sampleBuffer[i]);
Serial.print(" R:");
i++;
}
Serial.println(sampleBuffer[i]);
if (sampleBuffer[i] > 10000 || sampleBuffer[i] <= -10000) {
LED_SWITCH = !LED_SWITCH;
if (LED_SWITCH) {
Serial.println();
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_S, HIGH);
Serial.println("ON!");
Serial.println();
delay(500);
} else {
Serial.println();
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_S, LOW);
Serial.println("OFF!");
Serial.println();
delay(500);
}
}
}
// Clear the read count
samplesRead = 0;
}
}
/**
Callback function to process the data from the PDM microphone.
NOTE: This callback is executed as part of an ISR.
Therefore using `Serial` to print messages inside this function isn't supported.
* */
void onPDMdata() {
// Query the number of available bytes
int bytesAvailable = PDM.available();
// Read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
#include <WiFiNINA.h>
#include <PDM.h>
bool LED_SWITCH = false;
// default number of output channels
static const char channels = 1;
// PCM output frequency
static const int frequency = 14000;
// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[1024];
short avg;
long minv,maxv;
short led255;
#define PIN_LED (13u)
#define PIN_S (10u)
// Number of audio samples read
volatile int samplesRead;
/*
* Name: set_rgb
* Para: rgb index
* bit2: red led
* bit1: green led
* bit0: blue led
*/
void set_rgb(char c) {
int r, g, b;
r = (c >> 2) % 2;
g = (c >> 1) % 2;
b = (c % 2);
digitalWrite(LEDR, (PinStatus)r);
digitalWrite(LEDG, (PinStatus)g);
digitalWrite(LEDB, (PinStatus)b);
}
/**
Callback function to process the data from the PDM microphone.
NOTE: This callback is executed as part of an ISR.
Therefore using `Serial` to print messages inside this function isn't supported.
* */
void onPDMdata() {
// Query the number of available bytes
int bytesAvailable = PDM.available();
// Read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
void setup() {
Serial.begin(9600);
//pinMode(PIN_LED, OUTPUT);
pinMode(PIN_S, OUTPUT);
while (!Serial)
;
Serial.println("***************************************************************");
// Configure the data receive callback
PDM.onReceive(onPDMdata);
// Optionally set the gain
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shields
// PDM.setGain(30);
// Initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shields
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1)
;
}
minv=maxv=0;
}
void loop() {
// Wait for samples to be read
if (samplesRead) {
//Serial.println(samplesRead);
//return;
avg = 0;
// Print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
if (channels == 2) {
Serial.print("L:");
Serial.print(sampleBuffer[i]);
Serial.print(" R:");
i++;
}
//Serial.print(sampleBuffer[i]);
//Serial.print('*');
avg += (sampleBuffer[i] - avg) / (i + 1);
#ifdef WHATEVEREEEEEE
//if (sampleBuffer[i] > 10000 || sampleBuffer[i] <= -10000) {
if (abs(sampleBuffer[i]) > 10000) {
LED_SWITCH = !LED_SWITCH;
if (LED_SWITCH) {
Serial.println();
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_S, HIGH);
Serial.println("ON!");
Serial.println();
delay(10000);
} else {
Serial.println();
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_S, LOW);
Serial.println("OFF!");
Serial.println();
delay(10000);
}
}
#endif
}
//Serial.println(avg);
led255 = map(abs(avg), 0, 65535, 0, 255);
if(led255 > maxv)
{
maxv = led255;
}
if(minv > led255)
{
minv = led255;
}
Serial.print(minv);
Serial.print('\t');
Serial.print(led255);
Serial.print('\t');
Serial.println(maxv);
analogWrite(PIN_LED, led255);
if (led255 > 15) {
set_rgb(7);
//digitalWrite(PIN_LED, LOW);
} else if (led255 > 10) {
set_rgb(4);
//digitalWrite(PIN_LED, LOW);
} else if (led255 > 5) {
set_rgb(2);
//digitalWrite(PIN_LED, LOW);
} else {
set_rgb(1);
//digitalWrite(PIN_LED, HIGH);
}
// Clear the read count
samplesRead = 0;
}
}
#include <WiFiNINA.h>
#include <PDM.h>
#include <Adafruit_MCP9808.h>
Adafruit_MCP9808 sensor;
bool LED_SWITCH = false;
// default number of output channels
static const char channels = 1;
// PCM output frequency
static const int frequency = 14000;
// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[1024];
#define PIN_LED (13u)
#define PIN_S (10u)
// Number of audio samples read
volatile int samplesRead;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
//语音模块默认 GBK 编码, arduino 默认是 utf 编码
//需要先发<C>1 设置语音模块编码
Serial1.print("<C>1");
delay(500);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_S, OUTPUT);
//while (!Serial)
;
Serial.println("***************************************************************");
// Configure the data receive callback
PDM.onReceive(onPDMdata);
// Optionally set the gain
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shields
// PDM.setGain(30);
// Initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shields
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1)
;
}
if (!sensor.begin()) {
Serial.println("Failed to find Adafruit MCP9808 chip");
while (1) { delay(10); }
}
Serial.println("MCP9808 Found!");
Serial1.println("初始化完成");
}
void loop() {
char buffer[500];
sensors_event_t event;
sensor.getEvent(&event);
// Wait for samples to be read
if (samplesRead) {
//Serial.println(samplesRead);
//return;
// Print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
if (channels == 2) {
Serial.print("L:");
Serial.print(sampleBuffer[i]);
Serial.print(" R:");
i++;
}
//Serial.println(sampleBuffer[i]);
if (sampleBuffer[i] > 10000 || sampleBuffer[i] <= -10000) {
//Serial.print(event.temperature);
memset(buffer, 0, sizeof(buffer));
//event.temperature = -5.8;
if (event.temperature > 0) {
sprintf(buffer, "现在温度是%.1f摄氏度", event.temperature);
} else {
sprintf(buffer, "现在温度是零下%.1f摄氏度",-1.0*event.temperature);
}
Serial.println(buffer);
Serial1.print(buffer);
LED_SWITCH = !LED_SWITCH;
if (LED_SWITCH) {
Serial.println();
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_S, HIGH);
Serial.println("ON!");
Serial.println();
delay(6000);
} else {
Serial.println();
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_S, LOW);
Serial.println("OFF!");
Serial.println();
delay(6000);
}
}
}
// Clear the read count
samplesRead = 0;
}
//yield();
//delay(100);
}
#include <Arduino_LSM6DSOX.h>
#include "PluggableUSBHID.h"
#include "USBMouse.h"
USBMouse Mouse;
#define STEP 80
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1)
;
}
}
void loop() {
// put your main code here, to run repeatedly:
float accx, accy, accz;
float picth, roll, yaw;
float temperature;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(accx, accy, accz);
Serial.print(accx);
Serial.print('\t');
Serial.print(accy);
Serial.print('\t');
Serial.print(accz);
Serial.print('\t');
}
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(picth, roll, yaw);
Serial.print(picth);
Serial.print('\t');
Serial.print(roll);
Serial.print('\t');
Serial.print(yaw);
Serial.print('\t');
}
#if 1
if (IMU.temperatureAvailable()) {
IMU.readTemperatureFloat(temperature);
Serial.print(temperature);
}
#endif
Serial.println();
if(accz > 0.2)
{
Mouse.move(0,accy*STEP);
Mouse.move(-accx*STEP, 0);
}
delay(25);
}
const char voicecmd[21][7] = {
{ 0x7E, 0x05, 0x41, 0x00, 0x01, 0x45, 0xEF }, //播放0001
{ 0x7E, 0x05, 0x41, 0x00, 0x02, 0x46, 0xEF }, //播放0002
{ 0x7E, 0x05, 0x41, 0x00, 0x03, 0x47, 0xEF }, //播放0003
{ 0x7E, 0x05, 0x41, 0x00, 0x04, 0x40, 0xEF }, //播放0004
{ 0x7E, 0x05, 0x41, 0x00, 0x05, 0x41, 0xEF }, //播放0005
{ 0x7E, 0x05, 0x41, 0x00, 0x06, 0x42, 0xEF }, //播放0006
{ 0x7E, 0x05, 0x41, 0x00, 0x07, 0x43, 0xEF }, //播放0007
{ 0x7E, 0x05, 0x41, 0x00, 0x08, 0x4C, 0xEF }, //播放0008
{ 0x7E, 0x05, 0x41, 0x00, 0x09, 0x4D, 0xEF }, //播放0009
{ 0x7E, 0x05, 0x41, 0x00, 0x0A, 0x4E, 0xEF }, //播放0010
{ 0x7E, 0x05, 0x41, 0x00, 0x0B, 0x4F, 0xEF }, //播放0011
{ 0x7E, 0x05, 0x41, 0x00, 0x0C, 0x48, 0xEF }, //播放0012
{ 0x7E, 0x05, 0x41, 0x00, 0x0D, 0x49, 0xEF }, //播放0013
{ 0x7E, 0x05, 0x41, 0x00, 0x0E, 0x4A, 0xEF }, //播放0014
{ 0x7E, 0x05, 0x41, 0x00, 0x0F, 0x4B, 0xEF }, //播放0015
{ 0x7E, 0x05, 0x41, 0x00, 0x10, 0x54, 0xEF }, //播放0016
{ 0x7E, 0x05, 0x41, 0x00, 0x11, 0x55, 0xEF }, //播放0017
{ 0x7E, 0x05, 0x41, 0x00, 0x12, 0x56, 0xEF }, //播放0018
{ 0x7E, 0x05, 0x41, 0x00, 0x13, 0x57, 0xEF }, //播放0019
{ 0x7E, 0x05, 0x41, 0x00, 0x14, 0x50, 0xEF }, //播放0020
{ 0x7E, 0x05, 0x41, 0x00, 0x15, 0x51, 0xEF }, //播放0021
};
// Callback function for VR engine
void VRCallback(int nFlag, int nID, int nScore, int nSG, int nEnergy) {
if (nFlag == DSpotterSDKHL::InitSuccess) {
//ToDo
Serial.println("InitSuccess");
} else if (nFlag == DSpotterSDKHL::GetResult) {
/*
When getting an recognition result,
the following index and scores are also return to the VRCallback function:
nID The result command id
nScore nScore is used to evaluate how good or bad the result is.
The higher the score, the more similar the voice and the result command are.
nSG nSG is the gap between the voice and non-command (Silence/Garbage) models.
The higher the score, the less similar the voice and non-command (Silence/Garbage) models are.
nEnergy nEnergy is the voice energy level.
The higher the score, the louder the voice.
*/
switch (nID) {
case COMMAND_LED_GREEN:
Serial.println("Copy That: LED_GREEN");
Serial1.write(voicecmd[15], 7);
LED_RGB_Green();
break;
case COMMAND_LED_RED:
Serial.println("Copy That: LED_RED");
Serial1.write(voicecmd[15], 7);
LED_RGB_Red();
break;
case COMMAND_LED_BLUE:
Serial.println("Copy That: LED_BLUE");
Serial1.write(voicecmd[15], 7);
LED_RGB_Blue();
Serial.println("Switch ON!");
digitalWrite(PIN_S, HIGH);
break;
case COMMAND_LED_OFF:
Serial.println("Copy That: LED_OFF");
Serial1.write(voicecmd[16], 7);
LED_RGB_Off();
Serial.println("Switch ON!");
digitalWrite(PIN_S, LOW);
break;
default:
Serial.println("unkown nID!");
break;
}
} else if (nFlag == DSpotterSDKHL::ChangeStage) {
switch (nID) {
case DSpotterSDKHL::TriggerStage:
Serial.println("I'm waiting for\n\t\"Hey Arduino\"");
LED_BUILTIN_Off();
//LED_RGB_Off();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("You can say:");
display.setTextSize(2);
display.setCursor(0, 16);
display.println("Hey");
display.println("Arduino");
display.display(); // actually display all of the above
break;
case DSpotterSDKHL::CommandStage:
Serial.println("I'm here! You can say:\n\t\"LED red\"\n\t\"LED blue\"\n\t\"LED green\"");
Serial1.write(voicecmd[17], 7);
LED_BUILTIN_On();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("You can say:");
display.setTextSize(2);
display.setCursor(0, 16);
display.println("LED red");
display.println("LED blue");
display.println("LED green");
display.display(); // actually display all of the above
break;
default:
break;
}
} else if (nFlag == DSpotterSDKHL::GetError) {
if (nID == DSpotterSDKHL::LicenseFailed) {
Serial.print("DSpotter license failed! The serial number of your device is ");
Serial.println(DSpotterSDKHL::GetSerialNumber());
Serial.println("\nPlease get your license via https://tool.cyberon.com.tw/ArduinoDSpotterAuth/CTMain.php\n\n");
}
g_oDSpotterSDKHL.Release();
while (1)
; //hang loop
} else if (nFlag == DSpotterSDKHL::LostRecordFrame) {
//ToDo
//Serial.println("LostRecordFrame");
}
}