ESP32 – Getting Started MicroPython -- on Board Blink LED

Introduction: ESP32 – Getting Started MicroPython -- on Board Blink LED

The "ESP32 -- Getting Started -- On Board Blink LED" explains about, installing MicroPython and accessing the on-board LED, through Python program

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

Getting Started Video Link

Step 1: Abstract

The ESP32 is constructed with Xtensa dual-core (or single-core) 32-bit RISC architecture, operating at 160MHz or 240MHz, with up to 520KiB internal SRAM, integrated with Wi-Fi (802.11 b/g/n) and Bluetooth (v4.2 BR/EDR and BLE (shares the radio with Wi-Fi). The ESP32 provides peripheral interfaces like ADC, DAC, touch sensors, SPI, I2C, I2S, UART, CAN, PWM etc. The ESP32 supports all IEEE 802.11 standard security feature (WFA, WPA/WPA2 and WAPI), and cryptographic hardware acceleration (AES, RSA, SHA-2, ECC, RNG etc). It supports wake up from GPIO / Sensor interrupts, timers and 5uA deep sleep current consumption.

This experimentation explains about, getting started with ESP32 and accessing the on-board LED, through MicroPython program

Step 2: Components

ESP32 Development Board

Micro USB Cable

Step 3: Reference

thonny.org

https://micropython.org/download/esp32/

Step 4: Schematic

Connect the ESP32 board to USB port of the PC / Laptop as illustrated.

Download and install the Thonny Micro Python integrated development environment (IDE) from following web site or githhub

thonny.org

https://github.com/thonny/thonny/releases/tag/v3....

Run Thonny IDE

Step 5: ESP32 Interpreter Selection

Select the Tools -- Options – Interpreter – MicroPython (ESP32)

Select port as “Silicon Labs CP210x to UART Bridge

Plug in the ESP32 to micro-USB cable for connecting to PC or Laptop.

Select “Install or update firmware” option for installing the MicroPython.

Step 6: ESP32 MicroPython Firmware Installer

Select “Install or update firmware” option for installing the MicroPython.

Select port as “Silicon Labs CP210x to UART Bridge”

Download the stable MicroPython firmware for ESP32, as given in below link.

https://micropython.org/download/esp32/

Select the MicroPython firmware from local drive.

Select “Install” button and then press “BOOT” switch on the ESP32 board.

This process, erases the flash and then installs the MicroPython on ESP32 development board.

Now the ESP32 will be identified as removable mass storage drive on PC or Laptop, and contains a “boot.py” dummy start up file.

Drag and drop the needed python (“.py”) files.

Drag and drop the needed python (“.py”) files. The “boot.py” is the first starting file, which start executes after power up, and then follows the “main.py”.

Step 7: Python - Slow Blink Program

ESP32 contains an on-board LED, which is connected to GPIO2 general purpose Digital In / Out pin.

Import Pin and Timer libraries

The document related to MicroPython “TIMER” class can be found on the following link

o https://docs.micropython.org/en/latest/library/ma...

MicroPython’s Timer class defines a baseline operation of executing a callback with a given period (or once after some delay)

timer.init function callbacks the blink functionality for toggling the LED at 500mS duration. (frequency = 2 per second)

Write the main.py into ESP32 board, and “power OFF” then “power ON” will slow blink the on-board LED at 500mS duration.

'''<br>Description: ESP32 Onboard LED Blink Program.
Author     : M.Pugazhendi
Date       : 28thSep2021
A. Intialize timer_one, trigger LED blink period to 500 mSec.
'''
from machine import Pin, Timer
import time
led = Pin(2, Pin.OUT)
timer = Timer(0)
toggle = 1
def blink(timer):
    global toggle
    if toggle == 1:
        led.value(0)
        toggle = 0
    else:
        led.value(1)
        toggle = 1
timer.init(freq=2, mode=Timer.PERIODIC, callback=blink)

Step 8: Python - Fast Blink Program

ESP32 contains an on-board LED, which is connected to GPIO2 general purpose Digital In / Out pin.

Import Pin and Timer libraries

The document related to MicroPython “TIMER” class can be found on the following link

o https://docs.micropython.org/en/latest/library/ma...

MicroPython’s Timer class defines a baseline operation of executing a callback with a given period (or once after some delay)

timer.init function callbacks the blink functionality for toggling the LED at 100mS duration. (frequency = 10 per second)

Write the main.py into ESP32 board, and “power OFF” then “power ON” will slow blink the on bord LED at 100mS duration.

'''<br>Description: ESP32 Onboard LED Blink Program.
Author     : M.Pugazhendi
Date       : 28thSep2021
A. Intialize timer_one, trigger LED blink period to 100 mSec.
'''
from machine import Pin, Timer
import time
led = Pin(2, Pin.OUT)
timer = Timer(0)
toggle = 1
def blink(timer):
    global toggle
    if toggle == 1:
        led.value(0)
        toggle = 0
    else:
        led.value(1)
        toggle = 1
timer.init(freq=10, mode=Timer.PERIODIC, callback=blink)

Step 9: Python Program -- Change State, Using Two Timers

ESP32 contains an on-board LED, which is connected to GPIO2 general purpose Digital In / Out pin.

Import Pin and Timer libraries

Two timers are initialized.

The timer_two is used for changing the state @20 Sec interval.

And initialize timer_one ,for triggering LED blink period either 100mSec or 250mSec or 1Sec interval.

Timer_two.init function callbacks the ChangeState functionality at 20 Sec duration, for toggling the LED, as per state change.

State variable is used for changing the count.

Write the main.py into ESP32 board, and “power OFF” then “power ON” will execute the program.

'''
Description: ESP32 Onboard Timer / Scheduler Program. Author : M.Pugazhendi Date : 28thSep2021
A. Two timers are initialized.
B. The timer_two is used for changing the state @20 Sec interval
C. Initialize timer_one, trigger LED blink period either 100mSec or 250mSec or 1Sec.
'''
from machine import Pin, Timer
#Initialize the onboard LED as ouput
led = Pin(2, Pin.OUT)
#Initialize timer_one. Used for toggeling the LED
timer_one = Timer(0)
#Initialize timer_two. Used for changing the State
timer_two = Timer(1)
#Initialize state variable. Used for changing the State
state = 1
toggle = 1
def BlinkLED(timer_one):
    global toggle
    if toggle == 1:
        led.value(0)
        toggle = 0
    else:
        led.value(1)
        toggle = 1
    
def ChangeState(timer_two):
    global state
    
    if state == 1:
     # 100mS Timer initialization   
     timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
     state = state+1
    elif state == 2:
     # 250mS Timer initialization
     timer_one.init(freq=4, mode=Timer.PERIODIC, callback=BlinkLED)
     state = state+1
    elif state == 3:
     # 1000mS Timer initialization
     timer_one.init(freq=1, mode=Timer.PERIODIC, callback=BlinkLED)
     state = 1
    else:
      # Default state
      # 100mS Timer initialization
      state = 1
      timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
 
# Initialize the timer one for first time
timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
state = state+1
#Subcequent timer states for 20 seconds interval
timer_two.init(freq=0.05, mode=Timer.PERIODIC, callback=ChangeState)

Step 10: Conclusion

The project is successfully completed with ESP32, on board LED and educated on following subjects.

Install and running the Tonny python compiler.

Connecting and identifying the ESP32 as mass storage device.

Flashing the MicroPython firmware into ESP32.

Utilizing MicroPython “Pin” and “Timer” libraries

Simple timer event generation and task execution mechanism for executing the python program.

Programming on board LED, and executing the program on ESP32 hardware

Step 11: Results Video and Links

SlowBlink.mp4

FastBlink.mp4

ChangeState.mp4

Please Visit You Tube for the Videos:,

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

"ESP32 -- Getting Started -- on Board Blink LED"

Be the First to Share

    Recommendations

    • Electronics Contest

      Electronics Contest
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest
    • Stone Concrete Cement Contest

      Stone Concrete Cement Contest

    Comments