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

Controlling an LED

First, we are going to see how to control a simple LED. The GPIO pins of the ESP8266 can be configured to realize many functions: inputs, outputs, PWM outputs, and also SPI or I2C communications. This first project will teach you how to use the GPIO pins of the chip as outputs:

  1. The first step is to add an LED to our project. These are the extra components you will need for this project:
  2. The next step is to connect the LED with the resistor to the ESP8266 board. To do so, the first thing to do is to place the resistor on the breadboard.
  3. Then, place the LED on the breadboard as well, connecting the longest pin of the LED (the anode) to one pin of the resistor.
  4. Then, connect the other end of the resistor to GPIO pin 5 of the ESP8266, and the other end of the LED to the ground.

    This is what it should look like at the end:

  5. We are now going to light up the LED by programming the ESP8266 chip, just as we did in the first chapter of the book by connecting it to the Wi-Fi network.

    This is the complete code for this section:

    // Import required libraries
    #include <ESP8266WiFi.h>
    
    void setup() {  
    
    // Set GPIO 5 as output
    pinMode(5, OUTPUT);
    
    // Set GPIO 5 on a HIGH state
    digitalWrite(5, HIGH);
    
    }
    void loop() {
    
    }

    This code simply sets the GPIO pin as an output, and then applies a HIGH state to it. The HIGH state means that the pin is active, and that positive voltage (3.3V) is applied to the pin. A LOW state would mean that the output is at 0V.

  6. You can now copy this code and paste it into the Arduino IDE.
  7. Then, upload the code to the board, using the instructions from the previous chapter. You should immediately see that the LED lights up. You can shut it down again by using digitalWrite(5, LOW) in the code. You could also, for example, modify the code so the ESP8266 switches the LED on and off every second.
主站蜘蛛池模板: 罗山县| 渭源县| 平山县| 洛南县| 抚顺市| 社旗县| 岳阳市| 呼图壁县| 隆化县| 徐汇区| 越西县| 新源县| 石柱| 塘沽区| 额尔古纳市| 化德县| 普兰店市| 曲沃县| 抚宁县| 新津县| 巴彦县| 金平| 庄浪县| 浦县| 抚州市| 延边| 蒙山县| 正蓝旗| 蕲春县| 河北省| 喀喇沁旗| 霍州市| 噶尔县| 明溪县| 桃园市| 孙吴县| 夹江县| 曲阳县| 大丰市| 鱼台县| 鲁甸县|