Basic stuff

This section contains examples to learn to play with the basic functionalities of the SBC.

Hello World!

Print something on the microPython terminal.

>>> print( "Hello World from SBC!" )
Hello World from SBC!

Enable GPS/GNSS receivers

Before enabling the GPS/GNSS receivers they should be powered on. Remember that GPS1 is always powered on, so only GPS2 and GPS3 need to be powered on.
We assume that the receivers UART1 is configured at 38400bps, which is the default value.
You can find more information about the Gps class here: GPS – Global Positioning System.

>>> import sbc
>>> pm = sbc.Power_Module()
>>> pm.gps2and3_on()
# At this point, all GPS/GNSS receivers are available from the USB hub (from your PC)!
>>> gps1 = sbc.GPS( 1, 38400 )
>>> gps2 = sbc.GPS( 2, 38400 )
>>> gps3 = sbc.GPS( 3, 38400 )
# All GPS/GNSS receivers are now ready to be used by your microPython application

Enable XBee sockets

This code powers both XBEE sockets and makes them available from the USB hub and then creates a Xbee object for each socket (A and B).
We assume that whatever is mounted on XBee sockets A and B is configured at 115200bps.
You can find more information about the Xbee class here: XBEE.

>>> import sbc
>>> pm = sbc.Power_Module()
>>> pm.xbee_on()
# At this point, you can access XBee socket A and B from the USB hub (from your PC)!
>>> xbee_A = sbc.Xbee( 115200, "XBEE_A" )
>>> xbee_B = sbc.Xbee( 115200, "XBEE_B" )
# XBee sockets A and B are now ready to be used by your microPython application

Turn on/off peripherals

This example turns ON individually all SBC peripherals that have power control: GPS/GNSS receivers 2 and 3, GSM modem (if available), XBee sockets A and B.
After all are enabled, it turns all of them OFF with one command.
You can find more information about the Power_Module class here: Power - Main Power Supply.

>>> import sbc
>>> pm = sbc.Power_Module()
>>> pm.gps2and3_on()
>>> pm.gsm_on()
>>> pm.xbee_on()
# All peripherals with power control are powered ON
>>> pm.disable_all()
# All peripherals with power control are powered OFF

Play with LEDs

This code turn the green LED ON and blinks the red LED at a 10Hz frequency. You can find more information about the Led class here: LED – Light-Emitting Diode.

>>> import sbc
>>> led = sbc.Led()
>>> led.write( "LEDGREEN", 1 )
>>> led.blink( "LEDRED", 10 )

Create a timer

Using a timer can be useful to run certain parts of your code periodically.
The timer functionality is not specific for the SBC, it is the generic from microPython.
In this example we set a timer to run at a period of 250ms (4Hz) and to call the callback function cb() and execute whatever is inside it. After 1 second, the timer is stopped.
You can find more information here.

>>> import time
>>> import machine
>>> def cb( tmr ):
>>>     print( "Timer callback function called!" )
>>> tim = machine.Timer( -1, period=250, callback=cb )
>>> time.sleep( 1 )
Timer callback function called!
Timer callback function called!
Timer callback function called!
Timer callback function called!
>>> tim.deinit()

Read digital input

This code reads the voltage on DIN1 pin.
You can find more information about the Din class here: DIO + PWM – Digital Input Output.
To find the location of the pins and hardware related information, check Digital inputs.

>>> import sbc
>>> din = sbc.Din()
>>> print( din.read( "DIN1" ))
0

Read SBC voltage

This code reads the voltage of the SBC when it is powered by an external power supply (not powered via USB only).
You can find more information about the Ain class here: AIN – Analog Input.
To find the location of the pins and hardware related information, check Analog inputs.

>>> import sbc
>>> ain = sbc.Ain()
>>> print( ain.read_v( "VIN" ), "[V]" )
12.2 [V]

Set digital output

This example sets the voltage on DOUT1 pin.
You can find more information about the Dout class here: DIO + PWM – Digital Input Output.
To find the location of the pins and hardware related information, check Digital inputs.

>>> import sbc
>>> din = sbc.Dout()
>>> dout.write("DOUT1", 1)

Set PWM signal

The code below sets a PWM signal at 1kHz with a 25% duty cycle ON on DOUT1 pin. You can find more information about the Dout class here: DIO + PWM – Digital Input Output.
To find the location of the pins and hardware related information, check Digital inputs.
You can also find a schematic of a servo connection: Servos.

>>> import sbc
>>> dout = sbc.Dout()
>>> dout.set_pwm( "DOUT1", 1000, 25 )

Interrupt with digital input

Example that sets a callback function on pin DIN1 that prints its value when either it states goes from 0 to 1 or 1 to 0.
You can find more information about the Din class here: DIO + PWM – Digital Input Output.
To find the location of the pins and hardware related information, check Digital inputs.

>>> import sbc
>>> def callback_din1( pin ):
>>>             print(pin.value())
>>> din = sbc.Din()
>>> din.set_callback( "DIN1", callback_din1, din.EDGE_RISING | din.EDGE_FALLING )

Add Watchdog

Using a watchdog can be useful to make sure your code does not get locked. In case this happens the watchdog will reset the SBC after a pre-defined timeout.
The watchdog functionality is not specific for the SBC, it is the generic from microPython.
In this example we set a watchdog that will reset the SBC if it is not fed for 2s (2000ms). In this example the watchdog is fed continuously so the SBC will not reset.

>>> import machine
>>> wdt = machine.WDT( timeout=2000 )
>>> while( True ):
>>>    wdt.feed()

You can modify this example and add:

>>> time.sleep(3)

before

>>> wdt.feed()

to see how the watchdog resets the SBC.
You can find more information in this link.

Create multiple threads

The SBC does not support parallel threads because its microprocessor has a single core, but if your application needs to run multiple threads, you can use this functionality. The multiple thread functionality is not specific for the SBC, it is the generic from microPython.
In this example we create two threads that print a simple message at different time intervals.
You can find more information following this link.

>>> import time
>>> import _thread as thread
>>> def task1( args ):
>>>     while( True ):
>>>             print( "task1" )
>>>             time.sleep(1)
>>> def task2( args ):
>>>     while( True ):
>>>             print( "task2" )
>>>             time.sleep(1)
>>> thread.start_new_thread( task1, (None,) )
>>> thread.start_new_thread( task2, (None,) )