Arduinoのアナログ入出力関数について解説します。
analogRead
analogReadは、指定したpin(アナログピン)から値を読み取ります。
analogReadのスペック
チャンネル数:通常=6、mini=8、Mega=16
ADコンバータのビット数: 10ビット(0から1023の数値)
基準電圧: 5V(ボードの電源電圧。種類によっては、3.3Vの場合もある)
分解能: 1単位あたり約4.9mV(基準電圧が5Vの場合)
処理時間: 約100μ秒(0.0001秒)
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: analogRead //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax analogRead(pin) //pin: the number of the analog input pin to read from //(0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega) //Returns: int (0 to 1023) //Example int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3 // outside leads to ground and +5V int val = 0; // variable to store the value read void setup() { Serial.begin(9600); // setup serial } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:analogRead()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、3ピンの電圧の値をanalogReadで読んで、valに格納しています。analogReadが使用できるピン番号は次の通りです。
analogReadが使用できるピン
通常のボード: 0から5
Arduino mini等QFPパッケージのATmega168搭載ボード: 0から7
なお、ピンに入力がなく浮いていると、analogReadで読み取る値は不安定になります。
analogWrite
analogWriteは、ほとんどのボードで指定ピンからPWM波形を出力します。使用例としては、LEDの調光、モータの回転制御などが挙げられます。analogWrite関数が実行されると、次にanalogWriteやdigitalRead、digitalWriteがそのピンに対して使用されるまで、PWMの矩形波が定常出力されます。PWM波形の周波数は約490Hzです。 なお、analogWriteの前にpinMode関数を呼び出して出力に設定する必要はありません。
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: analogWrite //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax analogWrite(pin, value) //pin: the pin to write to. //value: the duty cycle: between 0 (always off) and 255 (always on). //Example int ledPin = 9; // LED connected to digital pin 9 int analogPin = 3; // potentiometer connected to analog pin 3 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the pin as output } void loop() { val = analogRead(analogPin); // read the input pin analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255 } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:analogWrite()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、3番ピンの入力状態をanalogReadでvalに格納し、val/4の値を用いて、analogWriteで、9番ピンからPWM波形を出力しています。
下記にanalorWriteが使用可能なピン番号をまとめておきます。
analogWriteが使用できるピン
ATmega168/328を搭載ボード: 3,5, 6, 9, 10,11番ピン
Arduino Megaボード: 2~13番ピン
Arduino Dueボード: 2~13番ピン、DAC0,DAC1ピン
ATmega8搭載ボード: 9, 10, 11番ピン
Arduino Dueの場合は、上記のPWM出力が可能な2~13番ピンに加えて、DAC0とDAC1というピンが使用できます。これらは、PWMの様な発信波形ではなく、DA(Digital to Analog)コンバータと呼ばれる機能により定常的な一定電圧を出力します。出力電圧は、
(出力電圧) = (電源電圧)×(Value)/255
となります。つまり電源電圧が5Vの場合は、valueに0を指定すると、0Vの電圧が出力され、255を指定すると5Vが出力されます。
なお、ピン5と6のPWM出力はデューティ比が高めになりますので、特にディーティ比を低くして使う場合は注意が必要です。仮にパラメータを0に設定しても、ピン5と6の出力は完全にはオフにならない可能性があります。
analogReference
analogReferenceは、analogRead(アナログ入力)の基準電圧を設定します。analogRead関数は入力が基準電圧と同じとき1023を返します。
選択できる基準電圧
DEFAULT: ボードの電源電圧(通常5V)で、これがデフォルトです。
INTERNAL: 内蔵基準電圧を用います。ATmega168と328Pでは1.1Vです。
INTERNAL1V1: 内部基準電圧1.1V (Arduino Megaボードの場合)
INTERNAL2V56: 内部基準電圧2.56V (Arduino Megaボードの場合)
EXTERNAL: AREFピンに供給される電圧(0V~5V)を基準電圧とします。
(注)Arduino Megaボードの場合、内蔵基準が2種類あるので、どちらかを選択します。
EXTERNAL(外部基準電圧)を選択した場合、次の事に注意して下さい。
・0V未満あるいは5V(電源電圧)より高い電圧に設定してはいけません。
・analogRead()を実行する前に、必ず、analogReference(EXTERNAL)を実行します。
これらを守らないと、Arduinoボードが損傷する可能性があります。
1 2 3 4 5 6 7 8 9 |
//Arduino Sketch Example: analogReference //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax analogReference(type) //type: which type of reference to use (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, or EXTERNAL). |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:analogReference()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
なお、analogRead等の説明は、DEFAULTの場合で行っていますので注意して下さい。
analogReadResolution
analogResolutionは、Arduino Dueボードの為の関数です。 analogRead()が返す値のサイズ(ビット数)を設定します。デフォルトは10ビット(0~1023)で、従来のArduinoボードと互換性があります。 Arduino Dueは12ビットADコンバータを搭載しており、分解能(resolution)を12ビットに変更することで0~4095の値を得ることができます。
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
//Arduino Sketch Example: analogReadResolution //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax analogReadResolution(bits) //bits: determines the resolution (in bits) of the value returned by analogRead() function. You can set this 1 and 32. //You can set resolutions higher than 12 but values returned by analogRead() will suffer approximation. //See the note below for details. //Example void setup() { // open a serial connection Serial.begin(9600); } void loop() { // read the input on A0 at default resolution (10 bits) // and send it out the serial connection analogReadResolution(10); Serial.print("ADC 10-bit (default) : "); Serial.print(analogRead(A0)); // change the resolution to 12 bits and read A0 analogReadResolution(12); Serial.print(", 12-bit : "); Serial.print(analogRead(A0)); // change the resolution to 16 bits and read A0 analogReadResolution(16); Serial.print(", 16-bit : "); Serial.print(analogRead(A0)); // change the resolution to 8 bits and read A0 analogReadResolution(8); Serial.print(", 8-bit : "); Serial.println(analogRead(A0)); // a little delay to not hog serial monitor delay(100); } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:analogReadResolution()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、幾つかの分解能をanalogReadResolutionで指定してADCをテストします。
なお、この関数を使ってボードが提供するADCの能力を超える分解能を設定すると、不足するビットにはゼロが詰められます。
ボードの提供するADCの能力を超えた設定の場合
不足するビットにはゼロが詰められます。例えば、Dueボードで、
analogReadResolution(16)としたときにanalogRead()が返す値は、16ビットのうち上位12ビットだけがADCの結果で、下位4ビットにはゼロが詰められています。
ボードの能力よりも低い分解能を設定した場合
下位ビットが捨てられます。
つまり実際のボードよりも高い16ビットの分解能を指定しておけば、将来的にもあらゆるボードを変えた場合のスケッチを変更点を減らせる可能性があります。
analogWriteResolution
analogWriteResolutionは、Arduino Dueの為の関数です。 analogWrite()の分解能を指定し、デフォルトは8ビット(0~255)で、従来のArduinoボードと互換性があります。 Aruduino Dueは、8ビットのPWM(他のボードと同じ)を12ピンと、12ビットのDAC(DAコンバータ)を2つ持っています。この関数で12ビットを指定すると、DACの分解能をフル活用する12ビット(0~4095)の出力が得られ、PWMを使うときも符合が反転することがありません。
analogWrite()関数が使用する値の分解能は、bits単位で指定します。bits範囲は1から32です。
ハードウエアの能力を超える分解能を設定した場合
出力値は切り捨てられます。
ハードウエアの能力より小さい分解能を設定した場合
出力値にゼロが詰められます。
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//Arduino Sketch Example: analogWriteResolution //Date: 2015.1.12 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax analogWriteResolution(bits) //bits: determines the resolution (in bits) of the values used in the analogWrite() function. //The value can range from 1 to 32. If you choose a resolution higher or lower than your board's hardware capabilities, //the value used in analogWrite() will be either truncated if it's too high or padded with zeros if it's too low. See the note below for details. void setup(){ // open a serial connection Serial.begin(9600); // make our digital pin an output pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); } void loop(){ // read the input on A0 and map it to a PWM pin // with an attached LED int sensorVal = analogRead(A0); Serial.print("Analog Read) : "); Serial.print(sensorVal); // the default PWM resolution analogWriteResolution(8); analogWrite(11, map(sensorVal, 0, 1023, 0 ,255)); Serial.print(" , 8-bit PWM value : "); Serial.print(map(sensorVal, 0, 1023, 0 ,255)); // change the PWM resolution to 12 bits // the full 12 bit resolution is only supported // on the Due analogWriteResolution(12); analogWrite(12, map(sensorVal, 0, 1023, 0, 4095)); Serial.print(" , 12-bit PWM value : "); Serial.print(map(sensorVal, 0, 1023, 0, 4095)); // change the PWM resolution to 4 bits analogWriteResolution(4); analogWrite(13, map(sensorVal, 0, 1023, 0, 127)); Serial.print(", 4-bit PWM value : "); Serial.println(map(sensorVal, 0, 1023, 0, 127)); delay(5); } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:analogWriteResolution()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、幾つかのアナログ出力分解能をanalogWriteResolutionで設定しています。
Sponsored Link