blink.py

Hardware platform: ESP8266 and ESP32

Hardware connection: No need for peripheral.

Cautions: CLK (IO6), SD0 (IO7), SD1 (IO8), CMD (IO11) of ESP32 is not supported temporarily.

Code location: Examples=>Basic=>blink.py

  1. import time
    from machine import Pin
    led=Pin(2,Pin.OUT)
    
    while True:
      led.value(1)
      time.sleep(0.5)
      led.value(0)
      time.sleep(0.5)
    

Result: on-board LED light blinks.

timerBlink.py

Hardware platform: ESP8266

Hardware connection: no need for peripheral.

Code location: Examples=>Basic=> timerBlink.py

#hardware platform: FireBeetle-ESP8266



from machine import Pin,Timer
led=Pin(2,Pin.OUT)

tim = Timer(1)


def blink(t):
  led.value(not led.value())


tim.init(period=1000,mode=Timer.PERIODIC, callback=blink)

try:
  while True:
    pass
except:

  tim.deinit()

Result: on-board LED light blinks.

breathLight.py

Hardware platform: ESP8266

Hardware connection: no need for peripheral.

Code location: Examples=>Basic=> breathLight.py

#hardware platform: FireBeetle-ESP8266

from machine import Pin,Timer,PWM
pwm =PWM(Pin(2),100)
polar = 0
duty = 0
def setLed(t):
  global duty,polar
  if(polar == 0):
    duty+=16
    if(duty >= 1008):
      polar = 1
  else:
    duty -= 16
    if(duty <= 0):
      polar = 0
  pwm.duty(duty)
tim = Timer(1)
tim.init(period=10,mode=Timer.PERIODIC, callback=setLed)
try:
  while True:
    pass
except:
  tim.deinit()
  pwm.deinit()

digitalRead.py

Hardware platform: ESP8266 and ESP32

Hardware connection: connects D2 to LED Light; connects D4 to Button. (The two platforms connect hardware in a same way.)

Hardware connection image, shown as below:

Code location: Examples=>Basic=> digitalRead.py

ESP32 code:

#hardware Platform:FireBeetle-ESP32

from machine import Pin
import time

button=Pin(27,Pin.IN)
led=Pin(25,Pin.OUT)

while True:
  led.value(button.value())
  time.sleep(0.1)

ESP8266 code:

#hawdware Platform :FireBeetle-ESP8266

from machine import Pin
import time

button=Pin(15,Pin.IN)
led=Pin(13,Pin.OUT)

while True:
  led.value(button.value())
  time.sleep(0.1)

Result: press/ release bottom, LED light turns on/turns off.

analogRead.py

ESP32 code:

#hawdware Platform :FireBeetle-ESP32


from machine import ADC,Pin
import time

adc0=ADC(Pin(36))
adc1=ADC(Pin(39))
adc2=ADC(Pin(34))
adc3=ADC(Pin(35))

while True:
  print("adc0=",adc0.read())
  print("adc1=",adc1.read())
  print("adc2=",adc2.read())
  print("adc3=",adc3.read())
  time.sleep(1)

ESP8266 code:

#hardware platform: FireBeetle-ESP8266

from machine import ADC
import time

adc0=ADC(0)

while True:
  print("adc0=",adc0.read())
  time.sleep(1)

Result: all analog data been read printed to the terminal.

IO interruption (irq.py)

Hardware platform: ESP8266 and ESP32

Hardware connection: connect D2 to LED Light; connect D4 to Button. (The two platforms connect hardware in a same way.)

Hardware connection image, shown as below:

Code location: Examples=>Basic=> irq.py

ESP32 code:

#hardware platform: FireBeetle-ESP32

from machine import Pin
import time

value=1
counter=0
def func(v):
  global value,counter
  time.sleep_ms(50)
  if(button.value() == 0):
    return
  while(button.value() == 1):
    time.sleep_ms(100)
  time.sleep_ms(100)
  counter+=1
  led.value(value)
  value = 0 if value else 1
  print("IRQ ",counter)

led = Pin(25, Pin.OUT)
led.value(0)
button = Pin(27, Pin.IN)
button.irq(trigger=Pin.IRQ_RISING, handler=func)
while True:
  pass

ESP8266 code:

#hardware platform: FireBeetle-ESP8266

from machine import Pin
import time

value=1
counter=0
def func(v):
  global value,counter
  time.sleep_ms(50)
  if(button.value() == 0):
    return
  while(button.value() == 1):
    time.sleep_ms(100)
  time.sleep_ms(100)
  counter+=1
  led.value(value)
  value = 0 if value else 1
  print("IRQ ",counter)

led = Pin(13, Pin.OUT)
led.value(0)
button = Pin(15, Pin.IN)
button.irq(trigger=Pin.IRQ_RISING, handler=func)
while True:
  pass

Result: activate on the rising edge, the switch of LED light changes each time when you press the bottom, accompanying with blinking effect.

DAC output wave (waveform.py)

Hardware platform: ESP32

Hardware connection: connect GPIO25 and GPIO26 to an oscilloscope.

Code location: Examples=>Basic=> waveform.py

#hardware platform: FireBeetle-ESP32
#GPIO25 ouput sine wave, GPIO26 output triangle wave

from machine import DAC,Pin
import math
import time

dac0=DAC(Pin(25))
dac1=DAC(Pin(26))
a=0
while True:
  value=math.sin(a*math.pi/180)
  dac0.write(int(100+value*100))
  dac1.write(a*255//360)
  a+=1
  if(a==361):
    a=0
  time.sleep(0.0001)

Analog output (analogWrite.py)

Please refer to waveform.py

Low-power consumption (lowpower.py)

Hardware platform: ESP8266

Hardware connection: no need for peripheral.

Code location: Examples=>Basic=> lowpower.py

#hardware platform: FireBeetle-ESP8266

import machine

rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

if machine.reset_cause() == machine.DEEPSLEEP_RESET:
   print('woke from a deep sleep')

rtc.alarm(rtc.ALARM0, 3000)

machine.deepsleep()

Result: run the code and ESP8266 enters low-power consuming mode. Wake up in 3 seconds, running the code again, message about the reason of resetting is printed: woke from deep sleep.

Caution: the chip resets once wake from deep-sleep.

results matching ""

    No results matching ""