買ってはみたもののイマイチ使いこなせてないRaspberry Pi Picoをどうにか使いこなしたい。micropythonでやるのが王道なんだろうけどマイコンはすっかりArduinoに馴染んでいるためArduino IDEでどうにかしたいところ。
まずは
Raspberry Pi Pico (RP2040) のArduino IDEでの使い方 | STEAM Tokyo
こちらを参考にしてIDEにRP2040デバイス用のArduinoコア、arduino-picoをインストールする。
手始めにファイル>スケッチ例>rp2040からLEDのFadeをロードしてみる。
int led = LED_BUILTIN; // 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 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness
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);
}これはなんの問題もなく動くね。
次にI2Cの制御としてDS3231のRTCをつないでみる。

ファイル>スケッチ例>DS3232>rtcTimeTempを試す。
元の内容は
// Arduino DS3232RTC Library
// https://github.com/JChristensen/DS3232RTC
// Copyright (C) 2019 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Example sketch to display time and temperature.
//
// Jack Christensen 24Sep2019
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
DS3232RTC myRTC;
void setup()
{
Serial.begin(115200);
myRTC.begin();
}
void loop()
{
display();
delay(10000);
}
// display time, date, temperature
void display()
{
char buf[40];
time_t t = myRTC.get();
float celsius = myRTC.temperature() / 4.0;
float fahrenheit = celsius * 9.0 / 5.0 + 32.0;
sprintf(buf, "%.2d:%.2d:%.2d %.2d%s%d ",
hour(t), minute(t), second(t), day(t), monthShortStr(month(t)), year(t));
Serial.print(buf);
Serial.print(celsius);
Serial.print("C ");
Serial.print(fahrenheit);
Serial.println("F");
}なのだが、これに
#include <Wire.h>
を頭に付け足し、
シリアル通信の前に
void setup()
{
Wire.setSDA(16);
Wire.setSCL(17);
Wire.begin();
Serial.begin(115200);
myRTC.begin();
}とI2Cのためのピンを設定しておく。この場合、21ピン(GP16)をSDA、22ピン(GP17)をSCLに設定している。
これでエラーなくシリアルコンソールに時間、温度を表示できた。RTCへの時間あわせをしていないので、00:00:10からスタートした。時刻設定のコードも必要だが、まずはI2Cが使えたということで良しとしよう。
