树莓派 Pico 是2020年树莓派基金会推出的微控制器(MCU)产品,其功能丰富且成本低,非常适合我们的项目。
我们很多人都喜欢使用 Arduino IDE 来对微控制器进行编程。
Python 和 C/C++ 都非常适合来进行 Pico 的编程。
详情见【树莓派 Pico 和 Pico W】
而下面的要介绍的是使用 Arduino IDE 来作为树莓派 Pico 的开发环境,就和开发 Arduino 程序一样去做 Pico 上的开发,将树莓派 Pico 集成到 Arduino 的生态中。这样做的原因之一是可以直接使用丰富的 Arduino 底层库、允许集成模块、传感器和其他复杂的东西,而无需从头开始编写所有代码。
所以事不宜迟,让我们开始吧!
通过菜单 Tools>Boards>Boards Manager 来搜索开发板,输入关键词 ‘pico’ 找到并下载安装到 Arduino IDE。
点击安装Arduino Mbed os RP2040 Boards
开发板,安装完成如上图
打开文件>示例>01.basic>Blink例程
源码如下
Blink
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
说明
LED_BUILTIN
对应Pico的GPIO25
这里要注意的是,当Pico开发板成功连接到电脑后,我们无须再选择串行端口。这是因为电脑在第一次上传程序时会写入.uf2文件到Pico开发板,之后电脑会自动选择串行端口。
将 Pico 连上电脑,菜单 Tools>Port 中选择 Pico 对应的 COM 端口。
在工具Tools>Boards>OS RP2040 Board中选择Pi Pico
,点击 Upload 上传到 Pico。
当 IDE 显示已经上传完成,就可以看到 Pico 板载的 LED 开始闪烁。
还有Fade示例也可用,GPIO9加一个灯实现对应的呼吸效果
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
参考文献: