Arduino の勉強会
平成 23 年 1 月 5 日
於:鳥取大学
1
内容
1 Arduino とは ... 2 2 インストールと動作チェック ... 2 3 LED の点滅回路 ... 4 4 液晶(LCD)の表示 ... 6 5 電圧計測 ... 7 6 アナログ出力 ... 8 7 サーボの動作 ... 9 8 例題:ボリュームに応じて LED の明るさを変化させましょう。 ... 10 9 例題:モータの回転数を変化させましょう。 ... 10 この実習で必要な物品 今回使用した型式 備考Arduino 1 個 Arduino Duemilanove 今購入するのなら Arduino uno
USB ケーブル 1 本
ブレッドボード 1 個
2
1 Arduino とは
Arduino はイタリアで開発されたシステムで、Arduio 基板(以後:Arduino)と Arduino 開発環 境(以後 IDE)から、構成されたシステムである。このシステムはオープンハードウエア、オ ープンソフトウエアのであるので、多くの互換機が存在する。ただし Arduino を名乗れるの はプロジェクトで承諾されてものだけである(プロジェクト開発されたものに限定されてい ます)。購入は日本国内では、スイッチサイエンスや千石電商など、様々なところで売られ ており、簡単にできる。また、互換機は学研の「大人の科学」などにもある。Arduino の心 臓 部 は 、 Atamel 社 の 組 込 み 用 コ ン ピ ュ ー タ AVR(ATmega8, ATmega168, ATmega328P, ATMega644P, ATmega1280)であり、ブレッドボード上に互換機を作成することも安易であ る。IDE は Processing の開発環境で、言語は Writing(C 言語[avr-gcc]を基に関数を増やした 感じのもの)である。したがって、C 言語が判れば簡単に開発することができる。他にも様々 な開発環境が存在します。 豊富なサンプルプログラムが存在するので、勉強するには最適です。 Arduino はプログラム(コード)の事をスケッチと呼びます。 また、拡張ユニットの事をシールドと呼びます。シールドには Ethernet や TV に出力するも の、モータのドライバなど様々な種類が存在します。
2 インストールと動作チェック
Arduino のホームページ http://www.arduino.cc/ から Arduino Software をダウンロードして、 解凍を行います。 解凍できると右のような内容のフォルダが 作成されます。 Arduino と PC を接続します。 Duemilanove の場合は自動的に Driver が入り ます。 Uno の場合は Drivers 内のファイルでインス トールする必要があります。3 フォルダ内の arduino をダブルクリックして IDE を立ち上げます。
メニューバー内の Tools の Board と Serial Port を合わせます。 Serial Port が判らない場合は、デバイスマネ ージャで確認してください。 サンプルプログラムを読みます。 メニューバーの FileExamplesBasicBlink を読み込みま す。 コンパイルして Arduino に転送し、実行しま す。 を押します。これで一連の動作を行っ てくれます。 アップロード後は自動的にプログラムが動 作します。 基板上の L のランプが点滅します。
4
3 LED の点滅回路
Example の Blink は、LED を点滅するプログラムでした。プログラムを下に示します。
C 言語と同じく /* */ や // は注釈行を示します。
setup 関数と loop 関数が定義されていることがわかります。このように Arduino では、setup 関数で初期化し、loop 関数を実行します。つまりこの 2 つの関数を定義することで Arduino は動作します。
この blink は、Setup 関数で
pinMode(13, OUTPUT); と宣言し Disital の 13 番を出力モードにし
loop 関数で Digital の 13 番の信号を 1 秒間隔で On-off し LED を点滅させていることがわか ります。
13 番は 1kΩの抵抗が直列に入っているので、下記のようなに、直接 LED を刺す事が出来ま すが、余所では抵抗が入っていないのでしてはいけません。
/* Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain. */
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second }
5
Digital の4に変更してみましょう。配線図は次のようになります。抵抗は 1kΩです。プロ グラムは各自考えてみましょう。
電子回路に使用する LED は通常 20mA 程度の電流まで流せます。また、マイコン(Arduino や PIC など)も1ポートあたり 20-25mA 程度まで可能です。それ以上の電流が必要な場合 は別途回路が必要となります。
6
4 液晶(LCD)の表示
サンプルを例に説明します。 メニューバーの File --> Example --> LiquidCrystal Display を呼び出します。LCD に合わせてピンの配置を変更するためスケッチ中の LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)を変更します。 接続は下記のようになります Arduino : LCD 5V : VD Gnd : GND Digital2 : 4 Digital3 : 5 Digital4 : 6 Digital5 : 7 Digital6 : E Digital7 : RS Digital8 : RW 2 行目に表示をしたい時には setCursor()関数を使用します。
メモ
#include <LiquidCrystal.h>// initialize the library with the numbers of the interface pins LiquidCrystal lcd(7,8,6,2,3,4,5);
void setup() {
// set up the LCD's number of columns and rows: lcd.begin(16, 2);
// Print a message to the LCD. lcd.print("hello, world!"); }
void loop() {
// Turn off the display: lcd.noDisplay(); delay(500);
// Turn on the display: lcd.display(); delay(500); } リファレンスの 見方を覚えまし ょう。
7
5 電圧計測
4.の液晶(LCD)の表示のプログラムを変更して作成します。 analogRead で 0-1023 までのデータを読むことがで きます。 最後の Delay はあまり早すぎて LCD の表示が見え なくなるのを防ぐためです。 Arduino からは、5V の出力が 1 箇所なのでブレッ ドボードを使用して液晶とボリュームに分岐させ ます。 Arduino のサンプルには USB を使用した仮想シリアルポートへの出力もあります。 FileExsamplesAnalogAnalogInOutSerial でスケッチを読み込み、書き込みを行います。 仮想シリアルのデータは を押すことでモニタすることが可能です。メモ
#include <LiquidCrystal.h>// initialize the library with the numbers of the interface pins LiquidCrystal lcd(7,8,6,2,3,4,5);
void setup() {
// set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.print("Tottori Univ!"); } void loop() { int analogData; float Volt; analogData=analogRead(0); Volt= 5.0*(float)analogData/1023; lcd.setCursor(0,1); lcd.print("Volt="); lcd.print(Volt); lcd.print("V"); delay(10); }
8
6 アナログ出力
Arduino にはアナログ出力がありませんが、PWM を使用して信号の強弱をつけることがで きます。ここでは、サンプルを利用して説明します。FileexamplesBasicFade でスケッ チを読み込みます。1.LED の時を例に Digital9 に LED を接続します。
analogWrite 関数で出力しています。このように簡単に LED の明るさを変えたりすることが 可能です。使える出力は PWM と書かれているところだけです。
メモ
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by void setup() {
// declare pin 9 to be an output: pinMode(9, OUTPUT);
}
void loop() {
// set the brightness of pin 9: analogWrite(9, brightness);
// change the brightness for next time through the loop: brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ; }
// wait for 30 milliseconds to see the dimming effect delay(30);
9
7 サーボの動作
サーボはラジコンとかで使用されるもので、角度を調整できます。タイプには JR 型や F タ イプなどあります。今回使用するのは JR タイプです。正確に使用するためには信号発生を 接続し、パルス幅と角度の関係を見ておく必要がありますが、今回は動くことだけを確認 します。 正確に動作させるには setup()関数内の attach メソッドを変更します。詳しくはリファレン スマニュアルを参照してください。 接続はブレッドボードを介して制御線(橙色)を Digital9 に、電源(赤色)を 5V に、Gnd(茶色) を Gnd に接続します。メモ
#include <Servo.h>Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object }
void loop() {
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position }
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees {
myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position }
10