6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoとESP32のWire.begin()の差異

Posted at

ArduinoとESP32のWire.begin()の差異

ソフトウェアI2Cを調べていて、両者の差異に気がついたので記録しておく。

ソフトウェアI2C

何らかの理由で、マイコン搭載のI2C(ハードウェア)ではなく、GPIOを用いてI2Cを利用したいこともある。今回の参考記事は、これこれなど。

ArduinoのWire.begin()

本家の情報

Wire.begin()
Wire.begin(address)

これだけ。好みのGPIOを用いるには、ソフトウェアI2Cライブラリ(後述)を使う必要あり。

ESP32のWire.begin()

いきなりgitのソースコードとなるが、こちら

bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus

つまり、Arduinoとは異なり、

Wire.begin(26,27);

のような使い方ができる。これだけ。シンプル。

ソフトウェアI2Cライブラリ利用方法(Arduino)

Arduinoサイト経由でたどり着く、ここが本家本元の一つ(他にも見つかる)。下記のように使用したいGPIOを指定して使う。

# define SDA_PORT PORTD
# define SDA_PIN 2
# define SCL_PORT PORTD
# define SCL_PIN 3
# include <SoftI2CMaster.h>
# include <SoftWire.h>

SoftWire Wire = SoftWire();

このあとは、

Wire.begin();

と指定するのみ。

終わりに

個人的には、ESP32のように直接GPIOを指定できる方がうれしい。好みの問題だが。

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?