Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Physical Computing

00:00 Physical Computing. When it comes to MicroPython, what does physical computing actually mean? Well, usually, your project will consist of three elements.

00:11 Firstly, input—whether that’s a button, a signal, an internet event, or data from a sensor. Secondly, processing. A microcontroller processes the input and updates the output.

00:26 Thirdly, that output. This can be sent in the form of a motor spinning, an LED lighting up, a counter changing, a message being sent, or anything else that can be controlled via electronic or electrical means.

00:38 These elements will usually be connected by wires and powered by some power source, necessitating some electronic knowledge.

00:47 What is a microcontroller? You may know that a microcontroller is small and not as powerful as the computer on your desktop or in your server rack. What you may not know is microcontrollers are all around you, adding intelligence and control to your devices.

01:02 They’re embedded in everything from appliances and home security setups to pacemakers, heating and ventilation systems, and more. Microcontrollers do relatively simple things day after day, reliably, and in a compact package.

01:16 They compress a CPU, memory, and IO into one general-purpose chip, rather than requiring whole boards of chips that team up to perform a task. The code that they run is called firmware, which is flashed or burned onto writeable memory before executing.

01:33 While there are those of us who grew up with early microcomputers like the ZX Spectrum and Commodore Vic20 and may find these chips unbelievably powerful, you actually have more power available in your smartwatch.

01:46 Still, microcontrollers are extremely useful, even if their processing power and memory capabilities are limited by modern standards. A great deal can still be done with these little guys!

01:59 Why MicroPython? These microcontrollers were proliferating long before MicroPython existed, so what benefits will it bring you over traditional practices? Well, first, the language is more accessible to beginners than competing languages, while still being powerful enough for industrial use cases.

02:18 You can go from learning the basics to doing real work, and do so quickly. Secondly, Python allows for rapid feedback. This is because you can interactively enter commands and get a response using the REPL.

02:31 You can even tweak your code and run it right away rather than iterating through code-compile-upload-execute cycles.

02:40 Last, the wealth of Python code and experience out there means that you can do some things more quickly and easily as a Python programmer. Libraries such as requests and json make handling complex tasks simple that would be extremely difficult to do in other languages.

02:56 What about C++? Well, on the upside, C++ is fast, it’s compact, and it’s ubiquitous. It’s available everywhere, and there are tons of C++ programmers out there and a rich community of Arduino and PIC developers ready to help you.

03:15 Isn’t it a better choice? Well, there’s more to it than that. On the downsides of C++ compared to Python, development speed is often slower. It’s very quick to write code in Python, as we will see soon, and there is a steep learning curve to learning C++. It’s nowhere near as forgiving or easy to understand as Python is. Thirdly, C++ code can be extremely complex. Implementing identical functionality in Python can be quicker, simpler, and easier to understand.

03:49 Now, let’s take a look at this same function being implemented across multiple languages, each of which could be a contender for use with a microcontroller.

03:59 So, here, you’re going to see three programs, all of which do the same thing, which is calculate the terms of the Fibonacci sequence. So, this first one is written in Assembler, and due to the way that Assembler works, every single instruction for the processor needs to be explicitly stated.

04:14 This means the program is long and complicated. As you can see, it runs to over 120 lines of code.

04:22 And without the comments on each line—as you can see, most of them have—it would be difficult to understand this as you have to work out what it’s doing from first principles.

04:32 Compare that with the version written in C. Here, the program’s written in C, and it’s comprehensible even without comments. However, it’s not as prosaic as the final version we’re going to take a look at, which of course is in Python.

04:48 The Python version is concise and readable, even without any comments. This demonstrates one of the reasons for developing with Python. The code can be simple to create, easy to debug, comprehensible in the future, and extendable if needs be. In the next lesson, you’ll take a look at the hardware your Python code can run on.

Become a Member to join the conversation.