为什么使用 Micropython编进行变成呢,在我初步使用后有一下的体悟:
1.操作简单,python语法。arduino框架编译速度贼慢,即使用了platform平台还是很慢,而乐鑫提供的开发环境部署开发都有一点麻烦。
2.有命令行模式,也就是可以实时的进行调试,可以一行一行的执行代码,esp32好像没有像stm32那样强大的调试功能,而这个命令行模式对于简单程序来说调试还是很舒服的。
当然问题也不少
1.资源比较少,很难找到一些库文件,而且出现问题也不好解决,但目前这一块好起来了。
2.占用硬件资源,但是对我一个diy爱好者来说,能用就行,目前没感受到因为这个语言而出现的资源方面问题。
而且随着技术的发展,和python的爆火,Micropython一定会被越来越多的人接收使用。
Ok, let's start learning Micropython next (because I am recording while studying, so there will definitely be some mistakes, everyone is welcome to correct me, let's learn and progress together)
Step 1: Micropython programming environment setup
1. Thonny installation
一个python软件,但是支持micropython,
Download address: Thonny
chooses the corresponding version, all the way next is OK
2. Flash the micropython firmware for your esp32
Download address: Micropython
Select your corresponding hardware type, download the firmware, and then connect your board to the computer.
1. Click to select the interpreter
2. Select the interpreter is ESP32, and select the port, click install or update below
3. Add the firmware you just downloaded in Firmware, click install and wait for ok
to pull this step, your esp32 hardware is ready The micropython language is already supported, let's test it with a light.
The second step of the old actor - lighting
- If you have a little python foundation and a little hardware foundation, you should be able to understand what the code means, without explaining it.
- When we first came into contact with micropython, we were not very clear about some of its functions, and the compiler did not prompt, so it was very uncomfortable. At this time, it is very important to use the micropython development manual. You can find it on the official website of micropython. 】
# IO2对应led
import machine
import time
pin2 = machine.Pin(2, machine.Pin.OUT)
while True:
pin2.value(1)
time.sleep_ms(500)
pin2.value(0)
time.sleep_ms(500)
Pay attention to change it to your hardware led, but esp32 is generally IO2, take a look at your schematic diagram.
pwm output: realize the breathing light, the code is as follows
from machine import Pin, PWM
import time
pwm0 = PWM(Pin(2)) # create PWM object from a pin
pwm0.freq(1000) # set frequency
while True:
for i in range(0,1024,1):
pwm0.duty(i) # 0-1023
time.sleep_ms(1)
for i in range(1023,-1,-1):
pwm0.duty(i) # 0-1023
time.sleep_ms(1)
If you are just getting started, try the code above, and you will have a general understanding of micropython. This is the end of this article. Welcome to communicate~~~