An Overview of MicroPython
Tags: iot, micropython, nodemcu, esp8266
Published: 2017-04-26 14:00:10

---
title: An Overview of MicroPython
published: true
description: Well, for many micro-controllers thereÂ is a Python option which is, arguably easier to learn in many ways than Java, JavaScript, C or C++.
tags: IoT, MicroPython, NodeMCU, ESP8266
---

There are a lot of options available when one starts exploring the exciting field of the Internet of Things, or IoT. Not only are there many choices of <a href="https://medium.freecodecamp.com/hardware-fundamentals-what-exactly-is-a-microcontroller-8a502a3650dc">micro-controllers</a>, there are also a variety of languages in which to program these devices. Many times the micro-controllers are sold as being dependent on C or C++ as a language, which is great, but I don't know either of those. There is some great and amazing working being ledÂ by <a href="https://www.twitter.com/chalkers">Andrew Chalkley</a> around using the <a href="https://www.javascript.com/">JavaScript</a> language for micro-controllersÂ over at <a href="http://thingssdk.com/">thingsSDK</a> and if JavaScript is your language, I'd highly recommend checking out their work. There are also lots of resources for using Java for IoT, <a href="ttp://twitter.com/nathantippy">Nathan Tippy</a>, for example is an excellent resource.

All those are great languages, but what about if you are a beginning developer just interested in playing around in IoT? Or what if you are a Pythonista and don't want to pick up another programming language? Well, for many micro-controllers thereÂ <strong>is</strong> a Python option which is, arguably easier to learn in many ways than Java, JavaScript, C or C++. That option being <a href="http://micropython.org/">MicroPython</a>.

MicroPython is a tiny, open source Python programming language interpreter than runs on embedded boards. It was originally developed in 2013 and released in 2014 by Dr. Damian P. George of Cambridge University. It implements <a href="http://www.python.org">Python 3</a> and is designed specifically to run on devices with limited resources.

As with many things in the IoT world, the use of a particular language is greatly influenced by the tools, libraries, and packages one has available. MicroPython comes with an interactive read-evaluate-print-loop (REPL) for quick prototyping and also fully supports loading MicroPython scripts onto a device to run as well. It supports an extensive Python based library and for lower level operations can be extended with C/C++ functions as needed.

Many of the tasks that beginning IoT folks start off with, such as blinking lights, reading switches, driving servos, etc. are all easy to do in MicroPython. Further, it is often incredibly easy to read as well as it is all Python based. For example, if one has MicroPython installed on a <a href="https://www.amazon.com/gp/product/B01N0B48NI/ref=as_li_tl?ie=UTF8&amp;tag=kenwalgersite-20&amp;camp=1789&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=B01N0B48NI&amp;linkId=dc1e9f82e2beb42bca36616a22b8820d">NodeMCU ESP8266</a> board, a popular and inexpensive board to start working in IoT, with a basic LED light connected to <a href="https://iotbytes.wordpress.com/nodemcu-pinout/">GPIO Pin 5</a>Â it is pretty simple to get the light to turn on (high state) and off (low state).
<pre>import machine

led = machine.Pin(5, machine.Pin.OUT)
# Turn on LED
led.high()
# Turn off LED
led.low()
</pre>
The machineÂ module is included with MicroPython and handles many of the hardware connections from software. Â It allows access to things like GPIO pins, CPU frequency, I2C busses, etc. If it is low-level on the hardware, theÂ <em>machine</em> module is the way to go!

As one can imagine, crammingÂ <strong>everything</strong> from Python 3 into a micro-controller isn't possible, but core data types and modules which make sense for embedded systems are included. Another limitation is that since Python is a high level programming language it isn't as fast or lean as C or C++. While many operations aren't impacted by this, if your application requires tight timing or performance is of utmost importance, using MicroPython can be an issue. However, as stated earlier, one can extend MicroPython to use C/C++ in these circumstances.

Since MicroPython is open source, check it out on <a href="https://github.com/micropython/micropython">GitHub</a>, one can further extend or adapt it specific use case scenarios. For example, if you need something specific to run at boot time you can include it in the package that is flashed to the device. Pretty handy and convenient.

Hopefully I have at least sparked your interest enough in using MicroPython for IoT to take a further look at it. The development site actually has a live, <a href="http://micropython.org/live/">interactive device</a> where you can try out some code and see it run on a real world device. Give it a try and leave a comment below to share with others what you've done and built!

_Follow me on Twitter [@kenwalger](https://www.twitter.com/kenwalger) to get the latest updates on my postings, or see the original post on [my blog](https://www.kenwalger.com/blog)._