8 Examples

8.1 Blinking an LED

This example shows blinking the inbuilt LED on the Arduino board. Code is available by running:

edit examples/example_blink

Hardware setup

This example uses in the builtin LED, so requires only a connection of the Arduino board to computer for communication.

Create an Arduino object

ar = arduino ();

If you have more than one Arduino board connected, you may need to specify the port in order to connect to the correct device.

Query Device for pins connected to builtin LED

The pin connected to the Arduino UNO built in led if D13.

led_pin = "d13";

The connected pins can be queried programatically if desired.

pins = getLEDTerminals (ar);

Connected to a Arduino UNO would return a list pins containing only one item ’13’.

The terminal number can be converted to a pin using getPinsFromTerminals:

led_pin = getPinsFromTerminals (ar, pins{1});

Turn the LED off

Write a 0 value to the pin to turn it off.

writeDigitalPin (ar, led_pin, 0);

Turn the LED on

Write a 1 value to the pin to turn it on

writeDigitalPin (ar, led_pin, 1);

Making the LED blink

Add a while loop with a pause between the changes in the pin state to blink.

while true
  writeDigitalPin (ar, led_pin, 0);
  pause (0.5)
  writeDigitalPin (ar, led_pin, 1);
  pause (0.5)
endwhile

8.2 Using I2C to communicate with an EEPROM

This example shows using I2C to communicate with a EEPROM chip. Code is available by running:

edit examples/example_i2c_eeprom

Hardware setup

Using an Arduino UNO, the board should be configured with the following connections between the board and a 24XX256 EEPROM chip:

A4

Connected to pin 5 of EEPROM

A5

Connected to pin 6 of EEPROM

5V

Connected to pin 8 of EEPROM

GND

Connected to pin 1,2,3,4 of EEPROM

Create an Arduino object

ar = arduino ();

If you have more than one Arduino board connected, you may need to specify the port in order to connect to the correct device.

Query I2C pins

Display the I2C terminals of the board:

getI2CTerminals(ar)

Scan the arduino for the connected device

scanI2Cbus(ar)

The devices listed should contain 0x50, the address of the EEPROM chip.

Create an I2C object to communicate to the EEPROM

eeprom = device (ar, "i2caddress", 0x50)

Write data to the EEPROM

The EEPROM expects the first byte to be the page number, the second the offset, followed by data, so to write 1 2 3 4, starting address 0 (page 0, offset 0):

write(eeprom, [0 0 1 2 3 4])

Reading from the EEPROM

Reading from the EEPROM requires first writing the address to read from, in this case, if we want to read the 3, 4, this would be page 0, offset 2:

write(eeprom, [0 2])

Next read the 2 bytes:

data = read(eeprom, 2)

8.3 Using SPI to communicate with a mcp3002 10 bit ADC

This example shows using SPI to communicate with an mcp3002 10 bit ADC. Code is available by running:

edit examples/example_spi_mcp3002

Hardware setup

Using an Arduino UNO, the board should be configured with the following connections between the board and a mcp3002 chip:

D10

Connected to pin 1 (CS) of MCP3002

D11

Connected to pin 5 (DI) of MCP3002

D12

Connected to pin 6 (DO) of MCP3002

D13

Connected to pin 7 (CLK) MCP3002

VCC

Connected to pin 8 (VDD) MCP3002

GND

Connected to pin 4 (VSS) MCP3002

Analog input

Connected from pin 2 of the MCP3002 to a LOW (< 5V) voltage to measure

Create an Arduino object

ar = arduino ();

If you have more than one Arduino board connected, you may need to specify the port in order to connect to the correct device.

Create an SPI object to communicate to the MCP3002

adc = device(ar, "spichipselectpin", "d10")

The d10 is the chip select pin connected from the Arduino to the MCP3002.

Read the ADC

The MCP3002 expects specific commands in order to read a channel.

For illustration for the command to read chan 0 in single ended mode:

command (bits) in MSB mode to device:
[START SGL ODN MSBF X X X X] [ X X X X X X X X ] 
   1    1   0    1   1 1 1 1    1 1 1 1 1 1 1 1 
      [chan 0 ] MSB    
data back:
   X    X  X     X   X 0 D D    D D D D D D D D

D is a output data bit

X is a don’t care what value is input/output

The first byte contains the command and start of the data read back, the second bytes is written to clock out the rest of the ADC data.

In hex, this corresponds to 0xDF 0xFF,

data = writeRead(adc, [hex2dec("DF") hex2dec("FF")])

Of the data returned, the last 10 bits is the actual data, so convert data to a 16 bit value:

val = uint16(data(1))*256 + uint16(data(2))

Then bitand it to remove the non value parts, to get the ADC value:

val = bitand (val, hex2dec('3FF'))

To make the value correspond to a voltage it needs to be scaled as 0 will be 0 Volts, 1023 will be 5 Volts.

volts = double(val) * 5.0 / 1023.0;

Next: Function Reference, Previous: Sensors Overview, Up: Introduction   [Contents][Index]