Skip to content

08A Project 1A: Using the RGB LED on the ESP8266 12

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

###Technologies, Tools, and Resources used:

  • An ESP8266-12 Full evaluation board, flashed with NodeMCU
  • A PC or laptop to program the device with
  • A 3.3V capable Serial-USB adapter
  • 4 x Male-Female DuPont Jumper wires
  • Micro USB to USB cable
  • ESPlorer IDE

This section is dedicated to users who have the ESP8266-12 Full evaluation board and no additional electrical hardware like the breadboard or separate LED. Testing on the NodeMCU using LUA commands to play with the RGB LED attached to the ESP8266-12 Full evaluation board is straight forward as shown bellow.

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).

Instructions

In this guide, the GPIO12, GPIO13 and GPIO15 will be used to turn on and off the LED. Each of this GPIO pins controls different colour GPIO12 – GREEN, GPIO13 – BLUE and GPIO15 – RED.

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)

1.Start by opening the ESPlorer with the ESP8266 connected to the computer and click on the ‘open’ port button. openESPlorer

2.To turn on/off the green LED of the ESP8266, the GPIO12 need to be set high to turn on and low to turn off. The corresponding IO index to the GPIO12 is pin 6 from the table.
Note: By default the green LED should be on so in this test we will turn it off. The code is as follow:

gpio.mode(6, gpio.OUTPUT) -- set pin index 6 to GPIO output mode
gpio.write(6, gpio.LOW) -- set GPIO12 to low              

Copy the codes and paste it on ESPlorer left panel. on LED

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

4.In order to turn it back on, change the code to ‘gpio.HIGH’.

gpio.mode(6, gpio.OUTPUT) -- set pin index 6 to GPIO mode   
gpio.write(6, gpio.HIGH) -- set GPIO12 to high          

set high

5.To adjust the brightness of the LED, the PWM command is issued: pwm.setup(pin, clock, duty) where pin is the index 1-12, clock is the pwm frequency 1-1000 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(6, 100, 512) -- set pin index 6 as pwm output, frequency is 100Hz, duty cycle is half.
pwm.start(6) -- start pwm          

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

pwm.stop(6) -- stop pwm for pin index 6
pwm.close(6) -- quit pwm for pin index 6        

6.To turn on the blue LED, change the number to 7 to set the GPIO13. Replace with the code above with these one.
Note: The blue LED at start-up is on but with a very faint light. So when setting it high the colour of the LED will have a combination of colour. The GPIO can be play around to get different LED colour.

gpio.mode(7, gpio.OUTPUT) -- set pin index 7 to GPIO mode   
gpio.write(7, gpio.HIGH) -- set GPIO13 to high      

or number 8 for red LED

gpio.mode(8, gpio.OUTPUT) -- set pin index 8 to GPIO mode
gpio.write(8, gpio.HIGH) -- set GPIO15 to high    

different LED

7.Instead of having to change the code to set it high or low every time. A better way is to use if else statement like this. Copy the following code and paste into ESPlorer then send it to the ESP8266. This code set the GPIO pin to high if it is low and vice versa. The pin number can be change as well if want other GPIO to be set.

pin = 6 -- assign 6 into variable pin   
gpio.mode(pin, gpio.OUTPUT) -- set pin index 6 to GPIO mode   
led_state = gpio.read(pin) -- read GPIO12 state (0,1) and assign it into led_state   
if led_state == gpio.LOW then -- if led_state is equal to low (0)   
led_state = gpio.HIGH -- assign led_state to gpio.high   
gpio.write(pin, led_state) -- set GPIO12 to high   
else   
led_state = gpio.LOW -- assign led_state to gpio.low   
gpio.write(pin, led_state) -- set GPIO12 to low   
end -- end of statement 

8.To save the coding being done, ESPlorer can help save the file onto the ESP8266 flash memory so that it can be run for future purposes. This is done by clicking on the ‘Save to ESP’ button. The output is as shown if it is done successfully. save to esp

That is the end of the guide on playing with the RBG LED on the ESP8266_12 Full evaluation board with the NodeMCU firmware. To enjoy more and allow easy access to all of the LED on the ESP8266 testing on Internet of Things the IoT Demo firmware can be flashed (06 Flashing the ESP8266 - Flash with IoT Demo Firmware on Windows). The section below show how to use the IoT Demo firmware.

Testing of the IoT Demo Firmware

###Technologies, Tools, and Resources used:

  • An ESP8266-12 Full evaluation board, flashed with IoT Demo
  • A Android phone with IoT.apk installed
  • 3 x AA cells battery

This section will cover how to use the IoT demo firmware. This firmware allow you to control all the GPIO pins on the ESP8266_12 Full evaluation board using a phone application (IoT.apk) from AI-Thinker.

At this stage, the IoT Demo firmware should be flashed onto the device (Section 06 - Flashing the device).

Instructions

1.To test the firmware, your phone will need to install an application called IoT.apk (Section Installing the toolchain). Transfer the file to the phone and start the installation process. After installation is completed the application is as shown.

2.After installing the application on the phone, with the ESP8266-12 Full evaluation board turned on. Open the WiFi option on the phone, the ESP8266-12 Full evaluation board will broadcast its own access point SSID "AI-THINKER_9B9963". Connect to the ESP8266 using the phone with the default password "ai-thinker".

3.Once connected, open the application IOTManager. The start-up screen is as follow.

4.During the start of the application, it will automatically search for the connection between the phone and ESP8266-12 Full evaluation board.

5.After discovering the device is successful, a screen like this will appear. This is where the brightness of the LED can be adjusted accordingly. GPIO12 controls Green, GPIO13 controls Blue and GPIO15 controls Red. Slides up or down the bar to see the brightness of the LED changes on the ESP8266 or tap on the light bulb to turn it off or on.

6.By clicking the next tab, more GPIO pins can be chosen to be control. The GPIO can be set to turn on or off and the lights on the ESP8266 will change with it.

7.Adjust and play with the application to know which GPIO correspond to which LED on the ESP8266. This is a fun and quick way to test the ESP8266-12 Full evaluation board at the same time to see Internet of Things.