LoginSignup
49

More than 5 years have passed since last update.

Raspberry PiとArduinoでシリアル通信

Posted at

Raspberry PiとArduinoでシリアル通信をしてみた。

Arduinoには、シリアル通信をエコーするプログラムを書き込んでおく。

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
  }
}

void loop()
{
  if (Serial.available()) Serial.write(Serial.read());
}

Raspberry PiをWiFi接続できるようにしてあるのでsshでログイン。
cuコマンドを入れる。

$ sudo apt-get install cu

ArduinoとはUSBケーブル一本つなぐだけでOK。

IMG_3465.JPG

dmesgを確認すると、ttyACM0がそれっぽい。

$ dmesg
...
...
[89367.152516] cdc_acm 1-1.3:1.0: ttyACM0: USB ACM device

ACMとかCDCってなんだ?と思ったら、こちらのページによるとUSB上でRS-232Cを使うためのクラスみたいです。

cuコマンドでArduinoと通信する。

$ cu -l /dev/ttyACM0 -s 9600
Connected.
hogefuga~.
Connection to 10.0.1.26 closed.

入力した文字がそのままターミナルに出力される。
終了するときは~.(チルダドット)。
sshも終了されてしまうが。

とりあえず、これでRaspberry PiとArduinoでシリアル通信の確認ができる。
あとはもう少しArduino側のプログラムを書いてやれば、Raspberry PiからArduinoにつながったセンサーのデータを自由に読み出したりアクチュエーターを動かしたりすることができるようになると思う。

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
49