Arduino Yun with SPI ?

Hello all, how do i connect the ADXL345 onto Arduino Yun using SPI communication ? I cant seems to get it.

Previously I was using Arduino Uno which uses D10,11,12,13 for CS, SDA, SDO, SCL for the SPI communication.

motomage:
Previously I was using Arduino Uno which uses D10,11,12,13 for CS, SDA, SDO, SCL for the SPI communication.

And how are you doing it now?

Be aware that if you are using the hardware SPI interface pins (MISO, MOSI, SCK - the ADXL345 seems to use non-standard pin names) are on the 6 pin ICSP header, not on the regular shield connectors.

ShapeShifter:
And how are you doing it now?

Be aware that if you are using the hardware SPI interface pins (MISO, MOSI, SCK - the ADXL345 seems to use non-standard pin names) are on the 6 pin ICSP header, not on the regular shield connectors.

Hi, actually I need to use three ADXL to measure three different location.

Initially, I started off using 1 ADXL using the I2C protocol, so I thought i could add another one using this reference. But I dont know what is wrong, it doesnt work and I realised I have only two addreess for only 2 ADXL, thats why I decided to use SPI for the third ADXL. If the SPI wiring and programming is easier, I would glady use SPI for all three.

Would really appreciate any help!

(I have little to none wiring and programming skills)

motomage:
Hi, actually I need to use three ADXL to measure three different location.

Different locations? How far are they? Neither SPI nor I2C are really appropriate for long distances (I don't tend to use them if I'm going off of the main board.)

I would glady use SPI for all three.

I would also use SPI for all three connections. I don't like I2C, and avoid it when possible, as it is been less reliable that SPI in my experience. To wire it up, the MISO, MOSI, and SCK lines go in parallel to each device, and each device gets its own SS line.

The ADXL uses strange names for the SPI pins, since they also are used for I2C. The pin mapping is: (ICSP is the six pin header - 2 rows of 3 pins)

  • MOSI - Master In Slave Out - ADXL calls this SDO, Yun has it on ICSP Pin 4
  • MISO - Master Out Slave In - ADXL calls this SDA, Yun has it on ICSP Pin 1
  • SCK - Serial ClocK - ADXL calls this SCL, Yun has it on ICSP pin 3
  • SS - Slave Select - ADXL calls this CS (Chip Select), Yun has no dedicated pin, use any digital data pin

So, pins 1, 3, and 4 of the six pin ICSP header go to the SDA, SCL, and SDO pins of each of the ADXL devices (all connected in parallel.) Then choose three available data pins on the Yun (not D0 or D1) and connect one to each of the CS pins on the ADXL devices (three separate point to point connections.)

To use it, set up the SPI interface once. Set each of the three pins you selected as chip selects as digital outputs, and set them all HIGH. Then, when you want to talk to one of the ADXL pins, set the corresponding CS line LOW, do the operation, and set the CS line high. It's just like the sample code, except that you have three different CS pins: only one should go LOW at a time (the others stay HIGH) and the ADXL that is connected to the line you put LOW is the one that will respond.

I'm sure I didn't explain this very well, so ask questions...

I am getting this error using the simple code from Sparksfun tutorial for one sensor.

//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>
#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>

//Assign the Chip Select signal to pin 7.
int CS=7;

//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D;	//Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;	//X-Axis Data 0
char DATAX1 = 0x33;	//X-Axis Data 1
char DATAY0 = 0x34;	//Y-Axis Data 0
char DATAY1 = 0x35;	//Y-Axis Data 1
char DATAZ0 = 0x36;	//Z-Axis Data 0
char DATAZ1 = 0x37;	//Z-Axis Data 1

//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
float x,y,z;
float x1, y1, z1; //Acceleration variable


void setup(){ 
  Bridge.begin();
  Console.begin();  // start serial for output
  while (!Console) ; // stops the program until Serial Monitor is opened
  FileSystem.begin();
  //Initiate an SPI communication instance.
  SPI.begin();
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //Set up the Chip Select pin to be an output from the Arduino.
  pinMode(CS, OUTPUT);
  //Before communication starts, the Chip Select pin needs to be set high.
  digitalWrite(CS, HIGH);
  
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeRegister(DATA_FORMAT, 0x01);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeRegister(POWER_CTL, 0x08);  //Measurement mode  
}

void loop(){
  //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  //The results of the read operation will get stored to the values[] buffer.
  readRegister(DATAX0, 6, values);

  x = ((int)values[1]<<8)|(int)values[0];
  //The Y value is stored in values[2] and values[3].
  y = ((int)values[3]<<8)|(int)values[2];
  //The Z value is stored in values[4] and values[5].
  z = ((int)values[5]<<8)|(int)values[4];
  
  //Print the results to the terminal.
  Console.print(x/100);
  Console.print(',');
  Console.print(y/100);
  Console.print(',');
  Console.println(z/100);
    
  delay(10); 
}

//This function will write a value to a register on the ADXL345.
//Parameters:
//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress);
  //Transfer the desired register value over SPI.
  SPI.transfer(value);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(CS, HIGH);
}

omg, i dont see any more yun in the "Port" like I usually do..

Oh dear, is it something bad? L13 Led is also blinking red too..

Unplug your Yun to cycle power, and let it reboot. Then choose the Yun again from the Port menu. I've found that it sometimes loses the connection and doesn't appear. Cycling power usually helps, rarely do I have to resort to restarting the computer (but I have reached that point once or twice when the USB was really acting up.)

actually can I just transfer via usb?

*Edit, I cant see my Yun port in the IDE anymore…

AVR device not responding

i cant transfer via wifi or usb!