程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Developing esp32 with micropython - writing programs with thonny

編輯:Python

Chen Tuo 2022/06/11-2022/06/12

1. brief introduction

stay 《 use MicroPython Development ESP32- Firmware burning and testing 》

https://zhuanlan.zhihu.com/p/527291091

https://blog.csdn.net/chentuo2000/article/details/125231902?spm=1001.2014.3001.5501

In this article, we have been in ESP32 It's finished MicroPython The firmware , And with the help of the serial port debugging assistant, I did some simple tests with the command , For further use MicroPython Conduct ESP32 Development also requires a convenient development tool .

Thonny It is suitable for beginners Python IDE (Python Integrated development tools ) Programming tools .

Let's talk about using micropython + Thonny Conduct ESP32 Development .

2. Connect PC and ESP32 Development board

use USB Switch to serial connection PC and ESP32 Development board , View port number :

 

The serial port number is COM3.

3. Download installation settings Thonny

  • Thonny The official website of

https://thonny.org/

 

  • download

We are Win10 Development under the system , choice Windows:

 

The current version is 3.3.13.

  • install

double-click thonny-3.3.13.exe

Default installation all the way .

  • Setup language

function Thonny, Choice language

 

We choose simplified Chinese .

Let’s go!

Get into Thonny Interface

 

  • Set up MicroPython Decoder and serial port

Thonny We choose to support multiple chips ESP32.

Tools > Set up > decoder

choice MicroPython(ESP32), And select serial port number :

 

confirm

Shell Window response :

 

thus Thonny It's too late to install .

4. test

Shell Windows are used for human-computer interaction , We can enter commands here , control ESP32.

  • Command interaction

Enter the command print('Hello World'), enter :

 

ESP32 Respond to Hello World

  • On board lighting LED

There is a blue one on the development board LED The lamp is connected to the pin 2 On .

Enter the command :

import machine # Import machine modular

pin2 = machine.Pin(2, machine.Pin.OUT) # use machine Modular pin Function setting pin 2 For export .

pin2.value(1) # Insert the pin 2 Set to high level

You can see the blue LED The light is on .

Enter the command :

pin2.value(0) # Insert the pin 2 Set to low level

You can turn off the blue LED The lamp .

 

explain :

1) stay Python In the syntax # Symbols are annotators , Used to describe the function of the program ,# The part after the number is not executed .

2) High level , Low level . In digital logic circuits , Low level means 0, High level means 1. When the pin output is high , The voltage on the pin is the supply voltage of the chip , It's here LED Voltage is applied at both ends ,LED There is an electric current flowing through , The light goes on ; When the pin output is low , The voltage on the pin 0, Add to LED The voltage at both ends disappears , Then the light goes out ,

5. Write py Program files

Command interaction can only do some simple operations , For the programming work of replication, we need to write Python Program .

Computer languages are divided into compiled languages and interpreted languages . Compiled languages are common C、C++, Explanatory languages are common Java、Python.

Programs written in interpretive languages are often called script programs , therefore Python The program is called Python Script program .Python The extension of the script file is .py.

5.1 first Python Script program

We turn on and off LED The command of the lamp is converted into py Program files .

stay Shell Above the command window is the program editing window , We write here Python Script program .

  • Write program

We write the following program :

import machine
import utime
pin2 = machine.Pin(2, machine.Pin.OUT)
pin2.value(1)
utime.sleep(3)
pin2.value(0)

 

The difference from the command mode is that we add delay , The delay function is in the module utime in , Use the delay function , First import utime modular import utime

utime.sleep(3) Time delay 3 second .

  • Run the test

 

Click the run button , The program save location window pops up :

 

Select save program to MicroPython equipment .

 

There is already a program boot.py, This is the system startup program written in the previous section .

Give our program a name led_on_off.py.

confirm .

You can see LED The light is on 3 It turns off automatically after seconds .

stay Shell There is a prompt for program running in the window

 

Because the program has been written ESP32, You can run it many times in the future

5.2 Program reuse

The saved program files can be used repeatedly .

Select... From the menu next time you start the machine : file > open

 

choice MicroPython equipment :

 

confirm , You can open the file again led_on_off.py.

 

6. Let the program in ESP32 It runs automatically when powered on

The above program runs under the control of the development environment , As a product program, it needs to be separated from the development environment , Power on for automatic operation .

This is easy to do , We just put the program name led_on_off.py Change it to main.py That's all right. .

main.py It is the starting point of the user program , When powered on, the system will automatically find main.py, And load and run .

  • take led_on_off.py Save as main.py

file > Save as

 

choice MicroPython equipment :

 

confirm .

Now press... On the development board RST key , Or power on again , The program will run automatically .

  • Delete led_on_off.py

Right click in the upper window led_on_off.py

 

Then click delete .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved