- Internet of Things with ESP8266
- Marco Schwartz
- 253字
- 2021-07-14 10:58:54
Reading data from a GPIO pin
In the second project in this chapter, we are going to read the state of a GPIO pin. For this, we will use the same pin as in the previous project. You can therefore remove the LED and the resistor that we used in the previous project.
Now, simply connect this pin (GPIO 5) of the board to the positive power supply on your breadboard with a wire, applying a 3.3V signal on this pin.
Reading data from a pin is really simple. This is the complete code for this part:
// Import required libraries #include <ESP8266WiFi.h> void setup(void) { // Start Serial (to display results on the Serial monitor) Serial.begin(115200); // Set GPIO 5 as input pinMode(5, INPUT);} void loop() { // Read GPIO 5 and print it on Serial port Serial.print("State of GPIO 5: "); Serial.println(digitalRead(5)); // Wait 1 second delay(1000); }
We simply set the pin as an input, read the value of this pin, and print it out every second. Copy and paste this code into the Arduino IDE, then upload it to the board using the instructions from the previous chapter.
This is the result you should get in the Serial monitor:
State of GPIO 5: 1
We can see that the returned value is 1 (digital state HIGH), which is what we expected, because we connected the pin to the positive power supply. As a test, you can also connect the pin to the ground, and the state should go to 0.
- 演進(jìn)式架構(gòu)(原書(shū)第2版)
- WebAssembly實(shí)戰(zhàn)
- Practical Internet of Things Security
- Reactive Programming With Java 9
- Python編程:從入門(mén)到實(shí)踐
- SQL經(jīng)典實(shí)例(第2版)
- 運(yùn)用后端技術(shù)處理業(yè)務(wù)邏輯(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- Extreme C
- HTML5權(quán)威指南
- SpringBoot從零開(kāi)始學(xué)(視頻教學(xué)版)
- HTML+CSS+JavaScript網(wǎng)頁(yè)制作:從入門(mén)到精通(第4版)
- 進(jìn)入IT企業(yè)必讀的324個(gè)Java面試題
- Visual Basic語(yǔ)言程序設(shè)計(jì)基礎(chǔ)(第3版)
- H5+移動(dòng)營(yíng)銷(xiāo)設(shè)計(jì)寶典
- C語(yǔ)言程序設(shè)計(jì)教程