{"id":74753,"date":"2018-10-16T09:48:16","date_gmt":"2018-10-16T09:48:16","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=74753"},"modified":"2019-04-02T10:23:18","modified_gmt":"2019-04-02T10:23:18","slug":"micropython-programming-basics-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-programming-basics-esp32-esp8266\/","title":{"rendered":"MicroPython Programming Basics with ESP32 and ESP8266"},"content":{"rendered":"<p>MicroPython is a re-implementation of Python programming language targeted for microcontrollers and embedded systems like the ESP32 or ESP8266.<!--more--><\/p>\n<p>Programming in MicroPython is very similar to programming in Python: all of the language features of Python are also in MicroPython, apart from a few exceptions. Because microcontrollers and embedded systems are much more limited than our computers, MicroPython does not come with the full standard library by default.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-75535 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>If you already know how to program in Python, programming in MicroPython is the same. You just need to bear in mind that MicroPython is used for constrained devices. So, you must keep your code as simple as possible.<\/p>\n<p>This article explains the basics of Python programming language syntax that also apply to MicroPython, like:<\/p>\n<ul>\n<li>Mathematical operators<\/li>\n<li>Relational operators<\/li>\n<li>Data types<\/li>\n<li>print() function<\/li>\n<li>Conditional statements<\/li>\n<li>While and for loops<\/li>\n<li>User defined functions<\/li>\n<li>Classes and objects<\/li>\n<li>Modules<\/li>\n<\/ul>\n<h2>Prerequisites<\/h2>\n<p>In this tutorial we\u2019re going to use uPyCraft IDE as a development environment, but you can use any other program. To install and get started with uPyCraft IDE, follow these next tutorials:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with MicroPython on ESP32 and ESP8266<\/a><\/li>\n<li>Install uPyCraft IDE:\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Install uPyCraft IDE \u2013 Windows PC Instructions<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Install uPyCraft IDE \u2013 Mac OS X Instructions<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Install uPyCraft IDE \u2013 Linux Ubuntu Instructions<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<h2>Mathematical Operators<\/h2>\n<p>Micropython can perform mathematical operations. The following table shows the mathematical operators supported:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Operator<\/strong><\/td>\n<td><strong>Mathematical Operation<\/strong><\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>Addition<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>Subtraction<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>Multiplication<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>Division<\/td>\n<\/tr>\n<tr>\n<td>\/\/<\/td>\n<td>Division, discarding the decimal point<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>Remainder after division<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">In the Shell, try several operations to see how it works. For example:<\/span><\/p>\n<pre>&gt;&gt;&gt;<strong> 2+2*9-3<\/strong>\n17\n&gt;&gt;&gt;<strong> 28594\/2312<\/strong>\n12.36765\n&gt;&gt;&gt;<strong> 214522236\/7.5<\/strong>\n2.860297e+07\n&gt;&gt;&gt;<strong> 23\/\/2<\/strong>\n11\n&gt;&gt;&gt;<strong> 25%3<\/strong>\n1\n<\/pre>\n<p>You can perform other mathematical operations if you import the <em>math<\/em> module, like square root, trigonometric functions, logarithm, exponentiation, etc..<\/p>\n<h2>Relational Operators<\/h2>\n<p>You can make comparisons using relational operators. These compare the values on either sides and show the relation between them.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Operator<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>==<\/td>\n<td>Equal to<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td>Not equal to<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td>Greater than<\/td>\n<\/tr>\n<tr>\n<td>&lt;<\/td>\n<td>Less than<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>Greater than or equal to<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>Less than or equal to<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Try several comparisons and test the result:<\/span><\/p>\n<pre><strong>&gt;&gt;&gt; 2 == 3<\/strong>\nFalse\n<strong>&gt;&gt;&gt; 4 == 4<\/strong>\nTrue\n<strong>&gt;&gt;&gt; 3 &gt; 2<\/strong>\nTrue\n<strong>&gt;&gt;&gt; 489808234 != 2223<\/strong>\nTrue\n<strong>&gt;&gt;&gt; 4.5 &gt;= 4.5<\/strong>\nTrue<\/pre>\n<h2>Assigning Values to Variables<\/h2>\n<p>In Python you don\u2019t need to declare what type each variable is. If you&#8217;re used to program your boards using Arduino IDE, you know that you need to declare the type of a variable when creating a new variable.\u00a0There isn\u2019t such thing in Python.<\/p>\n<p>Variables are simply a storage placeholder for values: number or text. To assign a value to a variable you use the equal sign (=), with the variable name on the left and the value on the right.<\/p>\n<p>For example, to create a variable to hold the GPIO number where an LED is connected to, you can simply type the following:<\/p>\n<pre>led_pin = 23<\/pre>\n<p>In the Arduino IDE, you would have something like:<\/p>\n<pre>const int led_pin = 23;<\/pre>\n<p>As you can see, Python is much simpler than programming in C (in Arduino IDE).<\/p>\n<p><strong>Note:<\/strong> the names you give variables can\u2019t have spaces and are case sensitive, so <em>led_pin<\/em> is different from <em>LED_PIN<\/em> or <em>Led_Pin.<\/em><\/p>\n<h2>Data Types<\/h2>\n<p>Variables can store several types of values, not just whole numbers. That\u2019s where <em>data types<\/em> come in. A data type is a classification of a value that tells what operations can be done with the value and how it should be stored.<\/p>\n<p>The following table shows the data types we\u2019ll use most often in our projects.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Data type<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>int (Int)<\/td>\n<td>Integer (whole number)<\/td>\n<\/tr>\n<tr>\n<td>float (Float)<\/td>\n<td>Number with a decimal point<\/td>\n<\/tr>\n<tr>\n<td>str (String)<\/td>\n<td>Set of characters between quotation marks<\/td>\n<\/tr>\n<tr>\n<td>bool (Boolean)<\/td>\n<td>True or False<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Let\u2019s create variables with different data types:<\/span><\/p>\n<pre>&gt;&gt;&gt; a = 6\n&gt;&gt;&gt; b = 95.32\n&gt;&gt;&gt; c = 'Hello World!'\n&gt;&gt;&gt; d = True<\/pre>\n<ul>\n<li>The first value assigned to <em>a<\/em>, is an <strong>integer<\/strong>, which is a whole number.<\/li>\n<li>The <em>b<\/em> variable contains a <strong>float<\/strong> value, which is a number with a decimal.<\/li>\n<li>The third value, &#8216;Hello World!&#8217;, is a <strong>string<\/strong>, which is a series of characters. A string must be put inside single (&#8216;Hello World!&#8217;) or double quotation (&#8220;Hello World!&#8221;) marks.<\/li>\n<li>Finally, <em>d<\/em><i> <\/i>is a <strong>Boolean<\/strong>, which is a type that can only take either True or False.<\/li>\n<\/ul>\n<p>There is a function to check the data type of a variable: the <em><strong>type()<\/strong> <\/em>function. This function accepts as argument the variable you want to check the data type.<\/p>\n<pre>type(variable)<\/pre>\n<p><span style=\"font-weight: 400;\">For example, after declaring the variables in the previous example <em>a<\/em>, <em>b<\/em>, <em>c<\/em>, and <em>d<\/em>, you can check its data type. For example, if you type:<\/span><\/p>\n<pre>&gt;&gt;&gt; type(a)<\/pre>\n<p>It returns:<\/p>\n<pre>&lt;class 'int'&gt;<\/pre>\n<p>This tells that <em>a<\/em> is an int (integer). Experiment with the other variables and you should get:<\/p>\n<pre>&gt;&gt;&gt; <strong>type(b)<\/strong>\n&lt;class 'float'&gt;\n&gt;&gt;&gt; <strong>type(c)<\/strong>\n&lt;class 'str'&gt;\n&gt;&gt;&gt; <strong>type(d)<\/strong>\n&lt;class 'bool'&gt;<\/pre>\n<h2>print() Function<\/h2>\n<p>The <em>print()<\/em> function prints the message between parentheses into the Shell. This is specially useful in our projects to debug the code and keep track of what&#8217;s going on. For example:<\/p>\n<pre>&gt;&gt;&gt; <strong>print('LED is on')<\/strong>\nLED is on<\/pre>\n<h2>Comments<\/h2>\n<p>Comments in Python start with the hash character (#) and continue to the end of the line. A comment is useful to add &#8220;notes&#8221; in your program or to\u00a0tell anyone who reads the program what the script does. This doesn&#8217;t add any functionality to your program. For example:<\/p>\n<pre># This is just a comment<\/pre>\n<p>Because in MicroPython we are working under constrained conditions, there are occasions in which you should avoid adding comments to save space on the ESP memory.<\/p>\n<h2>Conditional Statements<\/h2>\n<p>To write useful programs, you&#8217;ll probably need to perform different actions depending on whether a certain condition is True or False. We&#8217;re talking about conditional statements. They have the following structure:<\/p>\n<pre>if &lt;expr1&gt;:\n  &lt;statement1&gt;\nelif &lt;expr2&gt;:\n  &lt;statement2&gt;\nelif &lt;expr3&gt;:\n  &lt;statement3&gt;\n(...)\nelse:\n  &lt;statementn&gt;<\/pre>\n<p>&lt;expr&gt; is a Boolean expression and it can be either <strong>True<\/strong> or <strong>False<\/strong>. If it is True, the &lt;statement&gt; right after it is executed. The &lt;statement&gt; should be indented so that Python knows what statement belongs to each expression.<\/p>\n<p>The <em>elif<\/em> statement stands for else if and runs only if the first <em>if\u00a0<\/em>condition is not True.<\/p>\n<p>The <em>else<\/em> statement only runs if none of the other expressions are True.<\/p>\n<p>There&#8217;s no limit to the number of <em>elif<\/em> statements in a program. It\u2019s also not necessary to include an <em>else<\/em> clause, but if there is one, it must come at the end.<\/p>\n<p>In the Arduino IDE, we use {} curly brackets to define code blocks. With MicroPython, we use indentation. Additionally, you need to use a colon <strong>:<\/strong> after each expression. Contrary to the Arduino IDE, the expression doesn\u2019t need to be inside parentheses.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important<\/strong>:<\/span>\u00a0Python\u2019s standard indentation is 4 spaces. In MicroPython <strong>indentation should be only 2 spaces<\/strong> to fit more code into the microcontroller memory.<\/p>\n<h2>While and For loops<\/h2>\n<p>Loops allow you to execute a block of code multiple times for as long as a condition is met. There are two kinds of loops: <em>while<\/em> and <em>for<\/em> loops. For example, you can print all numbers from 1 to 10 with a <em>while<\/em> loop:<\/p>\n<pre>number = 1\n<strong>while<\/strong> number &lt;= 10:\n  print(number)\n  number = number + 1<\/pre>\n<p>The code that belongs to the while loop, indicated by the indentation, is executed as long as the value in the\u00a0<em>number<\/em> variable is less than or equal to (&lt;=) 10. In every loop, the current <em>number<\/em> is printed and then 1 is added to it.<\/p>\n<p>You can also print numbers from 1 to 10 using a <em>for<\/em> loop, like this:<\/p>\n<pre>number = 1\n<strong>for<\/strong> number in range(1, 11):\n  print(number)<\/pre>\n<p>The <em>for<\/em> loop is executed as long as the value in the\u00a0<em>number<\/em> variable is within the range of 1 and 11. The <em>range()<\/em> function automatically assigns the next value to the <em>number<\/em> variable, until 1 below the final number you specify.<\/p>\n<p>You should use a for loop when you want to repeat a block of code a certain number of times. Use a while loop when you want to repeat code until a certain condition is no longer met. In some situations, you can use either one,\u00a0oftentimes one is more suitable than the other.<\/p>\n<p>Similar to the conditional statements, the <em>for<\/em> and <em>while<\/em> Boolean expressions should have a colon <strong>:<\/strong> right after them, and the expressions to be executed should be indented.<\/p>\n<h2>User-defined Functions<\/h2>\n<p>To define a new function, you use the word <em>def<\/em> followed by the name you want to give the function and a set of parentheses (and arguments inside, if necessary). After the parentheses you add a colon <strong>:<\/strong> and then tell the function what instructions to perform. The statements should be indented with 2 spaces (in MicroPython). For example:<\/p>\n<pre><strong>def<\/strong> my_function(&lt;arg1&gt;, &lt;arg2&gt;, ...):\n  &lt;statement&gt;\n  (...)\n  return<\/pre>\n<p>For example, a function that converts the temperature in Celsius to Fahrenheit could be the following:<\/p>\n<pre>def celsius_to_fahrenheit(temp_celsius): \n  temp_fahrenheit = temp_celsius * (9\/5) + 32 \n  return temp_fahrenheit<\/pre>\n<p>The <em>celsius_to_fahrenheit()<\/em> function accepts as argument a temperature in Celsius (<em>temp_celsius<\/em>). Then, it does the calculation to convert the temperature. Finally, it returns the temperature in Fahrenheit (<em>temp_fahrenheit<\/em>).<\/p>\n<p><strong>Note<\/strong>: functions don&#8217;t necessarily need to return something. They could just perform some work without the need to return anything.<\/p>\n<h2>Classes and Objects<\/h2>\n<p>Python is an Object-Oriented Programming (OOP) language. There are two important concepts you need to understand about OOP: classes and objects.<\/p>\n<p>A class is a blueprint for objects. It defines a set of attributes (data and functions) that characterize an object. The functions inside of a class are called methods. Classes are defined by the keyword <strong><em>class<\/em> <\/strong>followed by the name of the class. For example:<\/p>\n<pre>class MyClass:\n  (...)<\/pre>\n<p><strong>Note:<\/strong> by convention, classes&#8217; names in Python should be CapWords. However, you can give whatever name you want.<\/p>\n<p><span style=\"font-weight: 400;\">An object is an instance of a class. It&#8217;s simply a collection of data and methods into a single entity. Through the object, you can use all functionalities of its class. Confused? Let\u2019s take a look at a simple example.<\/span><\/p>\n<p>If we would like to define several persons in a Python program using the same attributes, we can think of the term person as a class. We may want to define a person using attributes like name, age, country, etc.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-75558\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/objects-class.png?resize=455%2C323&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"455\" height=\"323\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/objects-class.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/objects-class.png?resize=300%2C213&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 455px) 100vw, 455px\" \/><\/p>\n<p>So, we can create a class called <em>Person<\/em>. Our class will have the following attributes: <em>name<\/em>, <em>age<\/em>, and <em>country<\/em>. You can add as many attributes as you want. We\u2019ll also create a function (method) that prints a description of the person based on its attributes:<\/p>\n<pre>class Person:\n  name = \"\"\n  age = 0\n  country = \"\"\n  def description(self):\n    print(\"%s is %d years old and he is from %s.\" %(self.name, self.age, self.country))<\/pre>\n<p>As you can see, we define a new class by using the keyword <em>class<\/em>, followed by the name we want to give to the class.<\/p>\n<p>Inside the <em>Person<\/em>\u00a0class, we define several variables to hold values. By default the <em>name<\/em> and <em>country<\/em> are empty strings, and the <em>age<\/em> is 0. Then, we also define a function (method) that prints all the variables values into the Shell.<\/p>\n<p>All functions inside a class should have the <em>self<\/em>\u00a0parameter as argument and other arguments, if needed.<\/p>\n<p>The\u00a0<em>self\u00a0<\/em> parameter refers to the object itself. It is used to access variables that belong to the class. For example, to access the <em>name<\/em> variable inside the class, we should use <em>self.name<\/em>.<\/p>\n<p>Now that we\u2019ve create a class, we can create as many <em>Person<\/em> objects as we want by using that class. The <em>Person<\/em> object will have a name, age, and country. We\u2019ll also be able to print its description using the <em>description()<\/em> method.<\/p>\n<p>For example, to create a new <em>Person<\/em> object called <em>person1<\/em>:<\/p>\n<pre>&gt;&gt;&gt; person1 = Person()<\/pre>\n<p><strong>Set the object&#8217;s properties<\/strong><\/p>\n<p>To set the <em>name<\/em>, <em>age<\/em>, and <em>country<\/em> of the <em>person1<\/em> object. You can do it as follows:<\/p>\n<pre>&gt;&gt;&gt; person1.name = \"Rui\"\n&gt;&gt;&gt; person1.age = 25\n&gt;&gt;&gt; person1.country = \"Portugal\"<\/pre>\n<p><strong>Calling methods<\/strong><\/p>\n<p>Later in your code you can use the created <em>description() <\/em>method\u00a0on any\u00a0<em>Person<\/em> object. To call the <em>description()<\/em> method on the <em>person1<\/em> object:<\/p>\n<pre>&gt;&gt;&gt; person1.description()<\/pre>\n<p>This should print the following:<\/p>\n<pre>Rui is 25 years old and he is from Portugal.<\/pre>\n<p>You should now understand that you can create as many objects as you want using the same class and you are able to use the available methods with all the objects of that class.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-75563\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"750\" height=\"422\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/creating-objects.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<p><strong>The constructor method<\/strong><\/p>\n<p>Instead of having to define a class, and then set the object&#8217;s properties, which can be time consuming, you can use the constructor method inside your class.<\/p>\n<p>The constructor method is used to initiate data as soon as an object of a class is instantiated. The constructor method is also known as __init__ method.\u00a0 Using the __init__ method, the <em>Person()<\/em>\u00a0class looks as follows:<\/p>\n<pre>class Person():\n  def __init__(self, name, age, country):\n    self.name = name\n    self.age = age\n    self.country = country\n  def description(self):\n    print(\"%s is %d years old and he is from %s.\" %(self.name, self.age, self.country))<\/pre>\n<p>Then, to instantiate a <em>Person<\/em> object with the same attributes we&#8217;ve defined earlier, we just need to do the following:<\/p>\n<pre>&gt;&gt;&gt; person1 = Person(\"Rui\", 25, \"Portugal\")<\/pre>\n<p>If you call the <em>description()<\/em> on the <em>person1<\/em> object, you&#8217;ll get the same result:<\/p>\n<pre>&gt;&gt;&gt; <strong>person1.description()<\/strong>\nRui is 25 years old and he is from Portugal.<\/pre>\n<h2>Modules<\/h2>\n<p>A module is a file that contains a set of classes and functions you can use in your code &#8211; you can also call it library. To access the classes and functions inside that code, you just need to import that module into your code.<\/p>\n<p>You can create your own modules, or use already created modules from the standard Python library. When it comes to MicroPython, it only comes with a small subset of the standard Python library, but it does come with a set of modules to control GPIOs, make networks connections and much more.<\/p>\n<p>Importing modules\/libraries is as simple as using:<\/p>\n<pre>import module_name<\/pre>\n<p>For example, to import the <em>machine<\/em> library that contains classes to control GPIOs, type the following:<\/p>\n<pre>import machine<\/pre>\n<p>In most programs you won&#8217;t need all the classes from one module. You may just want to import a single class. For example, to import only the <em>Pin<\/em> class from the <em>machine<\/em> module:<\/p>\n<pre>from machine import Pin<\/pre>\n<h2><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75560\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/from-machine-import-pin-Copy.png?resize=750%2C347&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"750\" height=\"347\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/from-machine-import-pin-Copy.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/from-machine-import-pin-Copy.png?resize=300%2C139&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/h2>\n<h2>Wrapping Up<\/h2>\n<p>In this tutorial we&#8217;ve just scratched the surface of Python basics that also apply to MicroPython. If you are used to program electronics using the Arduino IDE, you&#8217;ll find that MicroPython has a much simpler and user-friendly syntax. Let&#8217;s just summarize some of the main differences between your Arduino sketches and programs in MicroPython:<\/p>\n<ul>\n<li>You don&#8217;t use semicolon <strong>;<\/strong> at the end of a statement<\/li>\n<li>After Boolean expressions in conditional statements and loops, you use a colon <strong>:<\/strong><\/li>\n<li>To define code blocks use indentation instead of curly brackets {}<\/li>\n<li>When creating a variable, you don&#8217;t need to define which data type it is &#8211; you don&#8217;t need to declare a variable<\/li>\n<li>Indentation in MicroPython is 2 spaces<\/li>\n<\/ul>\n<p><strong>Recommend reading: <\/strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/p>\n<p>We hope you&#8217;ve found this article about the MicroPython basics syntax useful. We&#8217;ll be adding tutorials about programming the ESP32 and ESP8266 using MicroPython very soon. So, stay tuned and <a href=\"https:\/\/randomnerdtutorials.com\/download\/\">subscribe the RNT blog<\/a>!<\/p>\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MicroPython is a re-implementation of Python programming language targeted for microcontrollers and embedded systems like the ESP32 or ESP8266.<\/p>\n","protected":false},"author":5,"featured_media":75535,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,265,310,309,264],"tags":[],"class_list":["post-74753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp8266-project","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/programming-micropython-esp32-esp8266.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/74753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=74753"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/74753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/75535"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=74753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=74753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=74753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}