Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Created by Franck Albinet with source available here. Also presented by NSRC at APRICOT 2018.

Introduction

In this example, we will create and deploy the proverbial 1st app, “Hello, world!” to a Pycom device. Because the board does not have a display, we will use the USB connection between the board and the development PC to switch on and off an LED. In the simplest terms, a Light-Emitting Diode (LED) is a semiconductor device that emits light when an electric current is passed through it. LEDs are described as solid-state devices. The term solid-state lighting distinguishes this lighting technology from other sources that use heated filaments (incandescent and tungsten halogen lamps) or gas discharge (fluorescent lamps). Different semiconductor materials produce different colors of light.

...

If you want your code to be permanently stored on the board, you need to open the LED directory and sync it to your board.

Boot.py


Code Block
languagepy
titleboot.py
linenumberstrue
### boot.py

from machine import UART
import os
uart = UART(0, 115200)
os.dupterm(uart)

...

  • line 1: we import from the machine module the class UART (duplex serial communication bus)
  • line 2: we import the os module (basic operating system services)
  • line 3: we create an UART object (initalized with bus number=0 and baudrate=115200 - the clock rate)
  • and finally pass it to the dupterm method of the os module in order to make the REPL possible via Atom editor for instance.


Code Block
languagepy
titlemain.py
linenumberstrue
main.py
import pycom
import time
pycom.heartbeat(False)
for cycles in range(10): # stop after 10 cycles
    pycom.rgbled(0x007f00) # green
    time.sleep(5)
    pycom.rgbled(0x7f7f00) # yellow
    time.sleep(5)
    pycom.rgbled(0x7f0000) # red
    time.sleep(5)

You should see the LED light up for five seconds at a time, in green, yellow and red. After the sequence has gone through ten cycles, the system will stop and the LED will remain red.

Let's analyze the code:

Code Block
import pycom
import time

We first import two libraries: pycom and time. The pycom one includes the utilities necessary to operate the specific pycom hardware. The time one is used to keep track of time and helps operate the internal clock.

Code Block
pycom.heartbeat(False)


We then deactive the heartbeat funcionality.

Code Block
for cycles in range(10): # stop after 10 cycles

We start a cycle and limit it to 10.

Indentation is very important in python! Make sure you indent the code after the for command.

Code Block
	pycom.rgbled(0x007f00) # green
    time.sleep(5)


Switch on the LED and select the green color. Then sleep for 5 seconds. The sleep command accepts seconds (and not milliseconds not minutes!).

...