ESP32-MicroPythonCodingExamples1
Jump to navigation
Jump to search
MicroPython Coding Examples V1
Some early / easy examples
Copy and paste to a new Mu Editor page
ESP32 Starter Template
You can use this to start a new MicroPython program. Change the IMPORTS as needed, and get rid of the Blink example.
# ESP32INFO.INFO Starter Examples # See: http://Esp32Info.Info # hardware platform: ESP32 DevKit # Result: # - CONNECTIONS: # - # - # - V1.00 01/26/2022 # Questions: terry@yourduino.com */ # /*-----( Import needed libraries )-----*/ from machine import Pin from time import sleep # /*-----( Declare Variables )-----*/ # /*-----( Declare Variables that define hardware )-----*/ led = Pin(23, Pin.OUT) # create LED object from pin32,Set Pin32 to output while True: # (FOREVER here ) led.on # turn LED on sleep(0.5) # 1/2 second led.off # turn LED off sleep(0.5)
BLINK Easy
# hardware platform: ESP-32 # Result: Blinking LED # Questions? terry@yourduino.com from time import sleep # Import needed libraries from machine import Pin led = Pin(23, Pin.OUT) # create LED object from pin23,Set Pin23 to output while True: led.on # turn LED on sleep(0.5) led.off # turn LED off sleep(0.5)
BLINK Morse Code "HI"
# hardware platform: ESP32 DevKit # Result: Blink Morse code "HI" # The information below shows blink can not use these pins: # IO0 IO4 IO10 IO12~19 IO21~23 IO25~27 # /*-----( Import needed libraries )-----*/ from time import sleep from machine import Pin # /*-----( Declare Variables )-----*/ # /*-----( Declare Variables that define hardware )-----*/ led = Pin(23, Pin.OUT) # create LED object from pin23,Set Pin23 to output n = 0 while True: # Sort of like LOOP in Arduino i = 1 while i <= 4: led.value(1) # Set led turn on sleep(0.1) led.value(0) # Set led turn off sleep(0.2) i = i + 1 # update counter sleep(0.2) i = 1 while i <= 2: led.value(1) # Set led turn on sleep(0.1) led.value(0) # Set led turn off sleep(0.2) i = i + 1 # update counter n = n + 1 print("N =", n) sleep(1.0)
PWM_Pin23_Test2
# ESP32INFO.INFO Starter Examples # See: http://Esp32Info.Info # hardware platform: ESP32 DevKit # Result: # - CONNECTIONS: # - Pin 32 to LED and Resistor (220 ohms good) # - # - V1.00 01/26/2022 # Questions: terry@yourduino.com */ # /*-----( Import needed libraries )-----*/ from machine import Pin, PWM from time import sleep # /*-----( Declare Variables )-----*/ frequency = 5000 # /*-----( Declare Variables that define hardware )-----*/ led = PWM(Pin(23), frequency) while True: # (FOREVER) duty_cycle = 0 while duty_cycle <= 1022: led.duty(duty_cycle) duty_cycle += 1 sleep(0.005) while duty_cycle >= 0: led.duty(duty_cycle) duty_cycle -= 1 sleep(0.005) sleep(2.0)