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.
- 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.
- On Mac, open a terminal window and type:
ls /dev/cu.*
- 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
- 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.*
- If that port does not show, try using a different USB cable. Also, you may need to restart your computer at this point.
Part 2: Setup Arduino IDE for ESP8266 Development
- Download IDE from Arduino.cc - current version is 1.8.1. URL is: https://www.arduino.cc/en/Main/Software On Mac, this will download arduino-1.8.1-macosx.zip to the Downloads folder. Double-click to install.
- Start the Arduino IDE
- Open Preferences window by choosing Arduino | Preferences… menu
- Add the following URL to the “Additional Boards Manager URLs” field: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- While we’re here, click the “Display line numbers” checkbox
- Press “OK” button to save these changes
- Open the Boards Manager by using “Tools | Board | Boards Manager…” menu
- In search box at top right of that window, enter “esp8266”
- Click on the “esp8266 by ESP8266 Community” item, and an “Install” button appears. Click it, and this will start the installation process.
- Once installation is complete, click the close button.
- You can now target your code for the ESP8266
- A number of example programs have been installed, available from “File | Examples” menu
Part 3: Connect Board and Configure the IDE
- Connect the ESP8266 dev board to your computer using a USB cable.
- Start Arduino IDE if it isn't already running.
- Under “Tools | Board” menu, choose NodeMCU 1.0 (ESP-12E Module)
- In Arduino IDE, choose “Tools | Port | /dev/cu.SLAB_USBtoUART”
- 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: } |
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 |
- 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.
- The ArduinoIDE has a built-in serial monitor. To open it, click the magnifier glass icon at top right corner of the IDE.
- 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