Thursday, August 30, 2018

Sync'ing ESP8266 GPIO Pins to Arduino Pin Numbers

Many ESP8266 12E Boards come with Arduino style pin numbers labeled on the boards, but you use the GPIO pin numbers in your code. If you include the following lines of code in your sketch before setup(), you can use the number silkscreened on your board (different boards may have different pin matchings).

const byte D0 = 16;
const byte D1 = 5;
const byte D2 = 4;
const byte D3 = 0;
const byte D4 = 2;
const byte D5 = 14;
const byte D6 = 12;
const byte D7 = 13;
const byte D8 = 15;
const byte D9 = 3;
const byte D10 = 1;

Now you can use something like

digitalWrite(D6, HIGH); //uses D6 on the ESP8266

in your sketch.

Of the above GPIO numbers, not all of them are easily available for use. Some are used for flash memory (GPIO 6 - 11), others are used for boot and flash purposes (GPIO 0, 2 & 15). GPIO 0-15 all have a built-in pull-up resistor (use INPUT_PULLUP), just like in an Arduino, while GPIO16 has a built-in pull-down resistor. The safe ones are GPIO 1, 3-5, 12-14, and 16. All GPIO pins can source about 12ma, and sink close to 20ma.

The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable (which is a good thing). Unlike #define, it types the "variable" instead of making the compiler guess. For these reasons it's preferable to use const instead of #define (https://www.arduino.cc/reference/en/language/structure/further-syntax/define/).

A byte stores an 8-bit unsigned number, from 0 to 255. This takes less space than an int, which takes up two bytes. You may see some code samples use uint8_t instead of byte. It means the same thing.

Further reading:

https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-esp8266-lolin-nodemcu-getting-started/

http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html





No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...