PymateIO: Difference between revisions
| (10 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
Micropython offers API classes to manage input/output ([https://docs.micropython.org/en/latest/library/machine.Pin.html Pin] class) and analog input ([https://docs.micropython.org/en/latest/library/machine.ADC.html ADC] class). Other MicroPython classes are available to access buses (like SPI, I2C, UART, CAN). | Micropython offers API classes to manage input/output ([https://docs.micropython.org/en/latest/library/machine.Pin.html Pin] class) and analog input ([https://docs.micropython.org/en/latest/library/machine.ADC.html ADC] class). Other MicroPython classes are available to access buses (like SPI, I2C, UART, CAN). | ||
<br />For sure, the Pymate Core can be controlled directly with such classes and it will work pretty smoothly. However, using those classes also requires some learning around the hardware specs to be efficient. | |||
On the other hand, the '''Pymate library''' offers an interesting '''PymateIO''' abstraction layer covering most of the required hardware details. | |||
[[File:PymateIO-howto2.jpg|800px]] | |||
The {{fname|PymateIO}} class is used to access input, output and analog pins.<br />The same {{fname|PymateIO}} class applies for the Core board as well as the 16ADI16DO expansion board IOs (one or several ones). | |||
<br />The Pymate {{fname|Board}} class provide access to other behaviors like switch button, rs485, CAN bus, UART, etc. | |||
The | == Pin names == | ||
The pin name in the code must follow a minimal convention. | |||
<br />It starts with "In" or "Out" followed by the Pin number (1 to n). | |||
<br />The output 5 will be named "OUT5" where as input 3 will be named "IN3". | |||
<br />The pin name decoding is quite tolerant. It is not case sensitive, tolerate space before the number and allow leading zero in the number. | |||
As such, the following pin name for IN1 are valid: | |||
As such, the following | |||
* In 1 | * In 1 | ||
* IN01 | * IN01 | ||
| Line 27: | Line 25: | ||
* iN 01 | * iN 01 | ||
{{dbox-gray|{{underline|Recommended | {{dbox-gray|{{underline|Recommended usage:}}<br /><br /> | ||
Use uppercase letter, no space, no leading zero for numbers.}} | Use uppercase letter, no space, no leading zero for numbers.}} | ||
== Accessing Pins == | |||
The following cheatsheet shows the main PymateIO input/output feature. | |||
=== Digital Input === | === Digital Input === | ||
Latest revision as of 14:53, 30 April 2026
Introduction
Micropython offers API classes to manage input/output (Pin class) and analog input (ADC class). Other MicroPython classes are available to access buses (like SPI, I2C, UART, CAN).
For sure, the Pymate Core can be controlled directly with such classes and it will work pretty smoothly. However, using those classes also requires some learning around the hardware specs to be efficient.
On the other hand, the Pymate library offers an interesting PymateIO abstraction layer covering most of the required hardware details.
The PymateIO class is used to access input, output and analog pins.
The same PymateIO class applies for the Core board as well as the 16ADI16DO expansion board IOs (one or several ones).
The Pymate Board class provide access to other behaviors like switch button, rs485, CAN bus, UART, etc.
Pin names
The pin name in the code must follow a minimal convention.
It starts with "In" or "Out" followed by the Pin number (1 to n).
The output 5 will be named "OUT5" where as input 3 will be named "IN3".
The pin name decoding is quite tolerant. It is not case sensitive, tolerate space before the number and allow leading zero in the number.
As such, the following pin name for IN1 are valid:
- In 1
- IN01
- in01
- iN 01
Use uppercase letter, no space, no leading zero for numbers.
Accessing Pins
The following cheatsheet shows the main PymateIO input/output feature.
Digital Input
from pymate.pymateio import PymateIO
from machine import Pin
i1 = PymateIO( "IN1", Pin.IN )
# Read the Pin State
val = i1.read() # return 1 or 0
print( "Pin State= %s" % val )The read() method is convenient in automation world.
MicroPython addict can still use the value() method (which act the same).
Digital Output
from pymate.pymateio import PymateIO
from machine import Pin
o4 = PymateIO( "OUT4", Pin.OUT )
# Change the output state
o4.write( 1 ) # activates output
o4.write( 0 ) # deactivate outputThe write() method is convenient in automation world.
MicroPython addict can still use the value( new_state) ) method.
More demonstrative call like on() and off() methods will also applies to outputs.
Analog Input
Any of the input pin can be configured as analog input to read voltage between 0 to 10V DC).
The Analog to Digital converter returns a unsigned 16 bits value (a number between 0 to 65535).
The analog value is returned by the read_u16() method (the same method used in MicroPython).
from pymate.pymateio import PymateIO
from machine import ADC
a2 = PymateIO( "IN2", ADC )
# read the value
val = a2.read_u16() # value from 0..65535
print( 'a2 raw = %s' % val )
# Read voltage
volt = a2.read_volts() # As float
print( "a2 volts= %s' % volt )The read_voltage() method do converts the read_u16() value into equivalent voltage (0 giving 0.0 volts, 65535 giving 10.0 Volts).
PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO