Developing with MicroPython

MicroPython is designed to be installed on various microcontrollers. The following is an architectural diagram of MicroPython:

The main difference between Arduino and MicroPython is the place where the code is compiled and run. With Arduino, the code is compiled on the PC before it is uploaded to the Arduino microcontroller. With MicroPython, the code is stored and then executed on the microcontroller.

MicroPython also differs from Arduino in the way code is written. Indents are used in MicroPython instead of curly braces and semi-colons are not required at the end of each line, as they are for C. Here is the MicroPython version of the blinking LED:

import pycom
import time

pycom.heartbeat(False)

while True:
pycom.rgbled(0xFF0000) # Red
time.sleep(1)
pycom.rgbled(0x00FF00) # Green
time.sleep(1)
pycom.rgbled(0x0000FF) # Blue
time.sleep(1)

This code is written for Pycom boards that have RGB LEDs, such as the WiPy or LoPy boards. The code starts by importing the pycom and time libraries before turning off the default heartbeat() function (flashing blue LED). Unlike Arduino C, there is no default method that runs continuously, so a while True: statement is used in its place. The pycom library is used to turn on and off the red, green, and blue LEDs in one-second intervals (time.sleep(1)).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.38.41