Any AVR microcontroller based board which follows the standard arduino schematic and is flashed with the arduino boot-loader can be called an arduino board. All arduino boards should be compatible with the arduino IDE which can be used to program the arduino boards. The arduino IDE is also open source and anybody can contribute their libraries to the arduino. The arduino is refered to as open source hardware, since the standard schematic is open to everyone and anybody can make their own version of arduino board following the standard schematic.Since the Arduino board can act as a stand-alone system it should have capabilities to take inputs process the input and then generate a corresponding output. It is through these inputs and outputs that the Arduino as a system can communicate with the environment. The Arduino boards can communicate with other devices using digital input/output analog input/output standard communication ports like USART, IIC, USB, SPI etc.
The Arduino IDE is very simple to use and anyone has the basic knowledge of C coding can start with it. The easy to IDE and hardware makes the Arduino an easy prototyping platform. The IDE has several inbuilt functions and libraries to access the internal hardware modules as well as the external modules like LCD, SDcard etc. This particular project explains how to initialize and send and receive the data with the internal SPI module with the help of the SPI library provided by the Arduino IDE.
In this particular project the Arduino pro-mini board is used since it is very small and bread board compatible. It occupies only small area of a breadboard and any kind of connectors can be soldered into the periphery of the board to connect it with the breadboard. The Arduino pro-mini board has digital pins marked as 2, 3, 4 up to 13. Among the digital pins four pins namely 10, 11, 12 and 13 can be configured as SS, MOSI, MISO and SCK. The image of the Arduino pro-mini board and the Arduino IDE version 1.0.3 for windows used in this project is shown in the following image;
Fig. 2: Typical Arduino Pro-Mini Board
Fig. 3: Arduino IDE Software Window
Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the PC through the USB port of the PC.
Fig. 4: External USB to TTL converter board for programming Arduino and serial communication
It is assumed that the reader has gone through the project how to get started with the arduino and done all the things discussed in it.The code written for this project uses functions from the library <SPI.h> for initializing the SPI module and sending and receiving data using it. The functions namely SPI.begin() is used to initialize the SPI module and the function SPI.transfer() used to transfer a byte of data and receive a byte of data at the same time.
SPI.begin()
This function initializes the SPI module of the Arduino board and configures the corresponding digital I/O pins as SPI pins. The function always initializes the Arduino as SPI master device with the pin number 10 as SS, pin number 11 as MOSI, pin number 12 as MISO, and pin number 13 as SCK pin. The function has no parameters and it returns nothing.
SPI.transfer()
The function SPI.transfer() is used to transfer a byte of data to the slave device and to receive a byte of data from the slave device at the same time. The function takes a parameter which is the byte of data to be transmitted and the return from the function is a byte which is the data byte received from the slave device. Whenever only data transfer is required the return value can be ignored and whenever there is data to be read from the slave only the parameter passed to the function can be any arbitrary value.
The data transfer using the code is monitored in the Serial monitor window with the help of the serial communicating functions written in the code. The functions used in this projects are namely Serial.begin(), Serial.print(),Serial.println(), Serial.available(),Serial.read() and Serial.write(). The details of these functions and similar functions for the serial communication are already discussed in previous projects on how to do serial communication with the Arduino, how to send and receive serial data using arduino, how to do serial debugging with the Arduino.
There is no slave device used in this project but the pin number 11 and 12 which are MOSI and MISO are shorted together so that whatever data send will be received at the same time. The function SPI.begin() is used to initialize the SPI module and the function SPI.transfer() is used to transfer bytes of a particular string and receive the same bytes which are then displayed in the Serial monitor using the serial communication functions.
When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino and observe the data bytes with the help of Serial monitor as explained in the project on how to do serial debugging with the Arduino.
Project Source Code
### /*================================= EG LABS ======================================= Loop Back test for the SPI port of the Arduino The circuit: * LED attached from pin 6 to ground through a 1K resistor ================================== EG LABS =======================================*/ #include <SPI.h> // including the SPI library char outByte [20] = "ENGINEERS GARAGE"; // string to be send and received via SPI port int led = 6; // variable which holds the pin number at which the LED is attached char inByte; // variable which stores the value of the byte received from SPI bus int i = 0; void setup() { pinMode(led, OUTPUT); // setting the LED pin as output Serial.begin(9600); // initializing the serial port at 9600 baud rate SPI.begin(); // initialize the SPI port as master delay(100); } void loop() { digitalWrite(led, HIGH); for(i = 0; outByte [i] != '