Here download the UF2 file , which is USB Flashing Format by Microsoft.
UF2 files can be opened and edited using a variety of Microsoft MakeCode editors.
To transfer a UF2 file to a microcontroller, developers plug their microcontroller into their PC’s USB drive. Their PC then recognizes the microcontroller as a FLASH DRIVE, allowing the developer to drag their UF2 file to the microcontroller and install their MakeCode program.
Drag and Drop the UF2 file on to RPI-RP2.
RPI-RP2 now disappears indicating Micropython installation is complete.
If you open the Device Manager , PICO appears as USB SERIAL DEVICE and a COM port is allotted.
ULTRASONIC sensor is popular in many applications where measuring distance or sensing objects are required.
The module has two eyes in the front which forms the Ultrasonic transmitter and Receiver.
1.Vcc The Vcc pin powers the sensor, typically with +5V,The current consumed by the sensor is less than 15mA
2.Trigger
Trigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending Ultrasonic burst of 40KHz.
3.Echo
Echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back from object to the sensor.
4.Ground
To start the measurement, the trigger pin has to be made high for 10uS and then turned off.
This action will trigger an ultrasonic wave at frequency of 40Hz from the transmitter and the receiver will wait for the wave to return.
Once the wave is returned after it getting reflected by any object the Echo pin goes high for a particular amount of time which will be equal to the time taken for the wave to return back to the sensor.
The amount of time during which the Echo pin stays high is measured by the esp32 as it gives the information about the time taken for the wave to return back to the Sensor. Using this information the distance is measured
As the ultrasonic sensor requires 5v , we use a separate power source.
This sensor will not operate on 3.3v.
Vcc –> 5v
Gnd to Gnd
Trigger –> GPIO 5
Echo –> GPIO 18
Both Gnd of 3.3v & that of 5v to be made common.
As the ECHO PIN of ultrasonic gives 5v level , we need a LEVEL SHIFTER in between ECHO & GPIO18
OLED connections :
Vcc to 3.3v
Gnd – Gnd
SCL –> GPIO 22
SDA –> GPIO 21
We use THONNY PYTHON to test and upload code.
2 library modules are required.
One ssd1306.py for OLED
hcsr04.py module for ultrasonic measurement support.
Pulse width modulation is used in a variety of applications particularly for control. It can be used for the dimming of LEDs, varying speed of motors and controlling the angle of servo motors
In ESP32 PWM can be initiated on all output-enabled pins.
PWM is digital, which means that it has two states: on and off.The amount of current that flows to a device like LED can be controlled by varying the time each pulse stays ON.
The longer each pulse is on, the brighter the LED will be.
DUTY CYCLE refers the amount of time the pulse is ON.
For a square wave the DUTY cycle is 50 % , the pulse is ON for 50% of the time and OFF for 50%
If duty cycle is 100% then the output is equal to Vcc ( 3.3v here) and LED will be full lit.
If duty cycle is 0% , the signal is flat and equal to Gnd voltage 0.
Connect an LED at GPIO 15.
Short lead of LED goes to Gnd through a Resistor 1k.
We shall use THONNY PYTHON IDE to test and upload python code on to ESP32.
Refer to my previous blog on how to use Thonny Python
Let us use the interpreter of Thonny Python to test the code.
First import from machine module the Pin & PWM classes.
Pin module is to control the GPIO pins.
Then create a PWM object called led and pass on the GPIO Pin 15.
Once the led object is created you can use the methods like duty to set the duty cycle and freq to set the frequency.
Duty cycle can be from 0 to 1023.
512 represents 50% , and 1023 = 100%. duty cycle.
led.freq(1) makes the LED flash once per second.
You can create the PWM object and pass on the duty,freq in a single line of code.
To stop PWM use the method deinit().
Now let us give the LED a fade effect using PWM.
Micropython executes boot.py file on power on.
Then it looks out for main.py file to execute.
In case you need your own naming for file , then create that file and import the module and place a call for the function inside main.py.
Though boot.py file has nothing to execute inside , this file exists by default in Micropython.
Inside main.py file
import fadeled
fadeled.ledpwm()
fadeled is the module we have to create
& ledpwm() is the function in that module.
Without creating this new module we could have named the file as main.py and write the complete code inside main.py
In the never ending loop while True: , we change the duty cycle from 0 to 1023
using in range function.
This gives a fading effect on LED.
Note the indentation after each colon : in python code.
This space or indentation represents braces.
Click on File –> Save as
and select Micropython device to save.
Save the file as fadeled.py
Like this save the main.py file also to ESP32.
Now click on F5 or the green arrow button on top to execute.
You can see the LED fading effect.
The PWM method can be used to position the SERVO motor from 0 to 180 degrees.
All servo motors have 3 wires.Red to 5v, Black or Brown to Gnd
Orange wire is Signal (pwm)
Connect the Servo signal pin at GPIO 23.
As servo requires 5V we use an external power source of 5 Volt.
The Gnd of 5V source and the Gnd of ESP 32 must be made common i.e connected together.This is a must while using 2 different power sources.
Frequency (1/period) is specific to controlling a servo.
A typical servo motor expects to be updated every 20 ms with a pulse between 1 ms and 2 ms, or in other words, between a 5 and 10% duty cycle on a 50 Hz waveform.
So frequency of 50 hz is a must to control most Servos .This gives a 20 msec pulse.
With a 1.5 ms pulse, the servo motor will be at the natural 90 degree position. With a 1 ms pulse, the servo will be at the 0 degree position, and with a 2 ms pulse, the servo will be at 180 degrees.
Duty cycle from 20 to 120 facilitates position of servo fro 0 to 180 degrees.
servo object is created by passing on PWM Pin as GPIO 23 and frequency as 50
1/50hz = 0.02 = 20 msec which is required to update most servos.
We set the duty cycle to 20 to position servo at 9 degree.
Normally in a Home Network we have a Router which is connected to Internet Service Provider.
This Router can be Ethernet capable, wireless capable or both.Nowadays mostly Routers are WIFI enabled.Over WIFI you can connect your laptop,mobile or ESP32 to the Router to get internet access.
In this scenario the ROUTER acts as ACCESS POINT & the devices that connect to it act as CLIENTS.
In Micropython CLIENT is also called as STATION.
When a Client/Station connects with the Router , the Router provides an IP address to the device connected.
Suppose the IP of Router is 192.168.0.1 which is also called as GATEWAY IP (this IP is printed on the back of Router) , the Clients will be given IP in the range 192.168.0.xxx .
The last octet only will change on first come first basis.That means the IP offered by DHCP method is DYNAMIC which changes on next power on.
To make it STATIC you can open the Gateway IP on a browser to set up configuration of Router.Here you can note the MAC address of the CLIENT and set the IP to static , so that it will be always offered the same IP on every power ON.
In the second scenario STATION INTERFACE
the ESP32 device can act as ACCESS POINT instead of Router & other devices like mobile or ESP modules can connect with it over WI FI.
In this post we shall explore the ways by which ESP32 acts as a CLIENT/STATION and connect with an ACCESS POINT like Router over WIFI automatically on Power ON.
For Micropython IDE we use THONNY PYTHON
Refer my previous post on getting started with Thonny python
This interface can be managed using the network module from MicroPython.
First import the network module.
Then create an object that represents this interface:
Create a WLAN network interface object. One of the Supported interfaces are
network.STA_IF (station or client, connects to upstream WiFi access points)
Make that network interface active.
Scan for the available wireless networks using
station.scan()
Scanning is only possible on STA interface. Returns list of tuples with the information about surrounding WiFi access points:
The tuples returned here have the parameters of each available Wi-Fi connection. In particular, the first element in each tuple is the SSID name, the third element is the channel number, and the fourth is the RSSI or signal strength indicator.
(ssid, bssid, channel, RSSI, authmode, hidden)
ssid is the service set identifier
bssid is hardware address of an access point, in binary form, returned as bytes object. You can use ubinascii.hexlify() to convert it to ASCII form.
RSSI signal strength in negative value
There are five values for authmode: 0- open ,1 –WEP , 2 –WPA-PSK , 3- WPA2-PSK ,4 WPA/WPA2-PSK
and two for hidden: 0 (false)-VISIBLE, 1- HIDDEN
You can check the connection using method isconnected()
As we have not yet connected it returns FALSE.
Connect to WI FI using your SSID name and its password.
ifconfig() method returns the parameters of the connection
IP Address allotted along with Subnet mask,Gateway IP & DNS Server
As we have tested the connection using Micropython Interpreter , we can implement auto connection to WIFI as follows :
Micropython executes
boot.py
on power up first.Then it looks out for
main.py
Under Thonny python IDE click File –> New
This file we shall save as boot.py
For intimation we shall include only a print statement
print(‘RUN : boot.py’)
Click on File –> Save As
You get 2 options 1. to store on your computer for later modification
2. On the Micropython device (ESP32) directly.
Provide the file name as boot.py and save it on MicroPython device.
Again File –> New
to create the second file main.py
Here we import module ConnectWifi & call the function connect()
The module & function have to be created by us.
For now save it on ESP32 as file main.py.
Remember not to change the file name, as Micropython will look to execute only main.py automatically.
Finally we have to create the file ConnectWifi.py
and create the function connect()
In this function replace the ssid,password with that of yours.
ESP32 is a powerful device for IOT development with WIFI & Bluetooth.Compared to NodeMCU it has more ADC pins , DAC & much more.
ESP32 can be programmed by many ways including Arduino core.
This post is on programming ESP32 using MICROPYTHON.
MicroPython is a lean and fast implementation of the Python 3 programming language that is optimised to run on a microcontroller.
MicroPython has advantages over traditional programming software such as C/C++ because it is essentially a live development environment and you write and debug code much faster…in fact 5-10 times faster than C++.
when a device boots up using MicroPython it does not consume much memory and thus doesn’t incur current drain.Micropython also has a number of Micropython specific libraries for accessing hardware level features.
Adafruit also introduced CircuitPython ,Soon after MicroPython started picking up the pace. However, CircuitPython offers support for the Adafruit range of hardware only.
Python allows for rapid feedback. This is because you can interactively enter commands and get a response using the REPL –read–eval–print loop. You could even tweak your code and run it right away, rather than iterating through code-compile-upload-execute cycles.
This post is on implementing MICROPYTHON on ESP32.
As a programming environment , many IDE s are available , of which THONNY IDE is simple & powerful.
Thonny IDE : An integrated development environment (IDE– Integrated Development Environment) for Python that is designed for beginners. It stands out for its simplicity of installation and use, ease of writing and debugging of code, possibility of incorporating plugins and its good documentation.
uPyCraft IDE : It is anIDE specifically oriented for MicroPython, which makes it one of the besteditorsfor this use. It allows you to upload the MicroPython firmware to the microcontroller in a very simple way (as an alternative toesptool.py)
Visual Studio Code (VSCode) , using the PyMakr extension: It is avery sophisticatedIDE , which supports many practical functionalities working with code, but is only recommended for advanced users
PYCHARM is for advanced users.There is community edition for free.There’s a PyCharm plugin that adds MicroPython support to your IDE. It’s available right in the plugin marketplace
visit https://thonny.org/ & download the installer relevant for you (available for Windows,Mac & Linux)
Install THONNY IDE by double clicking the downloaded installer.
Open THONNY IDE .
The top portion is the EDITOR where you write your Micropython code.
Bottom is the SHELL.
As we’ve not yet installed Micropython interpreter, the Shell displays the default Python prompt.
‘>>>’ is the prompt of the interactive Python interpreter, meaning that the interpreter is ready to get Python statements typed in.
The PROMPT Is called CHEVRONS.
To get the Micropython prompt, click on Tools –> Options
Under options window click on INTERPRETER.
Under first drop down menu select MICROPYTHON (ESP32)
Under Port , select the port number where ESP32 is connected.
Click on Firmware install bar.
r
Micropython firmware .bin file can be downloaded from
We can see this listing in Thonny using the function help(machine).
To control an LED on / off, the following must be done:
First, the Pin class of the machine module is imported , which allows controlling the inputs / outputs (General Purpose Input / Output – GPIO -) of the microcontroller – and therefore, of its corresponding pins on the board. Since pin 02 ( GPIO 02 ) is connected to the integrated LED, you also have control over it.
Then an object that we will call pin_02 is initialized to identify it, turning GPIO 02 “2” into an output pin “ Pin.OUT ”.
Among the classes of the module, the class
Pin()
is used to create objects to control inputs / outputs of the microcontroller ( GPIO ), which will help us to turn on /off the LED.
Additionally, if we ask for information about the classPin() we can see that it has 5 methods – Pin.init (), Pin.value (), Pin.off (), Pin.on () and Pin.irq () – :
In order to use this class , we need to know how your constructor and its methods work .
The constructor is the subroutine whose mission is to initialize the object . The constructor of the Pin class is:
class machine.Pin (id, mode, pull, *, alt)
The arguments of the constructor are:
The arguments to turn on the blue LED integrated in the ESP32 DEVKIT board will be the following:
id “ 2 ” since the on board LED is connected with pin 02 ( GPIO 02 ).
mode “ Pin.OUT ” to make pin 02 become a voltage output pin .
pull “ None ” to not configure pull-up or pull-down resistors (they are only useful in the case of being an input pin – Pin.IN -). It is not necessary to define this argument.
value “ 1 ” to turn on the LED-3.3V voltage output.
Once the code has been executed, we will have initialized an object , which has been assigned the name pin_02 , whose initial values convert pin 2 (GPIO 02) into an output pin and its status is on (3.3V voltage) .
The methods that we can use once the object is created allow us to manipulate (modify) its functionalities . In the Pin class we have the following:
Pin.init () : Modify theinitial arguments of the constructor . You can modify the mode , pull , * (value) and altargumentswhen new values are defined.
Pin.value () : allows you to read the presence / absence of input voltage of the pin (without an argument is provided) or set an output voltage on the pin providing the argument. The arguments can be “ 0 ” off (0.0V) and “ 1 ” on (3.3V). Booleans can be usedas arguments.
Pin.off () : Sets a pin output voltage of 0.0V- off.
Pin.on () : sets an output voltage of the 3.3V pin – on -.
Pin.irq () : enables external interruptions in the pin , as long as it is an input pin .
Put together
First from machine module import the class Pin.
Then assign the built in LED GPIO pin 2 & declare it as Pin.OUT
The pin that is connected to the led is instantiated & the second argument is set as OUT ,indicating
that the pin acts as OUTPUT.
t
Now led.value(1) makes the on board LED ON.
led.value ( 0) makes it OFF.
We have controlled the LED from Interpreter. Now we shall write the complete code in the EDITOR Section to blink the LED repeatedly.
Here we make use of the sleep class of time function to introduce delay.
Never ending loop is created using
while True:
Notice the loop starts by colon : & instead of braces micropython uses indentation.
Click File –> Save as
& select Micropython device
Provide the file name as main.py
You can notice , by default already boot.py is available on device.
Micropython will first execute boot.by & then looks out to execute file named main.py.
So you cannot upload file in other name to execute.If you use some other name , then the file will be executed once.After power reboot it will not execute.
Once the file is stored click on F5 or the Run green button.
The script is uploaded and the on board LED blinks.
To stop execution you can press the STOP Red button.
Now the chevrons >>> appear at the shell.
You can write new code & save it to device , again in the name main.py.