Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

Unit Watering Arduino 使用ガイド

1. 準備作業

  • 環境設定: Arduino IDE 入門チュートリアルを参照して IDE のインストールを完了し、実際に使用する開発ボードに応じて対応するボードマネージャーとドライバライブラリをインストールしてください。
  • 必要なドライバライブラリ:
  • 必要なハードウェア製品:

2. 注意事項

水の安全
ポンプは手動で停止する必要があり、空運転は禁止です。初回テスト時は吸水管が完全に水に浸かっていることを確認し、水が Unit の回路基板や Grove コネクターに入らないようにしてください。
ピンの互換性
各ホストのピン配置は異なるため、ユーザーがより便利に使用できるよう、M5Stack はピン互換表を公式に提供しています。実際のピン接続に合わせてサンプルプログラムを変更してください。

3. サンプルプログラム

  • 本チュートリアルでは CoreS3 と Unit Watering を PORT.B に接続して使用します。白い線は ADC 入力 G8、黄色い線はポンプ制御 PUMP_EN で、G9 に対応します。
  • プログラムは analogReadMilliVolts() でアナログ電圧を読み取り、1550 mV と 1450 mV の 2 つのしきい値で土壌状態を判定します。電圧が 1550 mV 以上の場合は DRY、1550 mV 未満かつ 1450 mV 以上の場合は WET、1450 mV 未満の場合は SATURATED です。
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include <M5Unified.h>

M5Canvas canvas(&M5.Display);

constexpr int kDryMv = 1550;
constexpr int kWetMv = 1450;
constexpr int kBtnX = 188, kBtnY = 168, kBtnW = 106, kBtnH = 48;
constexpr int kTouchPad = 12;  // Expand the hit area for finger touches.
constexpr uint16_t kBg = 0x0883, kSurface = 0x10E5, kAlt = 0x1948;
constexpr uint16_t kLine = 0x29EB, kText = 0xF7BF, kMuted = 0x8CD6;
constexpr uint16_t kGreen = 0x4ED4, kOrange = 0xFDAA, kBlue = 0x6D5F;
constexpr uint16_t kStartGreen = 0x1BCB, kStopRed = 0xF800;

const char* const kStateName[] = {"SATURATED", "WET", "DRY"};
const uint16_t kStateColor[] = {kBlue, kGreen, kOrange};

int sensorPin = -1, pumpPin = -1;
int rawAdc = 0, milliVolts = 0;
bool pumpOn = false;
uint32_t lastUpdateMs = 0, lastLogMs = 0;

void readSensor() {
  uint32_t raw = 0, mv = 0;
  for (int i = 0; i < 8; ++i) {
    raw += analogRead(sensorPin);
    mv += analogReadMilliVolts(sensorPin);
    delay(1);
  }
  rawAdc = raw / 8;
  milliVolts = mv / 8;
}

void setPump(bool on) {
  pumpOn = on;
  digitalWrite(pumpPin, on);
  lastUpdateMs = millis() - 250;
  Serial.println(on ? "PUMP=ON" : "PUMP=OFF");
}

bool hit(int x, int y, int rx, int ry, int rw, int rh) {
  return x >= rx && x < rx + rw && y >= ry && y < ry + rh;
}

void drawText(const char* text, int x, int y, textdatum_t datum,
              const lgfx::v1::IFont* font, uint16_t color, uint16_t bg) {
  canvas.setTextDatum(datum);
  canvas.setFont(font);
  canvas.setTextColor(color, bg);
  canvas.drawString(text, x, y);
}

void drawDrop(int x, int y, int r, uint16_t color) {
  canvas.fillTriangle(x, y - r, x - r * 3 / 5, y + 2, x + r * 3 / 5, y + 2, color);
  canvas.fillCircle(x, y + 3, r * 3 / 5, color);
}

void drawPower(int x, int y, int r, uint16_t color) {
  canvas.fillCircle(x, y, r, kAlt);
  canvas.drawCircle(x, y, r, kLine);
  canvas.drawCircle(x, y, r / 2, color);
  canvas.fillRect(x - 1, y - r / 2 - 3, 2, r / 2 + 3, color);
}

void drawScreen() {
  const int state = milliVolts >= kDryMv ? 2 : (milliVolts >= kWetMv ? 1 : 0);
  const uint16_t stateColor = kStateColor[state];
  const auto small = &fonts::FreeSans9pt7b;
  const auto medium = &fonts::FreeSansBold12pt7b;
  const auto large = &fonts::FreeSansBold18pt7b;
  char adcText[16], voltText[16];
  snprintf(adcText, sizeof(adcText), "ADC %d", rawAdc);
  snprintf(voltText, sizeof(voltText), "%d mV", milliVolts);

  canvas.fillSprite(kBg);
  canvas.fillRoundRect(14, 9, 4, 18, 2, kGreen);
  drawText("Unit Watering", 26, 9, textdatum_t::top_left, medium, kText, kBg);

  canvas.fillRoundRect(12, 46, 296, 94, 14, kSurface);
  canvas.drawRoundRect(12, 46, 296, 94, 14, kLine);
  drawDrop(36, 65, 7, stateColor);
  drawText("SOIL STATE", 50, 57, textdatum_t::top_left, small, kMuted, kSurface);
  drawText(adcText, 294, 57, textdatum_t::top_right, small, kMuted, kSurface);
  // SATURATED uses the smaller font so it fits beside the voltage.
  drawText(kStateName[state], 26, 105, textdatum_t::middle_left,
           state == 0 ? medium : large, stateColor, kSurface);
  drawText(voltText, 294, 105, textdatum_t::middle_right, medium, kText, kSurface);

  canvas.fillRoundRect(12, 146, 296, 86, 14, kSurface);
  canvas.drawRoundRect(12, 146, 296, 86, 14, kLine);
  drawPower(40, 192, 18, pumpOn ? kGreen : kMuted);
  drawText("PUMP", 68, 158, textdatum_t::top_left, small, kMuted, kSurface);
  drawText(pumpOn ? "ON" : "OFF", 88, 198, textdatum_t::middle_left, large,
           pumpOn ? kGreen : kMuted, kSurface);

  const uint16_t buttonColor = pumpOn ? kStopRed : kStartGreen;
  canvas.fillRoundRect(kBtnX, kBtnY, kBtnW, kBtnH, 12, buttonColor);
  drawText(pumpOn ? "STOP" : "START", 241, 192, textdatum_t::middle_center,
           medium, TFT_WHITE, buttonColor);
  canvas.pushSprite(0, 0);
}

void setup() {
  M5.begin();
  Serial.begin(115200);
  canvas.setColorDepth(16);
  canvas.createSprite(M5.Display.width(), M5.Display.height());
  canvas.setTextWrap(false);

  // CoreS3 PORT.B: G8 ADC, G9 PUMP_EN.
  sensorPin = M5.getPin(m5::pin_name_t::port_b_pin1);
  pumpPin = M5.getPin(m5::pin_name_t::port_b_pin2);
  pinMode(sensorPin, INPUT);
  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, LOW);
  analogReadResolution(12);
  analogSetPinAttenuation(sensorPin, ADC_11db);

  readSensor();
  drawScreen();
  lastUpdateMs = lastLogMs = millis();
}

void loop() {
  M5.update();
  auto touch = M5.Touch.getDetail();
  if (touch.wasPressed() &&
      hit(touch.x, touch.y, kBtnX - kTouchPad, kBtnY - kTouchPad,
          kBtnW + kTouchPad * 2, kBtnH + kTouchPad * 2)) {
    setPump(!pumpOn);
  }

  if (millis() - lastUpdateMs >= 250) {
    lastUpdateMs = millis();
    readSensor();
    drawScreen();
  }
  if (millis() - lastLogMs >= 1000) {
    lastLogMs = millis();
    const int state = milliVolts >= kDryMv ? 2 : (milliVolts >= kWetMv ? 1 : 0);
    Serial.printf("ADC=%d, SOIL=%s, MV=%d, PUMP=%s\n", rawAdc,
                  kStateName[state], milliVolts, pumpOn ? "ON" : "OFF");
  }
  delay(5);
}

4. コンパイルとアップロード

  • 上記のサンプルコードをプロジェクトのコード領域にコピー&ペーストし、デバイスのポートを選択して(詳細はプログラムのコンパイルと書き込みを参照)、Arduino IDE 左上のコンパイルとアップロードボタンをクリックし、プログラムのコンパイルとデバイスへの書き込みが完了するまで待ちます。

5. 動作効果

  • 画面には ADC 値、現在の土壌状態、電圧値、ポンプ状態が継続的に表示されます。START をタッチするとポンプが起動し、ボタンが純赤色の STOP に変わります。もう一度タッチするとポンプが停止します。
Page Tools
PDF
On This Page