Arduinoのデジタル入出力関数について解説します。
pinMode
pinModeは、ピンをINPUT(入力)かOUTPUT(出力)に設定します。 Arduino 1.0.1以降は、INPUT_PULLUP(内部プルアップ抵抗を有効化した入力)もできる様になりました。ただのINPUTの場合、内部プルアップは無効です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Arduino Sketch Example: pinMode //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax pinMode(pin, mode) //pin: the number of the pin whose mode you wish to set //mode: INPUT, OUTPUT, or INPUT_PULLUP. //Example int ledPin = 13; // LED connected to digital pin 13 void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() { digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:pinMode()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、13番ピンを出力に設定し、HIGHとLOWの切り替えを1秒間隔で行っています。このピンにLEDをつないでおけば、点滅させる事ができます。
digitalWire
digitalWireは、指定pin(ピン)にHIGHまたはLOWを出力しますが、pinModeの設定により動作が異なります。
指定ピンがpinMode()関数でOUTPUTに設定されている場合
HIGH: 5V出力 (ボードの電源電圧。製品によっては、3.3Vの場合もある)
LOW: 0V(GND)出力
指定ピンがpinMode()関数でINPUTに設定されている場合
HIGH: 20kΩの内部プルアップ抵抗が有効
LOW: 20kΩの内部プルアップ抵抗は無効
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Arduino Sketch Example: digitalWrite //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax digitalWrite(pin, value) //pin: the pin number //value: HIGH or LOW //Example int ledPin = 13; // LED connected to digital pin 13 void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() { digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:digitalWrite()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例は、先ほどのpinModeの例と内容は同じです。13番ピンの状態をdigitalWriteを使用してHIGHとLOWを交互に切り替えています。
digitalRead
digitalReadは、指定したピンの状態をを読み取ります。戻り値は、HIGHまたはLOWになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//Arduino Sketch Example: digitalRead //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax digitalRead(pin) //pin: the number of the digital pin you want to read (int) //Returns: HIGH or LOW //Example //Sets pin 13 to the same value as pin 7, declared as an input. int ledPin = 13; // LED connected to digital pin 13 int inPin = 7; // pushbutton connected to digital pin 7 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output pinMode(inPin, INPUT); // sets the digital pin 7 as input } void loop() { val = digitalRead(inPin); // read the input pin digitalWrite(ledPin, val); // sets the LED to the button's value } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:digitalRead()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、7番ピンの入力状態を読み取って、変数valに格納しています。
Sponsored Link