GPIO介紹
GPIO (General Purpose Input/Output),我們可以用來控制一條線的訊號,可分為輸入或輸出。
輸入的情況裡,GPIO會根據外面進來的電壓來決定讀到的值是0或是1。
輸出的情況裡,如果我們設定GPIO為1, 那麼GPIO就會輸出板子的工作電壓,以Ameba為例,會輸出3.3V的電壓。如果我們設定GPIO為0, 那麼會輸出0V的電壓。
輸入的情況裡,GPIO會根據外面進來的電壓來決定讀到的值是0或是1。
輸出的情況裡,如果我們設定GPIO為1, 那麼GPIO就會輸出板子的工作電壓,以Ameba為例,會輸出3.3V的電壓。如果我們設定GPIO為0, 那麼會輸出0V的電壓。
材料準備
範例說明
在Button這個例子裡, 使用2根GPIO。
其中D2為GPIO輸入,它接在按鈕上面,按鈕另一端接在3V3的地方。
D13則是GPIO輸出,它接到LED燈泡上。當我們按下接在D2的按鈕時,D2的GPIO會讀到1的值,此時我們將D13的輸出設定為1, 並且使接在D13的LED燈泡發亮。
這是Arduino的Button範例說明:
https://www.arduino.cc/en/Tutorial/Button
我們打開Button這個範例,它在 “File” -> “Examples” -> “02.Digital” -> “Button”
在接線時,要注意Arduino的工作電壓是5V,所以Arduino的範例說明裡將按鈕接到5V的地方。但是Ameba的工作電壓是3.3V,所以我們將要按鈕接到3V3的地方。
LED燈泡的接線裡,LED比較長的腳為正極,比較短的腳為負極,所以我們要將短腳接到GND的地方(電壓為0),並將長腳接到D13的地方。為了避免LED因為電流太大而燒壞,通常會在正極的前面加上電阻。現在很多LED其實都做得很好,不加電阻也不會燒壞,但保險起見還是加上電阻。
執行的情況裡,只要使用者按下按紐,LED燈泡就會亮,放開時則LED燈泡變暗。
其中D2為GPIO輸入,它接在按鈕上面,按鈕另一端接在3V3的地方。
D13則是GPIO輸出,它接到LED燈泡上。當我們按下接在D2的按鈕時,D2的GPIO會讀到1的值,此時我們將D13的輸出設定為1, 並且使接在D13的LED燈泡發亮。
這是Arduino的Button範例說明:
https://www.arduino.cc/en/Tutorial/Button
我們打開Button這個範例,它在 “File” -> “Examples” -> “02.Digital” -> “Button”
LED燈泡的接線裡,LED比較長的腳為正極,比較短的腳為負極,所以我們要將短腳接到GND的地方(電壓為0),並將長腳接到D13的地方。為了避免LED因為電流太大而燒壞,通常會在正極的前面加上電阻。現在很多LED其實都做得很好,不加電阻也不會燒壞,但保險起見還是加上電阻。
執行的情況裡,只要使用者按下按紐,LED燈泡就會亮,放開時則LED燈泡變暗。
程式碼說明
在Setup()裡面,
pinMode決定了GPIO為input或output:
https://www.arduino.cc/en/Reference/PinMode
digitalRead可以從GPIO的輸入裡讀到0或1:
https://www.arduino.cc/en/Reference/DigitalRead
digitalWrite則可以輸出3V3或0到GPIO:
https://www.arduino.cc/en/Reference/DigitalWrite
pinMode決定了GPIO為input或output:
https://www.arduino.cc/en/Reference/PinMode
digitalRead可以從GPIO的輸入裡讀到0或1:
https://www.arduino.cc/en/Reference/DigitalRead
digitalWrite則可以輸出3V3或0到GPIO:
https://www.arduino.cc/en/Reference/DigitalWrite
與Arduion的差異
在這個範例裡,Arduion的官方網站上提到,由於它的GPIO輸入預設是Floating,所以在按鈕按下去之前,它處於 “空接” 的狀態,也就是它什麼都沒接,此時因為什麼都沒接,所以那根GPIO也不會曉得它的輸入是0或者是1,於是它的值會亂飄 (Floating),因此會看到LED燈泡在我們沒按下按鈕,它也有可能會亮。
為了解決這個問題,我們可以根據我們的應用將GPIO輸入設定為下拉(Pull Down)或是上拉(Pull Up),意思是當空接的時候,內部硬體會將GPIO的電壓放在GND或3V3的地方。這種方式稱之為Internal Pull Down/Up。
另一種解決方式,則是加一個很大的電阻(10M)在GPIO上,另一端接到GND或3V3,那麼平時空接的時候,就會有個電壓值供參考。這種方式稱之為External Pull Down/Up。 為了避免使用者第一次使用就要檢查這種差異, Ameba在輸入的預設裡是Internal Pull Down。也就是空接的時候,輸入讀到的值是0。
為了解決這個問題,我們可以根據我們的應用將GPIO輸入設定為下拉(Pull Down)或是上拉(Pull Up),意思是當空接的時候,內部硬體會將GPIO的電壓放在GND或3V3的地方。這種方式稱之為Internal Pull Down/Up。
另一種解決方式,則是加一個很大的電阻(10M)在GPIO上,另一端接到GND或3V3,那麼平時空接的時候,就會有個電壓值供參考。這種方式稱之為External Pull Down/Up。 為了避免使用者第一次使用就要檢查這種差異, Ameba在輸入的預設裡是Internal Pull Down。也就是空接的時候,輸入讀到的值是0。
Introduction of GPIO
We can use GPIO (General Purpose Input/Output) to control the input and output signal of a board.
In the input case, GPIO reads the input signal, and depends on the voltage, GPIO decides whether the signal represents value 0 or 1.
In the output case, GPIO outputs its operating voltage when we set GPIO to 1. Take Ameba as an example, it outputs 3.3V when the GPIO is set to 1. Otherwise, if we set GPIO to 0, it outputs 0V.
In the input case, GPIO reads the input signal, and depends on the voltage, GPIO decides whether the signal represents value 0 or 1.
In the output case, GPIO outputs its operating voltage when we set GPIO to 1. Take Ameba as an example, it outputs 3.3V when the GPIO is set to 1. Otherwise, if we set GPIO to 0, it outputs 0V.
Materials
Procedure
In this example, we will use 2 GPIO pins.
First, we use D2 as GPIO input. Please connect one end of the button to D2, and connect the other end to 3V3.
Next, we use D13 as GPIO output. Please connect the LED to D13.
When we press the button, GPIO input (D2) reads value 1, then we set GPIO output (D13) to 1, so as to make the connected LED lights.
For more information, please refer to the documentation of this example on the Arduino website:
https://www.arduino.cc/en/Tutorial/Button
Open the "Button" example in "File" -> "Examples" -> "02.Digital" -> "Button"
Note that in the documentation, the button is connected to 5V, because the operating voltage of Arduino is 5V. However, the operating voltage of Ameba is 3.3V, so we should connect the button to 3V3.
In an LED, the longer pin is the positive pole, and the shorter pin is the negative pole. Therefore, we connect the shorter pin to GND (voltage is 0), and the longer pin to D13. Additionally, to avoid the electric current exceeds the tolerance of the LED and causes damage, we connect a resistance on the positive pole.
Finally, when we press the button, the LED lights. And when we release the button, the LED dims.
First, we use D2 as GPIO input. Please connect one end of the button to D2, and connect the other end to 3V3.
Next, we use D13 as GPIO output. Please connect the LED to D13.
When we press the button, GPIO input (D2) reads value 1, then we set GPIO output (D13) to 1, so as to make the connected LED lights.
For more information, please refer to the documentation of this example on the Arduino website:
https://www.arduino.cc/en/Tutorial/Button
Open the "Button" example in "File" -> "Examples" -> "02.Digital" -> "Button"
In an LED, the longer pin is the positive pole, and the shorter pin is the negative pole. Therefore, we connect the shorter pin to GND (voltage is 0), and the longer pin to D13. Additionally, to avoid the electric current exceeds the tolerance of the LED and causes damage, we connect a resistance on the positive pole.
Finally, when we press the button, the LED lights. And when we release the button, the LED dims.
Code Reference
In Setup(),
we use pinMode(pin, mode) to configure the specified pin to behave either as an input or an output:
https://www.arduino.cc/en/Reference/PinMode
and use digitalRead(pin) to read the value from a specified pin, and returns HIGH or LOW:
https://www.arduino.cc/en/Reference/DigitalRead
use digitalWrite(pin, value) to write a HIGH or a LOW value to a digital pin:
https://www.arduino.cc/en/Reference/DigitalWrite
we use pinMode(pin, mode) to configure the specified pin to behave either as an input or an output:
https://www.arduino.cc/en/Reference/PinMode
and use digitalRead(pin) to read the value from a specified pin, and returns HIGH or LOW:
https://www.arduino.cc/en/Reference/DigitalRead
use digitalWrite(pin, value) to write a HIGH or a LOW value to a digital pin:
https://www.arduino.cc/en/Reference/DigitalWrite
Comparison with Arduino
According to the documentation on the Arduino website, the GPIO input of Arduino is set to "floating" by default. In other words, before the button is pressed, the input is not connected to anything, therefore the GPIO input is not able to determine the value of input, so the value will be "floating". As a result, the LED may light when we are not pressing the button.
To solve this problem, we can set the GPIO input to GND ("pull down") or 3V3 ("pull up") when the input pin is not connected to anything. This approach is called "Internal Pull Down/Up".
Another solution is to connect one end of a resistor with large resistance (10M) to the GPIO pin, and connect the other end to GND or 3V3. This approach is called "External Pull Down/Up". By default, Ameba uses the "Internal Pull Down/Up" approach to avoid this problem, that is to say, when the GPIO pin is not connected, it reads the input value 0.
To solve this problem, we can set the GPIO input to GND ("pull down") or 3V3 ("pull up") when the input pin is not connected to anything. This approach is called "Internal Pull Down/Up".
Another solution is to connect one end of a resistor with large resistance (10M) to the GPIO pin, and connect the other end to GND or 3V3. This approach is called "External Pull Down/Up". By default, Ameba uses the "Internal Pull Down/Up" approach to avoid this problem, that is to say, when the GPIO pin is not connected, it reads the input value 0.


0 comments:
Post a Comment