Arduino — Simple IO using open source hardware

Generic Arduino Support

Generic Arduino abstractors support digital input with and without internal pullup, digital output, analog input and analog output (pwm).

Pins are read and written by simply reading from or writing to member variables of the used abstractor.

Digital output pins are accessed by writing a boolean or integer value to do<N> or digital_output<N> where <N> is the pin number. Additionally you can call the method digital_output(pin) if you like to. When writing a digital output pin the pin is automatically set to digital output mode.

Digital input pins can be accessed by reading di<N> or digital_input<N> where <N> is the pin number. Additionally you can call the method digital_input(pin) if you like to. When reading a digital input pin the pin is automatically set to digital input mode.

Digital input pins with internal pullup can be accessed by reading dip<N> or digital_input_pullup<N> where <N> is the pin number. Additionally you can call the method digital_input_pullup(pin) if you like to. When reading a digital input pin with internal pullup the pin is automatically set to digital input mode with pullup enabled.

Analog output or pwm pins are accessed by writing an integer value (0 .. 255) to pwm<N>, ao<N> or analog_output<N> where <N> is the pin number. Additionally you can call the method analog_output(pin) if you like to. When writing an analog output (pwm) pin the pin is automatically set to analog output (pwm) mode. There are different names for the same pins. This is useful to give you code a better understanding of what happens when the pin is set. A pwm pin with a low-pass filter connected results in an analog voltage so the user would rather use ao<N> instead of pwm<N>.

Analog input pins are accessed by reading ai<N> or analog_input<N> where <N> is the pin number. Additionally you can call the method analog_input(pin) or pwm(pin) if you like to. When reading an analog input the pin is automatically set to analog input mode.

You need the io feature in your license to unlock the Arduino features.

Arduino Nano

htf.arduino.ArduinoNano supports 12 digital input pins with and without internal pullup (D2 .. D13), 12 digital output pins (D2 .. D13), 8 analog input pins (A0 .. A7) and 6 analog output (pwm) pins (D3, D5, D6, D9, D10, and D11).

To setup the Arduino Nano use:

from htf.arduino import ArduinoNano
a = ArduinoNano("/dev/ttyUSB0")
# set pin D1 to 1
a.do3 = 1
# set pin D1 to 0
a.do3 = 0
# read pin D4
a.di4
>>> 0
# read pin D4 with internal pullup
a.dip4
>>> 1
# read A3
a.ai3
>>> 486
# set pwm level to 50 % on D3
a.pwm3 = 0x8f
# set analog voltage to 5 V on D5
a.ao5 = 0xff
class htf.arduino.ArduinoNano(port: str, timeout: int | float = 0.5)

Initialize an ArduinoNano.

Parameters:
  • port – the name of the serial connection

  • timeout=0.5 – the time-out in seconds

__del__() None

Automatically close serial connection on deletion.

analog_input(pin: int) int

Read an analog input pin.

Parameters:

pin – the pin to be read

Returns:

the analog value of pin

Return type:

int

analog_output(pin: int, value: int) None

Set pin to analog level value. This is useful if pin is connected to a low-pass filter.

Parameters:
  • pin – the pin to be used

  • value – the analog output level (0 .. 255)

close() None

Close the serial connection.

digital_input(pin: int) int

Read a digital input pin.

Parameters:

pin – the pin to be read

Returns:

the pin state of pin

Return type:

int

digital_input_pullup(pin: int) int

Read a digital input pin with internal pullup enabled.

Parameters:

pin – the pin to be read

Returns:

the pin state of pin

Return type:

int

digital_output(pin: int, state: int) None

Write a digital output pin.

Parameters:
  • pin – the pin to be written

  • state – the state for pin

get_board_name() str

Return the board name read from the device.

get_version() int

Return the version read from the device.

pwm(pin: int, value: int) None

Set pin to pwm-level value.

Parameters:
  • pin – the pin to be used

  • value – the pwm level (0 .. 255)

Arduino Leonardo

htf.arduino.ArduinoLeonardo supports 14 digital input pins with and without internal pullup (D0 .. D13), 14 digital output pins (D0 .. D13), 12 analog input pins (A0 .. A5, A6 .. A11 on digital pins D4, D6, D8, D9, D10, and D12) and 6 analog output (pwm) pins (D3, D5, D6, D9, D10, D11 and D13).

To setup the Arduino Leonardo use:

from htf.arduino import ArduinoLeonardo
a = ArduinoLeonardo("/dev/ttyACM0")
# set pin D1 to 1
a.do3 = 1
# set pin D1 to 0
a.do3 = 0
# read pin D4
a.di4
>>> 0
# read pin D4 with internal pullup
a.dip4
>>> 1
# read A3
a.ai3
>>> 486
# set pwm level to 50 % on D3
a.pwm3 = 0x7f
# set analog voltage to 5 V on D5
a.ao5 = 0xff
class htf.arduino.ArduinoLeonardo(port: str, timeout: int | float = 0.5)

Initialize an ArduinoLeonardo.

Parameters:
  • port – the name of the serial connection

  • timeout=0.5 – the time-out in seconds

__del__() None

Automatically close serial connection on deletion.

analog_input(pin: int) int

Read an analog input pin.

Parameters:

pin – the pin to be read

Returns:

the analog value of pin

Return type:

int

analog_output(pin: int, value: int) None

Set pin to analog level value. This is useful if pin is connected to a low-pass filter.

Parameters:
  • pin – the pin to be used

  • value – the analog output level (0 .. 255)

close() None

Close the serial connection.

digital_input(pin: int) int

Read a digital input pin.

Parameters:

pin – the pin to be read

Returns:

the pin state of pin

Return type:

int

digital_input_pullup(pin: int) int

Read a digital input pin with internal pullup enabled.

Parameters:

pin – the pin to be read

Returns:

the pin state of pin

Return type:

int

digital_output(pin: int, state: int) None

Write a digital output pin.

Parameters:
  • pin – the pin to be written

  • state – the state for pin

get_board_name() str

Return the board name read from the device.

get_version() int

Return the version read from the device.

pwm(pin: int, value: int) None

Set pin to pwm-level value.

Parameters:
  • pin – the pin to be used

  • value – the pwm level (0 .. 255)

Arduino Leonardo Digital Input

htf.arduino.ArduinoLeonardoDigitalInput can be used to read all 14 digital inputs (pins 0 .. 13) and to count edges (using pin-change interrupts) on pins 0, 1, 2 and 3.

To use the Arduino Leonardo Digital Inputs:

di = ArduinoLeonardoDigitalInput("COM4")
print("Version:", di.get_version())
data = di.get()
print(data) # a dictionary containing all data

All entries in data starting with "d" are digital input states, ie. 0 or 1. All entries in data starting with "c" are edge counts, ie. unsigned long int. Edge counts can be reset using reset.

class htf.arduino.ArduinoLeonardoDigitalInput(port: str, reset_on_init: bool = True, timeout: int | float = 0.5)

ArduinoLeonardoDigitalInput reads inputs (on pins 0 .. 13) and counts edges (on pins 0, 1, 2 and 3) using pin change interrupts on Arduino Leonardo.

Serial connection: 115200 baud, 8N1.

Parameters:
  • port – the com port the Arduino Leonardo listens to running the digital input application.

  • reset_on_init=True – reset internal edge counters on initialization.

  • timeout=0.5 – the timeout for serial communication in seconds.

close() None

Close serial connection.

get() Dict[str, int]

Get current data, ie. digital input states and edge counts.

Returns:

the current data containing digital port states and counter values.

E.g. {‘d0’:1,’d1’:1,’d2’:0,’d3’:1,’d4’:1,’d5’:1,’d6’:1,’d7’:1,’d8’:1,’d9’:1,’d10’:1,’d11’:1,’d12’:1, ‘d13’:1,’c0’:0,’c1’:0,’c2’:77,’c3’:102}

Return type:

dict

get_version() bytes

Get software version.

Returns:

the software version.

Return type:

bytes

reset() None

Reset all edge counters.