Next: Function Reference, Previous: Sensors Overview, Up: Introduction [Contents][Index]
This example shows blinking the inbuilt LED on the Arduino board. Code is available by running:
edit examples/example_blink
This example uses in the builtin LED, so requires only a connection of the Arduino board to computer for communication.
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.
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});
Write a 0 value to the pin to turn it off.
writeDigitalPin (ar, led_pin, 0);
Write a 1 value to the pin to turn it on
writeDigitalPin (ar, led_pin, 1);
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
This example shows using I2C to communicate with a EEPROM chip. Code is available by running:
edit examples/example_i2c_eeprom
Using an Arduino UNO, the board should be configured with the following connections between the board and a 24XX256 EEPROM chip:
Connected to pin 5 of EEPROM
Connected to pin 6 of EEPROM
Connected to pin 8 of EEPROM
Connected to pin 1,2,3,4 of EEPROM
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.
Display the I2C terminals of the board:
getI2CTerminals(ar)
scanI2Cbus(ar)
The devices listed should contain 0x50, the address of the EEPROM chip.
eeprom = device (ar, "i2caddress", 0x50)
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 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)
This example shows using SPI to communicate with an mcp3002 10 bit ADC. Code is available by running:
edit examples/example_spi_mcp3002
Using an Arduino UNO, the board should be configured with the following connections between the board and a mcp3002 chip:
Connected to pin 1 (CS) of MCP3002
Connected to pin 5 (DI) of MCP3002
Connected to pin 6 (DO) of MCP3002
Connected to pin 7 (CLK) MCP3002
Connected to pin 8 (VDD) MCP3002
Connected to pin 4 (VSS) MCP3002
Connected from pin 2 of the MCP3002 to a LOW (< 5V) voltage to measure
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.
adc = device(ar, "spichipselectpin", "d10")
The d10 is the chip select pin connected from the Arduino to the MCP3002.
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]