〔ESP-WROOM-02〕
〔マイコンのトップに戻る〕
[準備編]
[module編]
[GPIO編]
[通信編]
[I2S編]
[WiFi編]
[BLE編1]
[BLE編2]
[BLE編3]

--------------------------------------------------------------------------------
import time
import machine
from machine import TouchPad, Pin
import esp32
t = TouchPad(Pin(14)) # 14番ピンにタッチパッドを接続
t.config(200) # ピンが接触したと見なす敷居値を設定
esp32.wake_on_touch(True) # タッチ入力で起こすを有効にする
x = 0
while True:
print(x)
if x == 30:
# 30カウントしたらスリープ
print('I sleep a little.')
time.sleep_ms(100) # 上の文字が表示されず寝てしまうので少し待たせる
machine.lightsleep()
print('I woke up now')
x = x + 1
time.sleep_ms(1000)
--------------------------------------------------------------------------------
【外部ピン入力でスリープから起こす】
--------------------------------------------------------------------------------
import time
import esp32
import machine
sw1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) # ピン14番(GPIO14)をスイッチ入力で割り付ける
# スイッチのLOWでスリープから起こす
esp32.wake_on_ext0(sw1, esp32.WAKEUP_ALL_LOW)
x = 0
while True:
print(x)
if x == 30:
# 30カウントしたらスリープ
print('I sleep a little.')
time.sleep_ms(100)
machine.lightsleep()
print('I woke up now')
x = x + 1
time.sleep_ms(1000)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
import time
import machine
led = machine.Pin(4, machine.Pin.OUT) # ピン4番(GPIO4)をLED出力で割り付ける
led.on()
# ディープスリープから起こされたかをチェック
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
# 30秒してからスリープに入る
x = 0
while True:
print(x)
if x >= 30: break
x = x + 1
time.sleep(1)
# 10秒間のディープスリープに入る
print('I sleep deeply')
machine.deepsleep(10000)
print('pass')
--------------------------------------------------------------------------------
RTCモジュールを除くチップ全体の電源が遮断されます。
どうやら2000年1月1日 00:00:00 UTC のエポックを

--------------------------------------------------------------------------------
import time
from machine import RTC
import network
import ntptime
time.sleep(30) # 30秒後に開始するこの間にREPLを起動しましょう。
ESSID = "BBUser"
Password = ""
# Wi-Fi 接続を設定する為にクライアントオブジェクトのインターフェースを作成する
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
sta_if.connect(ESSID, Password) # ステーションインターフェイスを有効にする
while not sta_if.isconnected(): # WiFiネットワークに接続する
pass
print('\r\n[network config]\r\n', sta_if.ifconfig())
rtc = RTC() # RTCのオブジェクト作成
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
ntptime.NTP_DELTA = 3155641200 # 更にタイムゾーンを引いた値
# "pool.ntp.org"サーバーに接続している
# 他のサーバーに接続したいなら(例:host = "ntp.nict.jp")とする
ntptime.settime() # サーバからの日時を rtc に設定
print('Finished setting to RTC')
sta_if.active(False) # Wi-Fi 接続を切る
while True:
print(rtc.datetime()) # 日時を UTC で取得
time.sleep((1))
--------------------------------------------------------------------------------
動作は30秒後に起動します、この間にREPLを動かしましょう。
ESP32-WROOM-32Eは、クロック周波数が160Mhzと例) from esp32 import RMT pls = RMT(0, pin=Pin(18), clock_div=8, carrier_freq=38000)・RMT.write_pulses(pulses, start)
--------------------------------------------------------------------------------
import time
import esp32
from machine import Pin
# 基本クロックは80MHz 分周器は8ビット(0-255)
pulse = esp32.RMT(0, pin=Pin(25), clock_div=8)
# パターンをループさせる
pulse.loop(True)
# 100KHzのパルスで出力
# 100KHz=10us(1周期) の半分 5us は(80MHz/8div)*5us = 50(49は調整しています)
# パルスはLOWの0から開始で、パターンは"01"だから分解能は"50,50"で出力
pulse.write_pulses((49, 50), start=0)
while True:
time.sleep(1)
--------------------------------------------------------------------------------
分周は[clock_div=8]

ワンダフォな波形ですがぁ"(50,50)"ではチョットぉあれだった物でぇ"(49,50)"にした図です。
1周期が(50,50)でデュティ比50%なら、(25,75)とすれば25%って事ですね。
分周は[clock_div=1]

(80MHz/1div)*5us = 400
(399,400)にした図です、んん、まぁいっかぁ。

最高で1MHz辺りまでは行けそうですね。
【きむ茶工房ガレージハウス】
Copyright (C) 2006-2021 Shigehiro Kimura All Rights Reserved.