20
19

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 5 years have passed since last update.

Raspberry Pi3とArduino Unoのシリアル通信

Posted at

概要

ラズパイとArduinoをシリアル通信してみたメモ

ラズパイの設定

  • /boot/config.txtを編集する
/boot/config.txt
$ sudo vi /boot/config.txt
# 末尾にこの行を追記
dtoverlay=pi3-miniuart-bt
  • /boot/cmdline.txtを編集する
/boot/cmdline.txt
$ sudo vi /boot/cmdline.txt
前) dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
後) dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
  • 再起動
$ sudo reboot

Arduinoと接続

  • 下記のようにUSB接続する
    IMG_8987.JPG

  • コマンドを実行し接続されていることを確認

$ dmesg | grep ttyACM0
[ 2991.602972] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device
  • Arduinoは下記を書き込む
void setup(){
  // シリアルポートを9600 bps[ビット/秒]で初期化
  Serial.begin(9600);
}
 
void loop(){
  Serial.write("hello world¥n");
  delay(3000);
}
  • ラズパイ側のpython
import serial
import time

def main():
    con = serial.Serial('/dev/ttyACM0', 9600)
    time.sleep(2)
    print con.portstr
    while 1:
        # シリアル通信からデータを受け取って、MQTTでpublishするとかは下記でできそう
        str=con.read(30)
        #print str

if __name__ == '__main__':
    main()

実行結果

$ python serial-arduino.py
/dev/ttyACM0
helhello w
orld¥nhel
lo world¥
nhello wor
・・・・・

参考

20
19
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
20
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?