0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GPIO入力を使用したESP32のDeepsleepの外部ウェイクアップ

Last updated at Posted at 2025-07-11

はじめに

ESP32のGPIOのGPIO入力はLOWとHIGHの2つの状態を切り替えることができます。これを使用して、Deepsleepの外部ウェイクアップが可能です。今回は2台のESP32を使用して、Deepsleep状態のスリープ用ESP32を、ウェイクアップ用ESP32からのGPIO入力により外部ウェイクアップする方法を書きます。
ESP32のファームウェアバージョンは ESP32_GENERIC-20241129-v1.24.1 を使用しました。

配線について

ウェイクアップ用 ESP32 スリープ用 ESP32
GPIO4 GPIO4

image.png

サンプルコード

boot.py(スリープ用ESP32)

今回はGPIO4がHighになったらDeepsleepからウェイクアップするようにします。
esp32.wake_on_ext0(pin, level)でGPIOの状態をもとにDeepsleepからウェイクアップさせる方法を設定します。 pin にはウェイクアップに使用するGPIOを引数として初期化したPinオブジェクトを指定します。level には指定したGPIOがHighかLowのどちらのときにウェイクアップするかを設定するための引数を指定します。 level には esp32.WAKEUP_ALL_LOW もしくは esp32.WAKEUP_ANY_HIGH のどちらかを指定する必要があります。

参考
https://micropython-docs-ja.readthedocs.io/ja/latest/library/esp32.html

boot.py
from machine import Pin, deepsleep, RTC, wake_reason, DEEPSLEEP_WAKEUP_EXT0
import esp32
import time

rtc = RTC()

# 保存されたスリープ回数を読み込む
from machine import Pin, deepsleep, RTC
import esp32
import time

rtc = RTC()

# 保存されたスリープ回数を読み込む
counter_data = rtc.memory()
try:
    counter = int(counter_data.decode())
except:
    counter = 0

counter += 1
print("Wakeup count:", counter)

# 保存する
rtc.memory(str(counter).encode())

# ウェイクアップピン設定
wake_pin = Pin(4, Pin.IN, pull=Pin.PULL_DOWN)
esp32.wake_on_ext0(pin=wake_pin, level=esp32.WAKEUP_ANY_HIGH)

print("Going to deep sleep again...")
time.sleep(1)
deepsleep()

boot.py(ウェイクアップ用ESP32)

スリープ用ESP32のGPIO4とジャンプワイヤーでつながっているGPIO4をHighにします。

boot.py
from machine import Pin
import time

# GPIO4 を出力モードで初期化
p4 = Pin(4, Pin.OUT)

# High に設定
p4.value(1)

print("GPIO4 is now HIGH")

スリープ用ESP32の実行結果

スリープ用ESP32の,スリープをする前と復帰したあとの出力です。「ets Jun 8 2016 00:22:57」の前で復帰の前後が区切られています。出力を見ると、復帰前である上部のWakeup countが1であるのに対し、下部の復帰後の出力では2に増えていることがわかります。

スクリーンショット 2025-07-15 111743.png

「ets Jun 8 2016 00:22:57」はROMのサインオンメッセージで起動時の一番最初に表示されるメッセージらしいです。

参考
https://esp32.com/viewtopic.php?t=6302

ウェイクアップ用ESP32の実行結果

ウェイクアップ用ESP32の起動時の出力です。「GPIO4 is now High」のメッセージまで表示されているため、エラー無く最後まで実行されていることがわかります。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?