官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 高阳县| 富川| 陇川县| 潮州市| 页游| 崇信县| 射洪县| 永城市| 浠水县| 资阳市| 祥云县| 惠来县| 东台市| 朔州市| 台东市| 绵竹市| 江永县| 黄陵县| 原阳县| 沭阳县| 巩留县| 礼泉县| 通州区| 德庆县| 山东| 巴彦淖尔市| 南溪县| 泸西县| 阜康市| 博客| 葫芦岛市| 永平县| 醴陵市| 林州市| 汝州市| 云浮市| 沙雅县| 东乌| 保靖县| 夏津县| 凤阳县|