This Tutorial assume that
- You can read this ;)
- You can spell "Box0" correctly. ==> say: box zero
- You are familiar with Python 
- You have a Box0 and basic electronics components
Box0 devices can be controlled using Python.
There is a module called "box0" for python.
module can be imported in python by using
In order to do anything with the device, user need to open a device.
box0.Device crossponds to a physical Hardware.
you can open a device using
box0.usb.open_supported() means "Open a supported Box0 device connected on USB"
import box0 dev = box0.usb.open_supported()
Device is itself not useful for anything.
In order to do something useful out of device, we need to take a module from it and work with it.
You can consider Device as House but a House contain Rooms where you live.
In Box0, all modules are independent of each other.
So, if you have a module, it is expected not to be affected by any other module.
A device can contain many types of module.
you can open open module using
|AIN||Analog In||dev.ain()||Capture Analog Signal from cruel world|
|AOUT||Analog Out||dev.aout()||Generate Analog Signal for the cruel world|
|SPI||Serial Peripherial Interface||dev.spi()||Communicate with Slaves  connected on SPI Bus|
|I2C||Inter Integerated Communication||dev.i2c()||Communicate with Slaves  connected on I2C Bus|
|PWM||Pulse Width Modulation||dev.pwm()||Generate Pulse Width modulated signals|
|DIO||Digital Input/Output||dev.dio()||Read/Write Digital signals|
: We need "Electronic rights"!
If you have allocated a Device or Module, you need to dispose it properly. The problem become promintent in Jupyter or IPython because the same kernel for execution (until the "Kernel reset" is performed) So, if you have allocated a module, need to deallocate it after use.
You can use various techniques, some are given below.
with, you can allocate resource and deallocate the resource the automatically when the execution goes out of the scope of the block.
import box0 with box0.usb.open_supported() as dev, dev.<module>() as mod: # do something with "mod"
Manually handling Exception
When you press "interrupt kernel" it is converted into
KeyboardInterruptwhich is a exception that stop the execution of the program.
You need to catch the exception and deallocate the resource and then exit.
import box0 # allocate resources dev = box0.usb.open_supported() mod = dev.<module>() try: while True: # do something with "mod" except KeyboardInterrupt: # ah ha, we need to stop right now! pass # deallocate resources mod.close() dev.close()
If modules are not properly close (in Jupyter or IPython) the, the resources are still allocated to object that has not been closed (but not garbage collected i.e.
__del__ not called).
Next time the cell is executed, the module allocation will fail with Resource not available (ResultException: B0_ERR_UNAVIL)