ESP32-Coding-With-MicroPython

From ArduinoInfo
Jump to navigation Jump to search

PROGRAM THE ESP32 WITH MicroPython

NOTE: The ESP32 can also be programmed with the Arduino IDE. See our page HERE

LINKS

Learn Turtle Graphics SEE THIS PAGE

What is MicroPython?

MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. It is written (usually once) to flash memory on the ESP32, and then Python programs can be written in an editor on PC or MAC or Linux and downloaded to the ESP32 where they are run using the Python interpreter which was already loaded on the ESP32.

Micro Python makes it extremely easy and simple to program digital electronics. Micro Python’s goal is to make programming digital electronics as simple as possible, so it can be used by anyone. Currently, Micro Python is used by hobbyists, researchers, teachers, educators, and even in commercial products.

The classic BLINK program looks like this:

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.value(1)  # turn LED on
    sleep(0.5)
    led.value(0)  # turn LED off
    sleep(0.5)
 

How do we program ESP32 with MicroPython?

MuEditorModes.jpg

There are several possible ways but we find the easiest and best is the Mu Editor. Mu is a simple code editor for beginner programmers based on extensive feedback from teachers and learners.

Mu is a modal editor with modes for many different ways to use Python to create cool and interesting things. See the mode list on the right.

We will use the first two modes: ESP MicroPython and Python 3

  • Python 3 : We will use this to try out many of the Python features we may have learned using on online editor, or to do external lessons (like Turtle Graphics) with Python.
  • ESP MicroPython : This mode has many added capabilities for control of hardware on the ESP32 (or ESP8266) Microcomputers. It can talk to an ESP32 over USB cable and download code that will run on the Microcomputer and control or read many many hardware devices like switches, LEDs, Servomotors, Potentiometers and other sensors. It can also create web pages that appear over WIFI so that you can control devices from your phone or computer.

We will show several examples of using both of these modes.

DOWNLOAD and INSTALL the mu editor

We suggest you use the mu editor installation instructions below:

How to install Mu on Windows with the Official Installer: (CLICK HERE)

How to install Mu on macOS with the Official Installer: (CLICK HERE)

When you have mu editor installed come back here!

QUICK and DIRTY FIRST CODE

This is just for an easy first test on the mu-editor. Switch the Mode to PYTHON3. Click NEW and copy the code example below and paste into the editor window. Save (Name it) and RUN

PYTHON 3:

NOTE: the "#" is used to show a COMMENT that is not part of the code.

# Firstly, we are going to draw a simple shape (a circle) using the turtle module.
# ● Import the turtle module.
import turtle

# ● Make a turtle, give it a name,  and load it into a variable.
tina = turtle.Turtle()
# We have named the turtle Tina, however, you can give the turtle any name you want.
# ● Set the colour that the turtle will use to draw the shape.
tina.color("blue")
# ● Set the style of the turtle with a function called shape.
tina.shape("turtle")
# ● Set the turtle’s speed, choosing a number between 1 and 100 (100 is the fastest).
tina.speed(10)
# ● Set the thickness of the line your turtle will draw.
tina.pensize(4)
# ● Now, tell your turtle to draw a circle.
tina.circle(60)  # draws circle with radius of 60 pixels# Write your code here :-)
# Now draw the same circle, but fill it in
tina.fillcolor("red")
tina.begin_fill()
tina.circle(60)
tina.end_fill()

Many more examples and links to come! See THIS PAGE For how to learn Turtle Graphics.

ESP MicroPython:

First, you need to FLASH (Download) the semi-permanent "Firmware" onto your ESP32. This installs the MicroPython support and MicroPython interpreter in the flash memory on your ESP32 where it will stay "firmly" even after you turn the ESP32 off.

To "Flash the MicroPython Firmware" go to THIS PAGE

OK, when you get back, let's use the MuEditor to write a little Python code and download it to your ESP32.

There are some early examples HERE