Hi All,
I am trying send my sensor's data connected to SPI ports at Arduino to Serial to USB port and see the data on hyper terminal or serial monitor!
can anyone help me to write a code for such scenario
Thanks!!
Hi All,
I am trying send my sensor's data connected to SPI ports at Arduino to Serial to USB port and see the data on hyper terminal or serial monitor!
can anyone help me to write a code for such scenario
Thanks!!
can anyone help me to write a code for such scenario
Here's a start:
void setup()
{
Serial.begin(115200);
// Add some code here
}
void loop()
{
// Add some code here to read the data
Serial.print("Here's the data: ");
Serial.println(theData);
}
Be sure to properly define theData.
Post a link to the sensor you are trying to read from, if you are having difficulties reading from it.
Post a schematic showing how YOU have connected the sensor to the Arduino.
Post YOUR code.
Fun reading:
Here's the link to the sensor that I am using
http://www.analog.com/static/imported-files/data_sheets/ADIS16210.
Pin Layout is like this:
UNO SENSOR
DATAOUT 11//MOSI DIN PIN 4
DATAIN 12//MISO DOUT PIN 6
SPICLOCK 13//sck SCLK PIN 3
SLAVESELECT 10//ss CS PIN 2
I am totally confused how to access the registers of sensors
Hers's my cod up till now. Please help me write the part to read registers
//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADIS16210.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char XACCL_OUT = 0x04; //X-Axis Data 0
char YACCL_OUT = 0x06; //Y-Axis Data 0
char ZACCL_OUT = 0x08; //Z-Axis Data 0
//This buffer will hold values read from the ADIS16210 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
void setup(){
//Initiate an SPI communication instance.
SPI.begin();
//Configure the SPI connection for the ADIS16210.
SPI.setDataMode(SPI_MODE3);
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//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);
}
void loop(){
//Reading 6 bytes of data starting at register AXACCL_OUT will retrieve the x,y and z acceleration values from the ADIS16210.
//The results of the read operation will get stored to the values[] buffer.
readRegister(XACCL_OUT, 6, values);
//The ADIS16210 gives 16-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
//The X value is stored in values[0] and values[1].
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.
Serial.print(x);
Serial.print(',');
Serial.print(y);
Serial.print(',');
Serial.println(z);
delay(3000);
}
Here's the link to the sensor that I am using
The page cannot be found
Khyzer:
Hers's my cod up till now.
Please edit your post, select your code, and hit the # button to surround it in code tags. I can't get my head around all the smiley faces that appear in it currently.
Sir,
I am extremely sorry for any inconvenience caused, here's the code and link for datasheet! I´m using a code for the ADXL345 accelerometer from ADXL345 Hookup Guide - SparkFun Learn to test my accelelrometer. I already chance some of the registers adresses with the ones on the ADIS16210 datasheet.
CODE QUESTION? - this appear in the code...
//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;
I know that this lines correspond to the ADXL345 accelerometer datasheet, but don´t know the equivalent for the ADIS16210. I Don´t undestand what this lines are for and if I need to use it for my accelerometer.
First time trying with an accelerometer, not quite easy at first. Any help, tips or docs will be appreciate.
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADIS16210.
char XACCL_OUT = 0x04; //X-Axis Data 0
char YACCL_OUT = 0x06; //Y-Axis Data 0
char ZACCL_OUT = 0x08; //Z-Axis Data 0
//This buffer will hold values read from the ADIS16210 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
}
You can't take code for some OTHER accelerometer and expect it to work with YOUR accelerometer.
Read page 7 of the data sheet. To read a single register:
digitalWrite(SlaveSelect, LOW);
SPI.transfer(RegisterAddress); // 0x04 for XACCL_OUT, 0x06 for YACCL_OUT, 0x08 for ZACCL_OUT
SPI.transfer(0);
byte resultHigh = SPI.transfer(0);
byte resultLow = SPI.transfer(0);
digitalWrite(SlaveSelect, HIGH);
unsigned int result = (resultHigh << 8) ! resultLow;
The addresses is followed by a 0 byte. The two bytes of result come out as the next two bytes of 0 are sent.
If you want to read all three registers:
digitalWrite(SlaveSelect, LOW);
SPI.transfer(0x04); // Address of XACCL_OUT
SPI.transfer(0);
byte xresultHigh = SPI.transfer(0x06); // Address of YACCL_OUT
byte xresultLow = SPI.transfer(0);
byte yresultHigh = SPI.transfer(0x08); // Address o ZACCL_OUT
byte yresultLow = SPI.transfer(0);
byte zresultHigh = SPI.transfer(0);
byte zresultLow = SPI.transfer(0);
digitalWrite(SlaveSelect, HIGH);
unsigned int xresult = (xresultHigh << 8) ! xresultLow;
unsigned int yresult = (yresultHigh << 8) ! yresultLow;
unsigned int zresult = (zresultHigh << 8) ! zresultLow;
Hi John!
Thank you so much for your concerned reply! I really appreciate your help! However I tried your suggestion and couldn't get any output. What does following statement means? unsigned int result = (resultHigh << 8) ! resultLow;
When I compile program it gives me following error:
sketch_oct17a_SPI.cpp: In function 'void loop()':
sketch_oct17a_SPI:46: error: expected ',' or ';' before '!' token
Please help me to get on to the right path! thanks in advance!!!
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADIS16210.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char XACCL_OUT = 0x04; //X-Axis Data 0
char YACCL_OUT = 0x06; //Y-Axis Data 0
char ZACCL_OUT = 0x08; //Z-Axis Data 0
//This buffer will hold values read from the ADIS16210 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
void setup(){
//Initiate an SPI communication instance.
SPI.begin();
//Configure the SPI connection for the ADIS16210.
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//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);
}
void loop()
{
digitalWrite(CS, LOW);
SPI.transfer(0x04); // 0x04 for XACCL_OUT, 0x06 for YACCL_OUT, 0x08 for ZACCL_OUT
SPI.transfer(0);
byte resultHigh = SPI.transfer(0);
byte resultLow = SPI.transfer(0);
digitalWrite(CS, HIGH);
unsigned int result = (resultHigh << 8) ! resultLow;
Serial.print(result);
}
He means | rather than !
Hello Mr Nick!
Thanks for your clarification! I have already tried with '|' operand but still not getting any result! I only see 1024 trail at the output no effect of changing sensor position! It even generates 1024 when not connected to sensor! I have checked the pin outs also. Please suggest further! Thanks
Arduino Pin ADXL345 Pin
10 CS
11 DIN
12 DOUT
13 SCL
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADIS16210.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char XACCL_OUT = 0x04; //X-Axis Data 0
char YACCL_OUT = 0x06; //Y-Axis Data 0
char ZACCL_OUT = 0x08; //Z-Axis Data 0
//This buffer will hold values read from the ADIS16210 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
void setup(){
//Initiate an SPI communication instance.
SPI.begin();
//Configure the SPI connection for the ADIS16210.
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//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);
}
void loop()
{
digitalWrite(CS, LOW);
SPI.transfer(0x04); // 0x04 for XACCL_OUT, 0x06 for YACCL_OUT, 0x08 for ZACCL_OUT
SPI.transfer(0);
byte resultHigh = SPI.transfer(0);
byte resultLow = SPI.transfer(0);
digitalWrite(CS, HIGH);
unsigned int result = (resultHigh << 8) | resultLow;
Serial.print(result);
}
According to the datasheet:
SCLK Rate ? 830 kHz (Maximum serial clock rate)
Assuming you are running the processor at 16 MHz and you have this line:
SPI.setClockDivider(SPI_CLOCK_DIV16);
... then you will be running the clock at 1 MHz (which you would be if you have my setup, because I measured it).
Try:
SPI.setClockDivider(SPI_CLOCK_DIV32);
Dear Nick!
I have this clock rate in my mind (should be <830kHz) I have also tried using /32 but the result is same, no output! I am also measuring the clock frequency. Please suggest!
Thanks!
Stick to the /32 to stay in spec.
Do you have an oscilloscope or logic analyzer? Is the device responding at all? 1024 is not exactly all zeroes, nor is it all ones, so it sounds like it might be sending something.
Dear Nick,
Yes I am using Oscilloscope to look out for frequency. When I use /16 I get exact 1MHz frequency clock but when I use /32 I get strange values of frequency continuously varying, not stable!
Thanks!
That doesn't surprise me because the clock line is raised in bursts. What is more interesting is, does the device put out any sort of meaningful data on the DOUT line?
Hello Nick,
I am afraid still same 1024 output, nothing meaningful can be seen
On my oscilloscope I can see 4.6V signal at CS pin continuously and 2.6V signal at DOUT pin and 0V signal at DIN pin!
Please suggest if there are any changes need to be made to make it work!
Highly appreciate your support!
Thanks!
//Add the SPI library so we can communicate with the ADIS16210 sensor
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This is a list of some of the registers available on the ADIS16210.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char XACCL_OUT = 0x04; //X-Axis Data 0
char YACCL_OUT = 0x06; //Y-Axis Data 0
char ZACCL_OUT = 0x08; //Z-Axis Data 0
//This buffer will hold values read from the ADIS16210 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
void setup(){
//Initiate an SPI communication instance.
SPI.begin();
//Configure the SPI connection for the ADIS16210.
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV32);
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//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);
}
void loop()
{
digitalWrite(CS, LOW);
SPI.transfer(0x04); // Address of XACCL_OUT
SPI.transfer(0);
byte xresultHigh = SPI.transfer(0x06); // Address of YACCL_OUT
byte xresultLow = SPI.transfer(0);
byte yresultHigh = SPI.transfer(0x08); // Address o ZACCL_OUT
byte yresultLow = SPI.transfer(0);
byte zresultHigh = SPI.transfer(0);
byte zresultLow = SPI.transfer(0);
digitalWrite(CS, HIGH);
unsigned int xresult = (xresultHigh << 8) | xresultLow;
unsigned int yresult = (yresultHigh << 8) | yresultLow;
unsigned int zresult = (zresultHigh << 8) | zresultLow;
Serial.print(xresult);
Serial.print('\n');
delay(2000);
Serial.print(yresult);
Serial.print('\n');
delay(2000);
Serial.print(zresult);
Serial.print('\n');
delay(2000);
}
Khyzer:
On my oscilloscope I can see 4.6V signal at CS pin continuously and 2.6V signal at DOUT pin and 0V signal at DIN pin!
Can you post a screenshot? A 0V signal doesn't give you 1024.
Khyzer:
Yes I am using Oscilloscope ...
I didn't mean a screenshot of your sketch. I mean a screenshot of the oscilloscope, measuring SCK and DOUT (at least). Measuring voltages, when the data line is being pulsed at a rate of 500 kHz isn't going to tell you a heap.