GPIOのピン配置と作業治具
ピン配置
$ pinout
Raspberry Pi Model B Rev 2 はGPIOポートが2つに分かれている。
pinout の出力ではP5は裏から見た配置になっている(なんで?)よう
木の板にラズパイとブレッドボードを固定してピン配置図を貼っておく
Raspberry Pi 初期型?をセットアップ
ずっと放置していた Raspberry Pi 初期型?をセットアップ
カードスロットはマイクロSDに交換済み
無線lanのドングルも古い ELECOM の WDC-150SU2M
Raspberry Pi Imager でSDカードを作る
Raspberry Pi Imager v1.7.4
Raspberry Pi OS Lite(32-bit)
Tera Term でホスト名(raspberrypi.local)を使って接続
インストール直後の状態
$ cat /etc/os-release PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)" NAME="Raspbian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
$ cat /etc/debian_version 11.6
$ cat /etc/issue Raspbian GNU/Linux 11 \n \l
$ uname -a Linux raspberrypi 5.15.84+ #1613 Thu Jan 5 11:58:09 GMT 2023 armv6l GNU/Linux
$ cat /sys/firmware/devicetree/base/model Raspberry Pi Model B Rev 2
更新
$ sudo apt update
$ sudo apt upgrade
vimインストール
$ sudo apt install vim
無線lan
状態の確認
USBデバイス情報
$ lsusb Bus 001 Device 004: ID 056e:4008 Elecom Co., Ltd WDC-150SU2M Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ネットワークインターフェースの確認
$ ip add show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether b8:27:eb:ad:c7:c0 brd ff:ff:ff:ff:ff:ff inet 192.168.3.35/24 brd 192.168.3.255 scope global dynamic noprefixroute eth0 valid_lft 86010sec preferred_lft 75210sec inet6 fe80::abb1:f3a1:109f:6319/64 scope link valid_lft forever preferred_lft forever 3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000 link/ether bc:5c:4c:26:d7:75 brd ff:ff:ff:ff:ff:ff
アクセスポイントを検索してみる
$ sudo iwlist wlan0 scan | grep ESSID
WDC-150SU2M の頭が点滅して近所のSSIDが表示される。 無線lan 生きているのか?
定義ファイルを確認してみる
$ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1
Raspberry Pi Imager で設定した内容が反映されていないよう
ssid とかを設定
$ sudo wpa_passphrase 接続するSSID パスワード |sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf network={ ssid="接続するSSID" #psk="パスワード" psk=暗号化されたパスワード }
wpa_supplicant.conf を修正
$ sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
修正したwpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=JP network={ ssid="接続するSSID" #psk="パスワード" psk=暗号化されたパスワード scan_ssid=1 proto=WPA pairwise=CCMP key_mgmt=WPA-PSK group=CCMP auth_alg=OPEN }
wlan0 インターフェース用ファイルを作成
$ sudo vi /etc/network/interfaces.d/wlan0
ファイルの中身
allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
再起動したら無線lan繋がりました
アナログ入力
LM61CIZを繋いで温度測定
#LM61CIZ import machine import utime adc2in = machine.ADC(2) voltage_conv = 3.3 / 65535 while True: readvol = adc2in.read_u16() * voltage_conv temp_conv = (readvol * 1000 - 600) / 10 print('電圧:' + str(readvol) + 'V ' + '温度:' + str(temp_conv) + '℃') utime.sleep(2)
HC-SR04で距離測定
HC-SR04は5Vなので、HT7750Aで3.3V→5Vに昇圧。Echo出力は分圧抵抗で3.3V
#HC-SR04 # https://hellobreak.net/raspberry-pi-pico-ultrasonic-distance/ from machine import Pin import utime trigger = Pin(2, Pin.OUT) echo = Pin(3, Pin.IN) def read_distance(): trigger.low() utime.sleep_us(2) trigger.high() utime.sleep(0.00001) trigger.low() while echo.value() == 0: signaloff = utime.ticks_us() while echo.value() == 1: signalon = utime.ticks_us() timepassed = signalon - signaloff distance = (timepassed * 0.0343) / 2 print('距離 : ',distance,'cm') while True: read_distance() utime.sleep(1)
GPSを繋いでみる
GPS(秋月のAE-GYSFDMAXB)を繋いでみる。 GROVE端子に繋ぐので、AE-GPSに乗っているレギュレータを外してジャンパ
# GPS from machine import Pin, UART import time # NMEA(経度、緯度)変換 def changeNMEA(val): getval = float(val) d =getval / 100 m = ((d - int(d) ) * 100) / 60 return int(d) + m # UART設定 # GROVE1, uart = UART(0, 9600, tx=Pin(0), rx=Pin(1)) while True: time.sleep(5) #GPSデータ読み込み res=uart.read() #utf-8デコード data8 = res.decode('utf-8') #改行コード取り除き data = data8.strip() #分割 data_list = data.split(',') #時刻 time_str = (data_list[1])[0:6] hours = (int(time_str[0:2]) + 9) % 24 minutes = int(time_str[2:4]) seconds = float(time_str[4:]) times = str(hours) + ':' + str(minutes) + ':' + str(seconds) #日付 date_str = (data_list[9])[0:6] day = int(date_str[0:2]) month = int(date_str[2:4]) year = int(date_str[4:6]) dates = '20' + str(year) + '/' + str(month) + '/' + str(day) #緯度 latitudes = changeNMEA(val = data_list[3]) #経度 longitudes = changeNMEA(val = data_list[5]) print(dates + ' ' + times + ' 経度:' + str(longitudes) + ' 緯度:' + str(latitudes))
基板だけで遊ぶ
押したら点く
GP20に繋がっているSWを押すとGP0のLEDが点く
from machine import Pin led = Pin(0, Pin.OUT) sw = Pin(20, Pin.IN) while True: if sw.value() == 1: led.value(0) else: led.value(1)
トグル動作
スイッチを押すとLEDが点灯。もう一度押すと消灯
from machine import Pin import time led = Pin(0, Pin.OUT) sw = Pin(20, Pin.IN) swinp = 1 old_swinp = 1 led.value(0) while True: swinp = sw.value() if swinp == 0 and old_swinp == 1: led.toggle() time.sleep(0.03) old_swinp = swinp
流れるLED
LEDが順番に点灯
PINの定義を配列で
from machine import Pin import time LED_PINS = [0,1,2,3,4,5,6,7,16,17,26,27,28] LEDS = [] i = 0 for pin in LED_PINS: digout = Pin(pin, Pin.OUT) LEDS.append(digout) while True: for i in range(len(LED_PINS)): LEDS[i].value(1) time.sleep(0.1) LEDS[i].value(0)
LEDを順番に点ける
GP20を押すとLEDが右に
GP21を押すとLEDが左に
from machine import Pin import time sw20 = Pin(20, Pin.IN) sw21 = Pin(21, Pin.IN) LED_PINS = [0,1,2,3,4,5,6,7,16,17,26,27,28] LEDS = [] i = 0 sw20inp = 1 old_sw20inp = 1 sw21inp = 1 old_sw21inp = 1 swcount = 0 for pin in LED_PINS: digout = Pin(pin, Pin.OUT) LEDS.append(digout) for i in range(len(LED_PINS)): LEDS[i].value(0) while True: sw20inp = sw20.value() if sw20inp == 0 and old_sw20inp == 1: swcount += 1 time.sleep(0.03) print(swcount) old_sw20inp = sw20inp sw21inp = sw21.value() if sw21inp == 0 and old_sw21inp == 1: swcount -= 1 time.sleep(0.03) print(swcount) old_sw21inp = sw21inp if swcount >= len(LED_PINS): swcount = len(LED_PINS)-1 if swcount <= 0: swcount = 0 for i in range(len(LED_PINS)+1): if i == swcount: LEDS[swcount].value(1) time.sleep(0.1) else: LEDS[swcount].value(0)