Monday, February 20, 2017

Getting Started with the NodeMCU ESP8266 Board

In this tutorial we will use the Arduino IDE to compile and flash our code to the NodeMCU ESP8266. When we "flash" code to an embedded device, it will be written so that the device will retain that code and run it on reset or next power-up.

These instructions have been tested on Mac OS X 10.11.6.


Part 1: Connect Board and Install Drivers

By default, Mac OS X will not recognize the NodeMCU ESP8266 when it is plugged in! So we will install a driver so that ESP8266, when connected, appears as a serial port.

  1. Connect the ESP8266 dev board to your computer using a USB cable. Note: not all USB cables will work for this! In particular, cables designed for portable phone rechargers will usually not work. USB cables that are used for synching Android phones should be OK.
  2. On Mac, open a terminal window and type:
    ls /dev/cu.*
    You will find Bluetooth ports, but nothing we really want!
  3. Download and install the Silicon Labs VCP Driver. The URL is: https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
  4. Now we need to determine the serial port the ESP8266 is using, as the Arduino IDE may or may not detect it automatically. So, open a terminal window and type:
    ls -l /dev/cu.*
    The desired port should be something like /dev/cu.SLAB_USBtoUART
  5. If that port does not show, try using a different USB cable. Also, you may need to restart your computer at this point.

NOTE: In Windows, use the Device Manager to determine the NodeMCU's USB port.


Part 2: Setup Arduino IDE for ESP8266 Development

  1. Download IDE from Arduino.cc - current version is 1.8.1. URL is: https://www.arduino.cc/en/Main/Software
  2. On Mac, this will download arduino-1.8.1-macosx.zip to the Downloads folder. Double-click to install.
  3. Start the Arduino IDE
  4. Open Preferences window by choosing Arduino | Preferences… menu
  5. Add the following URL to the “Additional Boards Manager URLs” field: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  6. While we’re here, click the “Display line numbers” checkbox
  7. Press “OK” button to save these changes
  8. Open the Boards Manager by using “Tools | Board | Boards Manager…” menu
  9. In search box at top right of that window, enter “esp8266”
  10. Click on the “esp8266 by ESP8266 Community” item, and an “Install” button appears. Click it, and this will start the installation process.
  11. Once installation is complete, click the close button.
As a result of doing this:
  1. You can now target your code for the ESP8266
  2. A number of example programs have been installed, available from “File | Examples” menu

Part 3: Connect Board and Configure the IDE

  1. Connect the ESP8266 dev board to your computer using a USB cable.
  2. Start Arduino IDE if it isn't already running.
  3. Under “Tools | Board” menu, choose NodeMCU 1.0 (ESP-12E Module)
  4. In Arduino IDE, choose “Tools | Port | /dev/cu.SLAB_USBtoUART”
  5. If all goes well, the board type and port will be shown at bottom of Arduino IDE window.

Part 4: First Program

The default program in the IDE window is as follows:

1
2
3
4
5
6
7
8
9
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

For our first program, we will make the NodeMCU's onboard LED blink. Modify the code to read as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * BlinkLED
 * 
 * By: Mike Klepper
 * Date: 19 Feb 2017
 * 
 * This program blinks the LED on the NodeMCU board
 * 
 * LED_BUILTIN is used to find the pin for the onboard LED; 
 * the delay constants at the top are in milliseconds
 */

const int LED_ON_DELAY = 500;
const int LED_OFF_DELAY = 2000;

void setup() 
{
  pinMode(LED_BUILTIN, OUTPUT); 
  Serial.begin(115200);
}

void loop() 
{
  Serial.print("LED pin is: ");
  Serial.println(LED_BUILTIN);
  
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("LED is on");
  delay(LED_ON_DELAY); 
  
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("LED is off");
  delay(LED_OFF_DELAY); 
  
  yield();
  Serial.println("");
}

Lines Explanation
1-11 This is a multi-line comment. Arduino sketches support C/C++/Java/JavaScript's style of single-line and multi-line comments
13-14 Constants determining the length of time the LED is lit and unlit, measured in milliseconds
16 The setup() function is ran exactly once when the ESP8266 is powered-on or reset
18 LED_BUILTIN determines the pin number for controlling the user-controllable LED. This line sets that pin for output
19 Enables serial communication over the USB port between the board and the computer to which it is attached. The baud rate is set to 115200 here
16 The loop() function is ran after the setup() function completes
24-25 Write values to the serial port
27 Set the voltage sent to the pin to be LOW (on)
29 Causes control to wait for the specified number of milliseconds
31 Set the voltage sent to the pin to be HIGH (off)
35 Allows the ESP8266 to handle background processes, like WiFi connections

We now compile and flash this program:

  1. Click the arrow button at the top-left of the IDE window. The program will be compiled and then will be flashed to the ESP8266 board. When flashing, a blue LED on the NodeMCU board will flicker. Once the program starts, the red LED will turn-on for 0.5 seconds, and then turn-off for 2 seconds.
  2. The ArduinoIDE has a built-in serial monitor. To open it, click the magnifier glass icon at top right corner of the IDE.
  3. Set the speed to be 115200 baud to match the rate that the above program is transmitting. The serial monitor will display the results of the Serial.println() and Serial.print() statements. As you can see, LED_BUILTIN resolves to pin 16.

If you've completed this tutorial, then you now know how to use the Arduino IDE to write code for the ESP8266! Ooh RAH!

No comments:

Post a Comment