Easy Basics: Project 076c ESP8266 ESP-12E module - MicroPython with GPIOs

of Acoptex.com in ESP8266 ESP-12

Basics: Project 076c

Project name: ESP8266 ESP-12E module - MicroPython with GPIOs

Tags: ESP, ESP8266, WI FI module, ESP-12E, Ai Thinker, AI-Cloud, SOC, GPIO, General Purpose Input Output, System On a Chip, IOT, internet of things, FTDI232, FTDI 232, LoLin NODEMCU V3, NODEMCU, V3, Development Board with USB-to-Serial Onboard, MicroPython, GPIO, interaction, read digital and analog inputs, how to control digital outputs, how to generate PWM signals

Attachment: sketch

In this project, you need these parts :

1. ESP8266 ESP-12E module with micro USB cable 1pc

2.Jumper cables F-M, M-M

3. Resistor 3 pcs (220 Ohm -2 pcs and 10 KOhm - 1pc)

4.LED 2 pcs (can be of different color)


5. Breadboard 1 pc

6. Momentary switch (Pushbutton) 1 pc

7. Potentiometer 1 pc

8. uPyCraft IDE (you can download and read more about it here)


We will learn how to read digital and analog inputs, how to control digital outputs and how to generate PWM signals.

In order to program the ESP8266 ESP-12E module with MicroPython you need to:

  1. Install uPyCraft IDE in your PC;
  2. Upload MicroPython firmware to your ESP8266 ESP-12E module.

In this project you will learn how to control the ESP8266 ESP-12E module GPIOs with MicroPython:

  • Read digital inputs;
  • Control digital outputs;
  • Read analog inputs;
  • Generate PWM signals.

We will use the momentary switch and LED. Read the state of the momentary switch and set the LED state accordingly – when you press the momentary switch the LED will be ON, when you release the momentary switch the LED will be OFF. We will also read the voltage from a potentiometer and dim the LED accordingly to the potentiometer shaft's position (angle).

Understanding the potentiometer

See more information about potentiometer here.

Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

You can read more about it here.

Signals and connections of the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

TX - transmit pin. GPIO pin

RX  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND ( or G) - ground pin.

RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

D0-D8 - GPIO (General Purpose Input Output) pins 

D5-D8 - SPI interface

D1-D2– I²C/TWI Interface

SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin

SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin

SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin

S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin

S2 (or SD2) - GPIO pin

S3 (or SD3) - GPIO pin

VU (or VUSB) - external power 5VDC.

A0 - ADC output.

RSV - reserved


The ESP8266 only supports analog reading in pin ADC0 (A0)

Step by Step instruction

  1. Install uPyCraft IDE on your PC;
  2. Plug the ESP8266 ESP-12E module to your PC and wait for the drivers to install (or install manually any that might be required).
  3. ESP8266 ESP-12E module - Uploading MicroPython firmware.
  4. Open uPyCraft IDE. Double-click uPyCraft_V1.1.exe file. A new window opens with the uPyCraft IDE software.
  5. Go to Tools -> Board. If you are using ESP8266 ESP-12E module select esp8266.
  6. Go to Tools -> Serial and select your ESP8266 ESP-12E module COM port (in our case it’s COM8). If you don’t see the COM Port, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers , check your USB cable too (it should be data cable).
  7. You have established a serial communication with your board. The >>> should appear in the Shell window after a successful connection with your board.
  8. Press New file button to create a new file.
  9. Press Save file button to save the file in your PC. Input file name main.py and press ok button.
  10. You should see the following in your uPyCraft IDE -the boot.py file in your device and a new tab with the main.py file.
  11. Press Download and run button to upload the file to your ESP8266 ESP-12E module. The device directory should now load the main.py file. Your ESP8266 ESP-12E module has two files - boot.py and main.py stored now.
  12. You should see a message download ok in the Shell window.
  13. Copy the following  sketch to the Editor on the main.py file.
  14. Press Stop button to stop any script from running in your board.
  15. Press Download and Run button to upload the script to the ESP8266 ESP-12E module.
  16. You should see a message download ok in the Shell window.
  17. Press the ESP8266 ESP-12E module on-board EN button to reboot it.
  18. The LED should be ON when you press the momentary switch.The LED brightness changes when you rotate the potentiometer.


To interact with the GPIOs we need to import the machine module that contains classes to interact with the GPIOs. Import the Pin class to interact with the pins, the ADC class to read analog value and the PWM class to generate PWM signals: from machine import Pin, ADC, PWM

Import the sleep() method from the time module. The sleep() method allows us to add delays to the code:
from time import sleep

After importing all the necessary modules create a Pinobject called led on GPIO 2 that is an OUTPUT: led = Pin(2, Pin.OUT)

The Pin object accepts the following attributes in the following order: Pin(pin number, pin mode, pull, value):

  • pin number refers to the GPIO we want to control;
  • pin mode can be input (IN), output (OUT) or open-drain (OPEN_DRAIN);
  • pull argument is used if we want to activate a pull up or pull down internal resistor (PULL_UP, or PULL_DOWN);
  • value corresponds to the GPIO state (if is is on or off): it can be 0 or 1 (True or False).  Setting 1 means the GPIO is on. If we don’t have any parameter used, its state is 0 by default (that’s what we’ll do in this example).

After creating the led object we need another instance of the Pin class for the momentary switch. The momentary switch connected to GPIO 15 and it’s set an input: button = Pin(15, Pin.IN)


If you’re using an ESP8266, it only supports ADC on ADC0 (A0) pin. To instantiate an ADC object with the ESP8266:
pot = ADC(0)

The ESP8266 only supports ADC on ADC0 (A0) pin. To create an ADC object with the ESP8266: pot = ADC(0)


Then create a PWM object called led_pwm on GPIO 4 with 5000 Hz: led_pwm = PWM(Pin(4), 5000)

To create a PWM object, we need to pass as parameters: pinsignal’s frequency, and duty cycle.

  • frequency can be a value between 0 and 78125. A frequency of 5000 Hz for an LED works just fine.
  • duty cycle can be a value between 0 and 1023. In which 1023 corresponds to 100% duty cycle (full brightness), and 0 corresponds to 0% duty cycle (unlit LED).

We’ll just set the duty in the while loop, so we don’t need to pass the duty cycle parameter at the moment. If you don’t set the duty cycle when instantiating the PWM object, it will be 0 by default.

We have a while loop that is always True. This is similar to the loop() function in the Arduino IDE. We start by getting the button state and save it in the button_state variable. To get the pin state use the value() method as follows:
button_state = button.value() This returns 1 or 0 depending on whether the momentary switch is pressed or not.

To set the pin state, use the value(state) method in the Pin object. In this case we’re setting the button_statevariable as an argument. This way the LED turns on when we press the momentary switch: led.value(button_state)

To read an analog input use the read() method on an ADCobject (in this case the ADC object is called pot): pot_value = pot.read()

 To control the duty cycle, use the duty() method on the PWM object (led_pwm). The duty() method accepts a value between 0 and 1023 (in which 0 corresponds to 0% duty cycle, and 1023 to 100% duty cycle). So, pass as argument the pot_value (that varies between 0 and 1023). This way we change the duty cycle by rotating the potentiometer: led_pwm.duty(pot_value)


We have learnt how to read digital and analog inputs, how to control digital outputs and how to generate PWM signals.


  • None


  • See attachments on the begining of this project

Other projects of Acoptex.com
Easy CBASIC Lesson 9 Compiling and debugging in Visual Studio of Acoptex.com in Csharp 10-04-2019
Easy Basics: Project 081d ESP8266 ESP-12E module and Nextion HMI display of Acoptex.com in ESP8266 ESP-12 24-03-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 09-11-2018
Viewed: 235 times