Using MicroPython is a great way to get the most of your MicroPython ESP8266 D1R2 board. And vice versa, the MicroPython ESP8266 D1R2 board is a great platform for using MicroPython. With this board you can learn up MicroPython, getting a prompt, using WebREPL, connecting to the network and communicating with the Internet, using the hardware peripherals, and controlling some external components.
The MicroPython ESP8266 D1R2 connects to your PC over USB, giving you a USB flash drive on which to save your Python scripts. MicroPython ESP8266 D1R2 is equipped with a micro USB connector for power and serial communications, 17 GPIO, D13 LED, and reset button.
MicroPython is a complete rewrite of the Python (version 3.4) programming language so that it fits and runs on a microcontroller. It includes many optimizations so that it runs efficiently and uses very little RAM. Additionally, MicroPython runs bare-metal on the board and essentially gives you a Python operating system. The built-in MicroPython ESP8266 D1R2 module contains functions and classes to control the peripherals available on the board, such as UART, I2C, SPI, ADC.
For additional thinks, you can to add to order, a USB cable and breadboard.
Microsoft MakeCode is a free, open-source platform for creating engaging computer science learning experiences that support a progression path into real-world programming.
Connect your MicroPython ESP8266 D1R2 to your PC (Windows, Mac or Linux) with a micro USB cable. There is only one way that the cable will connect, so you can’t get it wrong.
Your PC should now recognise the board. It depends on the type of PC you have as to what happens next:
- Windows: Your MicroPython ESP8266 D1R2 will appear as a removable USB flash drive. Windows may automatically pop-up a window, or you may need to go there using Explorer.
- Windows will also see that the board has a serial device, and it will try to automatically configure this device. If it does, cancel the process. We will get the serial device working in the next tutorial.
- Mac: Your MicroPython ESP8266 D1R2 will appear on the desktop as a removable disc. It will probably be called BFLASH. Click on it to open the board folder.
- Linux: Your MicroPython ESP8266 D1R2 will appear as a removable medium. On Ubuntu it will mount automatically and pop-up a window with the board folder. On other Linux distributions, the board may be mounted automatically, or you may need to do it manually. At a terminal command line, type lsblk to see a list of connected drives, and then mount /dev/sdb1 (replace sdb1 with the appropriate device). You may need to be root to do this.
Okay, so you should now have the board connected as a USB flash drive, and a window (or command line) should be showing the files on the MicroPython ESP8266 D1R2 drive.
The drive you are looking at is known as /flash by the board, and should contain the following 4 files:
main.pyfile in a text editor. On Windows, you can use notepad or any other editor. On Mac and Linux, use your favorite text editor. With the file open you will see it contains 1 line:
# main.py -- put your code here!
This line starts with a # character, which means that it is a comment. Such lines will not do anything and are there for you to write notes about your program.
Let’s add 2 lines to this
main.py file, to make it look like this:
# main.py -- put your code here! import pyb pyb.LED(4).on()
The first line we wrote says that we want to use the
pyb module. This module contains all the functions and classes to control the features of the MicroPython ESP8266 D1R2.
The second line that we wrote turns the blue LED on: it first gets the
LED class from the
pyb module, creates LED number 4 (the blue LED), and then turns it on.
To run this little script, you need to first save and close the main.py file, and then eject (or unmount) the board USB drive. Do this like you would a normal USB flash drive.
When the drive is safely ejected/unmounted you can get to the fun part: press the RST switch on the board to reset and run your script. The RST switch is the small black button just below the USB connector on the board, on the right edge.
When you press RST the green LED will flash quickly, and then the blue LED should turn on and stay on.
Congratulations! You have written and run your very first MicroPython program!