-
1Getting Started with Arduino & ESP8266
For newbies to programming, I would highly recommend this approach over development with the SDK. Developing with Arduino adds a lot of convenience at the price of some control. However almost anything you want to develop can be done with Arduino, it just may not be as pretty, as fast or as small as doing it "by hand" with the SDK.
Getting set up with Arduino is super easy. If you don't already have it, download the latest version of the Arduino IDE from the Arduino website. On Debian/Ubuntu, it can be installed from repository with:
$ sudo apt install arduino
And, alternatively, you can build it from source if that's your thing (it's actually quite straightforward if you've built anything from source before). On Windows and Mac, just follow the installer. Open the application after it's installed, and you'll be greeted with a blank project screen. Click File->Preferences and then in the box "Additional Boards Manager URL:" add the following link:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
After you add this link, you can then click OK to close the preferences box. Now, click on the Tools menu, hover over the Board entry, and at the top of the list you will see Boards Manager... Open this dialog box, and in the search box, type ESP8266. You will see an entry called "esp8266 by ESP8266 Community". Click install, and this will add the latest version.
Once it is finished installing, restart the Arduino IDE. Now when you go to Tools->Boards you will see a bunch of ESP8266 boards and modules! Pick whichever board you are using. After choosing the board, you will see that the File->Examples menu has been populated with a whole bunch of examples for your module. We'll take a look at a few different examples. The Blinky example is a great way to test your module/board to make sure the USB connection is working.
-
2Getting to Blinky
Arduino is the fastest way to get up and running with the ESP8266. All we've had to do is install the Arduino environment, add ESP8266 to the Boards Manager, and then select our board after we plug it in. The ESP8266 community has written all the code which abstracts away the nitty gritty of the SDK into much easier to use functions. Let's take a look at our Blinky code:
void setup() { pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is active low on the ESP-01) delay(1000); // Wait for a second digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH delay(2000); // Wait for two seconds (to demonstrate the active low LED) }
If you have done any work with Arduino in the past, the code is indistinguishable from code for any other Arduino board. This is the power of abstraction. Arduino doesn't care what the hardware is, as long as it has an Arduino bootloader. There are hundreds upon hundreds of tutorials for Arduino out there that will get you up to speed. However, let's take a look at what the code is doing, and then why Arduino makes developing on the ESP8266 so easy.
For those of you completely new to using Arduino, the language used is C++. C++ is technically a different language than C, but you can write pure C in a C++ program and it will work and compile just fine. In this way, C++ is more a superset of C; it's basically C with a bunch of new stuff added on. In Arduino, the C++ we use is very much more like C than traditional C++. This is because almost all of the complexity has been abstracted away by using the power of object-oriented programming in the included libraries. This makes the code the user has to write much less dense and easier to use.
Let's go through the code quickly. I don't want to give a full tutorial on how to use the Arduino environment--there are literally thousands of them out there, many much better than what I could squeeze into this tutorial. However, in the name of expediency, I will explain enough to get you off and running.
Arduino uses two non-standard functions to divide all your code into two sections: things you want to happen once when the program starts, and things you want to happen over and over again while the program is running. These are the setup() and loop() functions. The very first thing the ESP8266 does when running this code is look inside setup(). The only statement we have there is a call to a function called pinMode(). We set a pin to be an output so we can turn the LED on and off. Notice that instead of giving a number, it uses the pre-defined name LED_BUILTIN. This is a special definition that will change depending on the board you are programming to always point to an LED that the user can use. In this case, it's pin 0, which is the built-in red LED on the Feather HUZZAH.
After it's done with pinMode(), it gets to the end of the setup() function and then jumps into the loop() function. The loop() function will run forever and ever and ever until something stops it -- usually power being disconnected. Our loop function only does a few basic things. It uses another Arduino function call, digitalWrite(), to change the state of the pin we prepared in setup(). It sets it LOW, which turns the LED on. This might be counterintuitive, but take a look at the schematic for the LED below.
Notice which way the LED is pointing? Current has to flow from the 3.3V power rail, through the LED, through the resistor, and then to ground in order for the LED to light up. When we set the pin to LOW, a transistor inside the pin turns on, connecting that pin to the chip's ground pin. This type of inverse signalling is often referred to as active low; the circuit is active when given a 0, or ground.
After this, we use the delay() function to wait for 1000ms. If we didn't do this, the LED would flash on and off so quickly that we wouldn't be able to see it. Using delay() is fine in this context, but once you get into more advanced programs it's advisable to avoid using it unless absolutely necessary. This is because during this type of delay, the processor sits in a loop, running at full speed and unable to do anything else. In low-power applications this can be a huge waste of battery, and in any other scenario it blocks the processor from being able to do anything else during that time. The solution to this is to use timers and interrupts, but if this is your first rodeo you're still a ways off from learning all that!
The next line just does the opposite of the previous digitalWrite -- it sets the pin HIGH, which stops current flowing and extinguishes the LED. We wait for another second, and then reach the end of the function -- which simply returns us back to the beginning of the function. This will loop infinitely unless we reset the chip or cut power. Pretty simple code, right? If you followed all this so far, then you'll have no problem learning how to program the ESP8266 with Arduino! Let's take a look at why using Arduino with the ESP8266 is so popular.
The function we call in setup(), pinMode(), takes the number of a pin and the type of output to set it to, as we established. If we were programming in C using the ESP8266 SDK, we would have to do quite a bit more work to get this basic thing to happen: we'd have to include a new header file, read the SDK documentation and figure out how the function call works, and add a library to the list that gets passed to the linker in the Makefile. The people who wrote the ESP8266 libraries for Arduino have done all this hard work for you. Under the hood, Arduino is doing exactly what I described, but all you need to know is that one function call.
The other powerful thing about Arduino is that function names have become standardised. This means that pinMode() -- or any standard function in the Arduino environment -- will work the same on an Arduino Uno, an ESP8266, or a FLORA. Any board that has support in the Arduino IDE will be able to use the same code. This is a big reason for Arduino's popularity. Sharing code and libraries is very easy and essentially guaranteed to work on boards that are officially supported.
Even more advanced features, like using Wi-fi, has been written in such a way that in most cases you can use code written for a different device and it will just work. This makes development much faster, and can be a very useful tool; however, I personally think that hiding the true mechanisms of a microcontroller take all the real fun and learning out of it. This is why after you get used to using the Arduino system, I highly recommend leaning the basics of C and give a try at using the SDK. You can check out the sister tutorial to this one!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.