ラズベリーパイのつまみ食い
by K.I
date 2014/11/16〜
Index
- ラズベリーパイが出たばかりの頃は、予約も一杯で手に入りづらかったので、そのままになっていた。
- 改良版の Model B+ が出たこともあり、遅ればせながら購入してみた
- SDROMにOSを書き込んで、マイクロUSBで5V電源を供給、
- HDMIでディスプレイ、USBでキーボードとマウスを接続すれば、立派にPCとして動作する
[top]
- SDROMへの書き込みには、 Win32DiskImager を使う
- Win32DiskImagerを起動して、ダウンロードしたzipファイルを解凍したImageFileをSDROMに書き込む
- RaspberryPiにSDROMを差して、Ethernetケーブルを接続、電源を供給する
- USBキーボードとか、マウスとか、ディスプレイを接続するのは面倒なので、
- とりあえず、TeraTerm等の SSH端末で接続してみる。
- IPアドレスがわからないので、Windowsの場合はコマンドプロンプトからarpコマンドで確認
> arp -a
- RaspberryPiのベンダIDは、b8-27-eb-**-**-** らしいので、それで始まるMACアドレスのIPを調べる
- でも、arpで見えない場合は、DHCPの範囲に全てpingしてから、改めてarpすれば良い
- IPアドレスが分かったら、TeraTermとかのSSH接続可能な端末で接続する
- Username: pi
- Password: raspberry
$ sudo raspi-config
$ sudo apt-get install libpng12-dev
$ wget https://github.com/AndrewFromMelbourne/raspi2png/archive/master.zip
$ unzip master.zip
$ cd raspi2png-master/
$ make
$ sudo cp raspi2png /usr/local/bin/
- 試しにHDMI端子にディスプレイを接続してみるが、表示されない。。
- デフォルトでは、常にHDMIが出力にはなっているわけではないらしい
- 起動時に、ディスプレイが接続されていれば、表示されたのかもしれないが
- /boot/config.txtを編集して、以下の2行のコメントアウトを外す
$ sudo vi /boot/config.txt
#hdmi_force_hotplug=1
#hdmi_drive=2
画面サイズについては、以下のコメントアウトを外して、解像度を設定する
#framebuffer_width=1280
#framebuffer_height=720
$ sudo reboot
$ startx
- USBキーボードを持っていないので、Xのデスクトップ上で使える仮想キーボードをインストールしてみる
- メニューから、アクセサリ→Keyboardで起動できる
- 設定→Main Menu Editorで、アクセサリの項目を見るとちゃんとKeyboardがある。
- よく分からないけど、一回チェックを外して、もう一度チェックし直してOKしてみる
- もう一度メニューで、アクセサリを見ると、今度はKeyboardがあった。
- 起動してみると、画面の横幅一杯に大きくキーボードが表示された
- とりあえず、これでUSBマウスだけ繋げば、何とか単体でも操作できるようになった。
1まぁでも自分は、iPhoneやAndroidのアプリのFingを使って調べちゃうことが多いけど。。
[top]
- 別のPCからのファイル転送用に、Sambaをインストールする
$ sudo apt-get install samba
$ sudo vi /etc/samba/smb.conf
$ mkdir /home/pi/share
$ sudo service samba restart
$ sudo update-rc.d samba defaults
$ sudo update-rc.d ssh defaults
$ sudo vim /etc/samba/smb.conf
[share]
path=/home/pi/share
read only=no1
browsable=yes
guest ok = yes
force user = pi
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
$ sudo /etc/network/interfaces
- eth0の設定をdhcpからstaticに変更して、
- しかしWindows7から、\\192.168.1.111でアクセスできるのは当然として、
- \\raspberrypiでアクセスできるのは何故だろう?
- DebianやUbuntuの場合、host名は/etc/hostsと/etc/hostnameで設定されているらしい
- /etc/hostnameファイルは知らなかった。
- でも、やはり他のPCから名前が分かるのは何故?arpじゃMACアドレスしか分からないし
- 通常は、HDMI出力だが、コンポジットビデオ出力も可能
- ケーブル接続は、自分が購入したケーブルは、以下のように黄色と赤色が逆になっていた2 →参考
- つまり、raspberrypiの接続は、追加された端子が映像出力となっているので、
- 3極の場合は、オーディオ出力のみ使うことが出来るように工夫されている
→3極ケーブルの場合、Video出力はショートするが、オーディオ出力は問題なし
- tvserviceコマンドで、現在の設定を確認可能
$ tvservice -s
state 0x40002 [NTSC 4:3], 720x480 @ 60.00Hz, interlaced
2これは、ケーブルによって異なる可能性があるので、要注意!
[top]
- RaspberryPi B+のヘッダピンのピン配置
| PinNo | | PinNo | |
| 1 | 3.3V | 2 | 5V |
| 3 | GPIO2(I2C1_SDA) | 4 | 5V |
| 5 | GPIO3(I2C1_SCL) | 6 | GND |
| 7 | GPIO4(GPCLK0) | 8 | GPIO14(UART_TXD) |
| 9 | GND | 10 | GPIO15(UART_RXD) |
| 11 | GPIO17 | 12 | GPIO18 |
| 13 | GPIO27 | 14 | GND |
| 15 | GPIO22 | 16 | GPIO23 |
| 17 | 3.3V | 18 | GPIO24 |
| 19 | GPIO10(SPI_MOSI) | 20 | GND |
| 21 | GPIO9(SPI_MISO) | 22 | GPIO25 |
| 23 | GPIO11(SPI_SCLK) | 24 | GPIO8(SPI_CE0) |
| 25 | GND | 26 | GPIO7(SPI_CE1) |
| 27 | ID_SD | 28 | ID_SC |
| 29 | GPIO5 | 30 | GND |
| 31 | GPIO6 | 32 | GPIO12 |
| 33 | GPIO13 | 34 | GND |
| 35 | GPIO19 | 36 | GPIO16 |
| 37 | GPIO26 | 38 | GPIO20 |
| 39 | GND | 40 | GPIO21 |
- /sys/class/gpio/ディレクトリにGPIOの仮想ファイルを作成してアクセスする
- exportにGPIOのピン番号を書き込むと、仮想ファイルが出来る
$ git clone git://git.drogon.net/wiringPi
$ cd wiringpi
$ ./build
$ gpio -v
gpio version: 2.26
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Model B+, Revision: 1.2, Memory: 512MB, Maker: Sony
- WiringPiを、C言語で使ってみる
- wiringPi.hをインクルード(blink.c)
#include <stdio.h>
#include <wiringPi.h>
// LED Pin - wiringPi pin 5 is BCM_GPIO 24.
#define LED 5
int main (void)
{
printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ;
pinMode (LED, OUTPUT) ;
for (;;)
{
digitalWrite (LED, HIGH) ; // On
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ;
}
return 0 ;
}
- wiringPiSetupで初期化した場合は、wiringPiのピン番号で指定する
- BCMの番号で指定したい場合は、wiringPiSetupGpioを使って初期化する
- 例えば、WiringPiの5番は、BCMの24ピンにあたる
- コンパイルは、Makefileを見ると以下のようにすれば良いらしい
$ sudo ./blink
$ sudo apt-get install libi2c-dev
i2c-dev
/etc/modprobe.d/raspi-blacklist.conf
#blacklist i2c-bcm2708
リブート
$ sudo reboot
$ dmesg | grep i2c
[ 5.436821] bcm2708_i2c_init_pinmode(1,2)
[ 5.550985] bcm2708_i2c_init_pinmode(1,3)
[ 5.557629] bcm2708_i2c bcm2708_i2c.1: BSC1 Controller at 0x20804000 (irq 79) (baudrate 100000)
[ 12.069793] i2c /dev entries driver
- 手持ちのI2Cデバイスを探してみると、
- Strawberry-Linuxの 音声合成モジュールが、I2Cでコントロール可能なので、繋いでみる
$ sudo i2cdetect -l
i2c-1 i2c bcm2708_i2c.1 I2C adapter
$ sudo i2cdetect 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3f
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
- I2Cバス1で、スレーブアドレスが3Fということか。
$ sudo i2cset -y 1 0x3f 0x4d 0x53 0x47 0x61 0x69 0x75 0x65 0x6f i
- ちゃんと、Start/Stopコンディションも出力されている
- "おんせいごうせいのてすとちゅうです"と出力(MSGonnseigo'useino/tesutochu-de'su.)
- と思ったけど、Usageが出てコマンドが受け付けられないので、アクセントのsingle-quote(0x27)を抜いて出力
$ sudo i2cset -y 1 0x3f 0x4d 0x53 0x47 0x6f 0x6e 0x6e 0x73 0x65 0x69 0x67 0x6f 0x75 0x73 0x65 0x69 0x6e 0x6f 0x2f 0x74 0x65 0x73 0x75 0x74 0x6f 0x63 0x68 0x75 0x2d 0x64 0x65 0x73 0x75 0x2e i
- 2回に分けて出力すれば、0x27を入れてもOKだった(なんでだろ?)
$ sudo i2cset -y 1 0x3f 0x4d 0x53 0x47 0x6f 0x6e 0x6e 0x73 0x65 0x69 0x67 0x6f 0x27 0x75 0x73 0x65 0x69 0x6e 0x6f 0x2f i
$ sudo i2cset -y 1 0x3f 0x4d 0x53 0x47 0x74 0x65 0x73 0x75 0x74 0x6f 0x63 0x68 0x75 0x2d 0x64 0x65 0x27 0x73 0x75 0x2e i
3何故独自の番号を使ってるんだろう。ややこしい。。
4BCMって何かな。Broadcomのこと?
[top]
pi@raspberrypi ~ $ sudo apt-get install samba
W: Not using locking for read only lock file /var/lib/dpkg/lock
E: Unable to write to /var/cache/apt/
E: The package lists or status file could not be parsed or opened.
pi@raspberrypi ~ $ sudo reboot
sudo: unable to execute /sbin/reboot: Input/output error
- ツール・ラボさんの、 GPIO制御の記事
- Raspberry PiのGPIO制御について、非常に分かりやすく解説されている
[top]
[電子工作関連に戻る]
⇒ Disqusの広告がうるさすぎるので基本は非表示にしました