Extension module
Olem has 4 extension slots, 2 of which are equipped with a connector that can be used to add new features to OLEM that require electronical components. The connectors have 4 pins :
- P1 and P2 carry logicial signals
- P3 is GND
- P4 is 3V3 and can provide up to 300 mA
The pins from all slots are connected, which means that P1 of one extension slot is connected to P1 of the other extension slot (and so on for all Px).
Extension modules can be used in GPIO, PWM or I2C mode, selected with extmod_mode_set() and the EXTMOD_GPIO, EXTMOD_PWM and EXTMOD_I2C constants.
GPIO mode
In GPIO mode, P1 and P2 can be controlled as simple GPIO in output mode. In that case, P1 is GPIO1 and P2 is GPIO2. Use extmod_gpio_set() with the EXTMOD_GPIO1, EXTMOD_GPIO2, LOW and HIGH constants.
Example
Both pins switch between LOW and HIGH every 3 seconds.
import olem
print("Starting extension module gpio periodic test")
olem.extmod_mode_set(olem.EXTMOD_GPIO)
for i in range(0, 10):
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)
olem.delay_ms(3000)
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.HIGH)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.HIGH)
olem.delay_ms(3000)
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)
print("End of extension module gpio periodic test")
PWM mode
In PWM mode, P1 and P2 can be used to output signals with varying duty cycle and periods. Use extmod_pwm_set() with the EXTMOD_GPIO1 and EXTMOD_GPIO2 constants.

Example
The duty cycle of P1 goes up while the one of P2 goes down.
import olem
period = 20
print("Start pwm")
olem.extmod_mode_set(olem.EXTMOD_PWM)
for i in range(0, period):
print("compare value " + str(i))
olem.extmod_pwm_set(olem.EXTMOD_GPIO1, period, i)
olem.extmod_pwm_set(olem.EXTMOD_GPIO2, period, period - i)
olem.delay_ms(2000)
olem.extmod_mode_set(olem.EXTMOD_GPIO)
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)
print("End pwm")
I2C mode
In I2C mode, P1 is assigned to SCL and P2 is assigned to SDA. Use extmod_i2c_read() and extmod_i2c_write().
Example
Every second, 2 bytes are written to the device at address 0x43, then 4 bytes are read from it.
import olem
print("Starting extension module gpio i2c test")
addr = 0x43
olem.extmod_mode_set(olem.EXTMOD_I2C)
timestamp_start = olem.get_time_ms()
while ((olem.get_time_ms() - timestamp_start) < 20000):
olem.extmod_i2c_write(addr, [0x86, 0x87])
olem.delay_ms(1000)
print(olem.extmod_i2c_read(addr, 4))
olem.delay_ms(1000)
print("End of extension module gpio i2c test")
Constants and functions
Constants
| Constant | Value | Description |
|---|---|---|
LOW | 0 | Low level of a GPIO output. |
HIGH | 1 | High level of a GPIO output. |
EXTMOD_GPIO1 | 1 | Pin P1 of the extension slots. |
EXTMOD_GPIO2 | 2 | Pin P2 of the extension slots. |
EXTMOD_GPIO | 0 | GPIO mode: P1 and P2 are digital outputs. |
EXTMOD_I2C | 1 | I2C mode: P1 is SCL and P2 is SDA. |
EXTMOD_PWM | 2 | PWM mode: P1 and P2 output PWM signals. |
extmod_mode_set(mode)
Initialize the extension slots in the given mode. Only one mode can be used at a time.
| Parameter | Description |
|---|---|
mode | EXTMOD_GPIO, EXTMOD_I2C or EXTMOD_PWM |
extmod_gpio_set(gpio, value)
Set the output level of a pin. Requires the EXTMOD_GPIO mode, see extmod_mode_set().
| Parameter | Description |
|---|---|
gpio | EXTMOD_GPIO1 or EXTMOD_GPIO2 |
value | LOW or HIGH |
extmod_pwm_set(gpio, period, compare_value)
Set the PWM signal of a pin. Requires the EXTMOD_PWM mode, see extmod_mode_set().
| Parameter | Description |
|---|---|
gpio | EXTMOD_GPIO1 or EXTMOD_GPIO2 |
period | period of the signal in ms, as an integer. With 0 the output stays LOW. |
compare_value | time the signal stays HIGH in each period in ms, as an integer |
extmod_i2c_read(addr, length)
Read bytes from a device on the I2C bus. Requires the EXTMOD_I2C mode, see extmod_mode_set().
| Parameter | Description |
|---|---|
addr | address of the device |
length | number of bytes to read |
Returns: list of the bytes read, as integers, or an empty list if the read failed
extmod_i2c_write(addr, data)
Write bytes to a device on the I2C bus. Requires the EXTMOD_I2C mode, see extmod_mode_set().
| Parameter | Description |
|---|---|
addr | address of the device |
data | list of the bytes to write, as integers from 0 to 255 |
Returns: 0 on success, an error code otherwise