Python :
Micro python
Punit A. Lathiya
Assistant Professor
EC Department
GEC Rajkot
Syllabus : 3151108
Syllabus : 3151108
Teaching and Examination Scheme
Books
Online Courses
• https://www.coursera.org/learn/python-programming
• https://nptel.ac.in/courses/106/106/106106145/
• https://nptel.ac.in/courses/106/106/106106182/
Index
• Introduction
• Difference between python and micropython
• Hardware Overview
• Installation of bootloader
• Software – Esplorer and Thonny
• GPIO Programming
• ADC Programming
• IOT Related Experiments
Introduction
• MicroPython is a software implementation of
a programming language largely compatible with Python 3,
written in C, that is optimized to run on a microcontroller.
• MicroPython is tiny open source Python programming
language interpreter designed to run on small embedded
devices.
• MicroPython does not support entire python standard
library so we can call it as sub-set of python.
Introduction
• Advantages:
▫ Small foot print (Requires less memory)
▫ It can run on 256 KB ROM and 16K RAM) so suitable for
embedded systems
▫ Near to hardware due to Hardware API
▫ Clean and simple Python code to control hardware
• Disadvantages:
▫ Limited standard library functions
▫ Execution speed less than C so sometimes it may create problem
for real time signal processing where execution speed requirement
is critical
NodeMCU
• WiFi support 802.11 b/g/n, 2.4 GHz, WPA/WPA2
• It has one analog input (10 bit integrated ADC)
• 13 Digital I/O pins (Alternate functions like
USART/I2C/SPI)
• USB – micro USB port for power, programming
and debugging
• Micropython & Arduino Programming support
NodeMCU
Configuring NodeMCU
• Install Python 2.7.13 or any other latest version
• Open command prompt (cmd)
• Install esptool
(If not installed then) Command: >pip2 install esptool
• To erase esp8266:
• >esptool.py –port COM6 erase_flash
• Download firmware in esp8266 for micropython
> esptool.py --port COM6 --baud 460800 write_flash --
flash_size=32m 0 esp8266-20170823-v1.9.2.bin
• In above command give path of binary file for the firmware if it is not
there in the same directory, use flash_size=detect if ROM size is
unknown
ESPlorer
• The ESPlorer is an IDE(Integrated Development Environment) for ESP
developers
• With ESPLorer, we can write MicroPython code and upload to
NodeMCU board
• Download link: http://esp8266.ru/esplorer-latest/?f=ESPlorer.zip
• To run ESPlorer, need to have JAVA installed. If JAVA is not installed,
install it from website.
• http://java.com/download
• Connect NodeMCU with LAPTOP/PC, run ESPLorer and set baud rate
115200
ESPlorer
Thonny
• Thonny Micropython IDE can be downloaded from
https://thonny.org/
• It provides editor to write Micropython program
• We can also erase and program firmware to board using it
from Tools→Options→Interpreter Menu
• It also provides micropython prompt where we can issue
commands which run directly on the board
• Thonny IDE provides different view options
• Program can be saved in host computer/laptop or in
Micropython board.
Thonny
MicroPython Code-1: Blink LED
import machine
import time
LED = machine.Pin(2,machine.Pin.OUT) # GPIO2 = D4
#do infinitely
while True:
LED.on();
Time.sleep_ms(500)
LED.off();
Time.sleep_ms(500)
MicroPython Code-2: Switch-LED
from machine import Pin
import time
LED=Pin(2,Pin.OUT)
sw=Pin(5, Pin.IN, Pin.PULL_UP) #Switch is connected at GPIO5
#do infinitely
while True:
status = sw.value()
if status == 0:
LED.off()
else:
LED.on()#print the value of sensor value on serial monitor
time.sleep(1) #provide time delay of 1 second
MicroPython Code-2: Analog Input
import machine
import time
LED= machine.Pin(2, Pin.OUT)
LED.off( )
#do infinitely
while True:
Sensor = machine.ADC(0) # Potentiometer is connected at ADC pin
data = Sensor.read();
print(data);
LED.value(not LED.value())
time.sleep(1);
Thank You