Skip to content

08 Project 1: Hello World! Blink LED

Edwin Wong edited this page May 21, 2015 · 3 revisions

###Technologies, Tools, and Resources used:

  • An ESP-01 or the ESP8266-12 Full evaluation board, flashed with NodeMCU
  • A PC or laptop to program the device with
  • A 3.3V capable Serial-USB adapter
  • Jumper cables
  • A breadboard
  • An LED
  • A wireless network to connect to

###Objectives:

  • Use Lua scripting to control the GPIO pins on the ESP8266
  • Learn to identify corresponding IO index to GPIO pins using the table index
  • Learn to use ESPlorer IDE to make a serial connection, load scripts to the device and run them.

At this stage you should be comfortable with making a serial connection (Section 5 - Communicating with the device) and have flashed the device with the latest edition of the NodeMCU firmware (Section 6 - Flashing the device).

This project will cover the 'Hello World!' of hardware, getting a light to turn on and off. We will turn an LED attached to a breadboard on and off using GPIO0. This means you will be able to use either the ESP-01 or the ESP-12 board for this project. To get into the spirit of Internet of Things applications, we will then extend this to a web page with a form, through which you or anyone on the local network will be able to turn the LED off or on.

Breadboard setup

Picture of the breadboard setup The setup of the breadboard, pictured here with an ESP-01. Voltage to the board is being supplied from the computer, via the USB-Serial adapter (the red wire connected to VCC, and the black wire connected to GND). The green wire connects Rx on the adapter to Tx on the ESP-01, and the white wire connects Tx on the adapter to Rx. The other red and black wires are connecting voltage and ground to the ESP-01, and the yellow wire connnects CH_PD to voltage (only needed if you are using the ESP-01, the ESP-12 is manufactured with this connection). The grey wire connects GPIO0 to the positive side of the LED, and the resistor connects the negative side to ground. The blue cable is attached to the Reset (RST) pin on the ESP8266; you can quickly insert and remove the pin on the ground rail of the breadboard to perform a hardware reset of the device.

Instructions

To start off, we want to know which index to use. NodeMCU needs the corresponding IO index to the GPIO pin. These are presented below. This index is used as the argument to the NodeMCU function for reading a GPIO pin, gpio.read(index).

IO index ESP8266 pin IO index ESP8266 pin
0 [*] GPIO16 7 GPIO13
1 GPIO5 8 GPIO15
2 GPIO4 9 GPIO3
3 GPIO0 10 GPIO1
4 GPIO2 11 GPIO9
5 GPIO14 12 GPIO10
6 GPIO12
(GitHub 2015)

*0 (GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. This my become a problem in your own personal projects however it will not interfere with this guide

1.Start by opening the ESPlorer with the ESP8266 connected to the computer and click on the ‘open’ port button.
Note: If there is no COM port in the text box above you may need to click the refresh button on the right hand side of the open button. Also Make sure the COM port is the same as you discovered in Section 5 (Communicating with the ESP8266: wiring up the device for a serial connection, Step 6)

2.To turn on/off the LED on the breadboard, the GPIO0 needs to be set to output mode, and then set to high to turn on and low to turn off. The corresponding IO index to the GPIO0 is pin 3 from the table.
The code is as follows:
Note: for the ESP8266_12 besides the GPIO12,13,15 the rest of the GPIO pins works oppositely. The LED will turn on when set low and off when set high.

gpio.mode(3, gpio.OUTPUT, gpio.PULLUP) -- set GPIO0 to output mode
gpio.write(3, gpio.HIGH) -- set GPIO0 to high               

Try this code out by copying and pasting it to one of the snippets in ESPlorer.

3.Send the commands to the ESP8266 by clicking the ‘Send to ESP’ button. The LED will turn on.

Example Picture of running a script

4.In order to turn it back off, change the code to ‘gpio.LOW’.

gpio.write(3, gpio.LOW) -- set GPIO0 to low              

To read the state of the pin use this command. 0 is set low, 1 is set high.

print(gpio.read(3))

5.To adjust the brightness of the LED, the PWM (Pulse Width Modulation, Read more about this in Section 3 Equipment) command is issued: pwm.setup(pin, clock, duty) where pin is the index 1-12, clock is the PWM frequency 1-1000 (the lower the clock the slower the LED flicker the higher the clock the faster the LED flicker) and duty is the duty cycle 0-1023 (the lower the duty cycle the lower the brightness the higher it is the brighter it gets)

pwm.setup(3, 100, 512) -- set pin index 3 as PWM output, frequency is 100Hz, duty cycle is half.
pwm.start(3) -- start PWM          

To stop the pwm and quit the PWM mode, issue the following commands:

pwm.stop(3) -- stop PWM for pin index 3
pwm.close(3) -- quit PWM for pin index 3        

6.To turn on and off the LED repeatedly the timer alarm is used. Try sending the following code snippet:

 -- Blink an LED light attached to GPIO0 on and off repeatedly
    gpio.mode(3, gpio.OUTPUT, gpio.PULLUP)

    function blinky()
        if gpio.read(3) == 1 then
            gpio.write(3, gpio.LOW)
        else
            gpio.write(3, gpio.HIGH)
        end
    end
    
    tmr.alarm(0, 500, 1, blinky)

7.The parameter of the timer is tmr.alarm(id, interval, repeat, function do()). To stop the timer alarm, use this command where 1 is the alarm id.

tmr.stop(0) -- stop timer alarm id 0

Go to 08 Project 1: Blink LED Through Webserver for continuation and extension of Project 1.