5 Protocol based I/O Overview

The arduino toolkit supports more complex I/O for SPI, I2C, Servo control and more.

5.1 SPI communication

SPI communication can be performed by creating a SPI device object and then calling the writeRead function:

spi = device (ar,  "spichipselectpin", "d2");

The function call expects a connected arduino object as the first argument, followed by the chip select pin of the SPI device.

After a device is created, a write to device followed by read can can be made using the writeRead function:

spi = device (ar,  "spichipselectpin", "d2");
data = writeRead (spi,  100);

5.2 I2C communication

I2C communication can be performed by creating an I2C device object for a specific I2C address.

The following example creates an I2C device that will communicate with a I2C device at address 100"

i2c = device (ar,  "i2caddress", 100);

After creating an I2C device, data can be read and written using read, write, readRegister and writeRegister. The data to send and receive will be device dependent.

5.3 Servo communication

Servo communication can be performed after creating a servo device object to operate on a PWM pin:

servoobj = servo(ar, "d9", "minpulseduration", 1.0e-3, ...
  "maxpulseduration", 2e-3);

The servo function expects the connected arduino object and the PWM pin that the servo is connected to. Optional properties can be specified to control the setup of device.

In the example, the min and max pulse width values are set.

Using the servo object the current position and be read or set with values ranging between 0 to 1, with 0 being the minimum pulse width and 1 being the maximum.

The following example sets the servo to its middle position.

servoobj = servo(ar, "d9", "minpulseduration", 1.0e-3, ...
  "maxpulseduration", 2e-3);

writePosition (servoobj, 0.5);

5.4 Shift Registers

A shift register can be controlled by creating a shiftRegister object:

registerobj = shiftRegister(ar, '74hc164', "d2", "d3");

The parameters required are dependent on the type of shift register created.

Once a register object has been created, it can be read and written to using the read and write functions.

5.5 Rotary Encoders

A rotary encoder can be created by creating a rotaryEncoder object.

encoder = rotaryEncoder(ar, "d2", "d3", 180);

Using the created object, the rotary encoder value and speed can be read.

5.6 Ultrasonic Sensors

An ultrasonic sensor can be read by creating an ultrasonic object.

sensor = ultrasonic(ar, "d9", "d10");

Using the created object, the sensor distance and echo time and be read.

5.7 Serial communication

Serial communication can be performed on devices that support multiple serial devices such as the leonardo and mega2560 boards. The communications port to Octave is reserved and can not be used as a user controlled communications port.

Serial communication can be performed by creating a serial device object and then calling the read and write functions:

ser = device (ar,  "serial", 1);

The function call expects a connected arduino object as the first argument, followed "serial" and serial id.

After a device is created, the device can be read and written:

ser = device (ar,  "serial", 1);
write(ser, "hello");
data = read(ser, 100);

Next: Addons Overview, Previous: Basic Input and Output Overview, Up: Introduction   [Contents][Index]