4 Basic Input and Output Overview

Basic input and output can be performed on a connected arduino device using by calling the read and write functions for a specific named pin on the arduino.

A list of available pins can get found from the pins property of the connected arduino object and are also displayed as part of the default shown properties:

ar = arduino();
% get the pin names
pins = ar.availablepins

Pin generally follow a naming scheme of D<number> for digital pins and A<number> for analog pins.

Digital pins can be used to read and write digital data, but can not read analog voltages. Analog pins can perform digital I/O as well as reading voltages.

4.1 Performing Digital I/O

A pin’s digital logic value can be true (1) or false (0) and can be set using the writeDigitalPin function.

The following example attempts to set the D2 pin of the connected arduino object "ar" to true, waits 5 seconds and then sets it to false:

writeDigitalPin (ar,  "d2", true);
pause 5
writeDigitalPin (ar,  "d2", false);

Using the readDigitalPin will read the current logic state of the pin.

value = readDigitalPin (ar,  "d2");

4.2 Performing Analog Input

For analog pins, the voltage level can be read using a analog to digital conversion and will return a voltage level between 0 and the boards voltage (nominally 5V):

value = readVoltage (ar,  "a0");

The raw digital value of the pin can also be read instead of a voltage, giving a value between 0 and 2^x where x is the number of bits used by the analog to digital converter.

value = readAnalogPin (ar,  "a0");

Next: Protocol based I/O Overview, Previous: Connecting to an arduino, Up: Introduction   [Contents][Index]