Table of Contents

Class Arduino

Namespace
Bonsai.Arduino
Assembly
Bonsai.Arduino.dll

Represents an Arduino board communicating with the host computer using the Firmata protocol.

public sealed class Arduino : IDisposable
Inheritance
Arduino
Implements
Inherited Members

Constructors

Arduino(string)

Initializes a new instance of the Arduino class using the specified port name.

public Arduino(string portName)

Parameters

portName string

The port to use (for example, COM1).

Arduino(string, int)

Initializes a new instance of the Arduino class using the specified port name and baud rate.

public Arduino(string portName, int baudRate)

Parameters

portName string

The port to use (for example, COM1).

baudRate int

The serial baud rate.

Fields

DefaultBaudRate

Represents the default serial baud rate used to communicate with the Arduino.

public const int DefaultBaudRate = 57600

Field Value

int

DefaultSamplingInterval

Represents the default sampling interval for analog pins.

public const int DefaultSamplingInterval = 19

Field Value

int

Properties

IsOpen

Gets a value indicating the open or closed status of the Arduino object.

public bool IsOpen { get; }

Property Value

bool

MajorVersion

Gets the major version of the Firmata firmware reported by the board on initialization.

public int MajorVersion { get; }

Property Value

int

MinorVersion

Gets the minor version of the Firmata firmware reported by the board on initialization.

public int MinorVersion { get; }

Property Value

int

Methods

AnalogRead(int)

Returns the current value of the specified analog pin.

public int AnalogRead(int pin)

Parameters

pin int

The number of the analog pin to read.

Returns

int

A int value representing a digitized analog measurement.

AnalogWrite(int, int)

Writes an analog value as a PWM wave to the specified digital output pin.

public void AnalogWrite(int pin, int value)

Parameters

pin int

The number of the digital pin to write.

value int

A int value used to update the PWM signal.

Close()

Closes the port connection, sets the IsOpen property to false and disposes of the internal SerialPort object.

public void Close()

DigitalPortRead(int)

Reads the current state of all the digital pins in the specified port.

public byte DigitalPortRead(int port)

Parameters

port int

The number of the digital port (i.e. collection of 8 pins) to read.

Returns

byte

A byte value where each bit represents the state of one pin in the digital port.

DigitalPortWrite(int, byte)

Sets the state of all the digital output pins in the specified port simultaneously.

public void DigitalPortWrite(int port, byte value)

Parameters

port int

The number of the digital port (i.e. collection of 8 pins) to write.

value byte

A byte value where each bit will be used to set the state of one pin in the digital port.

DigitalRead(int)

Reads the current state of the specified digital input pin.

public bool DigitalRead(int pin)

Parameters

pin int

The number of the digital pin to read.

Returns

bool

true if the pin is HIGH; false if the pin is LOW.

DigitalWrite(int, bool)

Sets the state of the specified digital output pin.

public void DigitalWrite(int pin, bool value)

Parameters

pin int

The number of the digital pin to write.

value bool

true to set the pin HIGH; false to set the pin LOW.

GetPortNumber(int)

Gets the digital port number for the specified pin.

public static int GetPortNumber(int pin)

Parameters

pin int

The pin number for which to retrieve the digital port.

Returns

int

A int identifier for the digital port containing the specified pin.

I2CConfig(params byte[])

Configures I2C settings such as delay time and power pins.

public void I2CConfig(params byte[] args)

Parameters

args byte[]

The I2C configuration arguments. The first two bytes are used to configure the optional delay time, in microseconds, between writing to the I2C register, and reading the data from the device.

I2CRequest(int, I2CRequestMode, params byte[])

Sends a request to the I2C device with the specified address.

public void I2CRequest(int address, I2CRequestMode mode, params byte[] data)

Parameters

address int

The address of the slave device in the I2C bus.

mode I2CRequestMode

The read/write mode of the request.

data byte[]

The data payload for the I2C request.

I2CWrite(int, params byte[])

Writes a data payload to the I2C device with the specified address.

public void I2CWrite(int address, params byte[] data)

Parameters

address int

The address of the slave device in the I2C bus.

data byte[]

The data payload to write to the device.

Open(CancellationToken)

Opens a new serial port connection to the Arduino board.

public void Open(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A CancellationToken which can be used to cancel the operation.

PinMode(int, PinMode)

Sets the mode of an individual Arduino pin.

public void PinMode(int pin, PinMode mode)

Parameters

pin int

The number of the pin to configure.

mode PinMode

The pin mode.

ReportAnalog(int, bool)

Enables or disables reporting of analog pin values for the specified pin number.

public void ReportAnalog(int pin, bool state)

Parameters

pin int

The number of the pin to configure.

state bool

true if analog reporting for the pin should be enabled; false otherwise.

ReportDigital(int, bool)

Enables or disables reporting of digital pin changes for the specified digital port in the Arduino.

public void ReportDigital(int port, bool state)

Parameters

port int

The digital port to configure.

state bool

true if reporting of digital pin changes on the specified port should be enabled; false otherwise.

SamplingInterval(int)

Sets the sampling rate for reporting analog and I2C data in the main firmware loop.

public void SamplingInterval(int milliseconds)

Parameters

milliseconds int

The sampling interval, in milliseconds, between analog and I2C measurements.

SendSysex(byte, params byte[])

Sends the specified MIDI SysEx command using the specified arguments.

public void SendSysex(byte command, params byte[] args)

Parameters

command byte

A byte value indicating the SysEx command ID.

args byte[]

The optional extended payload sent to configure the SysEx command.

Events

AnalogInputReceived

Occurs when the Arduino object receives a new analog input event.

public event EventHandler<AnalogInputReceivedEventArgs> AnalogInputReceived

Event Type

EventHandler<AnalogInputReceivedEventArgs>

DigitalInputReceived

Occurs when the Arduino object receives a new digital input event.

public event EventHandler<DigitalInputReceivedEventArgs> DigitalInputReceived

Event Type

EventHandler<DigitalInputReceivedEventArgs>

SysexReceived

Occurs when the Arduino object receives a new MIDI SysEx message.

public event EventHandler<SysexReceivedEventArgs> SysexReceived

Event Type

EventHandler<SysexReceivedEventArgs>