2016. december 1., csütörtök

ESP speed compare in LUA, MicroPython, Arduino

ESP8266 frequency generator



I started an ESP8266 square wave frequency doubler project for my Air Conditioner modification.
The idea is to reach frequency doubling with triggering new square for upper and lower edges.
But this is the far target.  :)


But what is the ESP01 pure performance ? 


1. with LUA


ESP family CPU speed is 80MHz as default, but you can switch to 160Mhz which sound good but reality is disappointing output frequency is somewhere 6khz.

So lets start check what is max frequency generating LUA language:

     
     

node.setcpufreq(node.CPU160MHZ)
 gpio.mode(4, gpio.OUTPUT)

 tmr.wdclr()
 for count = 1,102400 do
     gpio.write(4,1)
     gpio.write(4,0)  
 end




Conclusion: MAX frequency is ~6,6 KHz with LUA.

This is the theoretical max frequency with LUA. 
Each new code line will slow down the out frequency.

For example if you make a longer sound loop the loop detection in NodeMCU will stop your loop after 5 sec. (as a workaround you have to reset the inner watchdog. but in this case freq. slow down.

 node.setcpufreq(node.CPU160MHZ)  gpio.mode(4, gpio.OUTPUT)
 tmr.wdclr()
 for count = 1,102400 do
     gpio.write(4,1)
     gpio.write(4,0)  
 end 


So we have to use another language because LUA script language is slow for this.

Lets check micropython or Arduino IDE(C++)


2. with Micropython


Speed improvement  almost double, but similar league in performance as LUA.



import pyb
import time
pin = pyb.Pin(2, pyb.Pin.OUT)
for i in range(4):
    print('LED ON2')
    pin.value(0)
    time.sleep(1)
    print('LED OFF2')
    pin.value(1)
    time.sleep(1)
    print('2iteration done.')
print("All 2 done.")



3. Arduino IDE(C++)


Best performance -as expected. :)

125 KHz !!!! with 160 MHz CPU.

Which is 20 times faster than LUA or 10 times faster than MicroPython.


#define ESP8266_LED 2
void setup()
{
   pinMode(ESP8266_LED, OUTPUT);
}

void loop()
{
  digitalWrite(ESP8266_LED, HIGH);
  delayMicroseconds(3);
  digitalWrite(ESP8266_LED, LOW);
  delayMicroseconds(1);

}











1 megjegyzés: