【树莓派 Pico 基于Arduino IDE编程开发】

1.前言

树莓派 Pico 是2020年树莓派基金会推出的微控制器(MCU)产品,其功能丰富且成本低,非常适合我们的项目。
在这里插入图片描述

我们很多人都喜欢😍😍😍使用 Arduino IDE 来对微控制器进行编程。
在这里插入图片描述

Python 和 C/C++ 都非常适合来进行 Pico 的编程。
详情见【树莓派 Pico 和 Pico W】

在这里插入图片描述

2. 目标

而下面的要介绍的是使用 Arduino IDE 来作为树莓派 Pico 的开发环境,就和开发 Arduino 程序一样去做 Pico 上的开发,将树莓派 Pico 集成到 Arduino 的生态中。这样做的原因之一是可以直接使用丰富的 Arduino 底层库、允许集成模块、传感器和其他复杂的东西,而无需从头开始编写所有代码。
😀😀😀
所以事不宜迟,让我们开始吧!
👻👻👻

3. 准备好 Arduino IDE

3.1 安装Pico开发板

通过菜单 Tools>Boards>Boards Manager 来搜索开发板,输入关键词 ‘pico’ 找到并下载安装到 Arduino IDE。
在这里插入图片描述
点击安装Arduino Mbed os RP2040 Boards 开发板,安装完成如上图

3.2 上传示例程序

打开文件>示例>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。
在这里插入图片描述

4. 实验效果

当 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);
}

参考文献:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2345VOR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值