chipKIT® Development Platform

Inspired by Arduino™

read values via SPI (uno32)

Created Sun, 28 Apr 2013 20:14:00 +0000 by chris2013


Sun, 28 Apr 2013 20:14:00 +0000

Hello there,

I´ve got a question regarding the SPI protocoll. I want to read digital values via SPI.

I´ve got an accelerometer-sensor with a resolution of 16 bit. The adress of the value in the sensor for the LSB is 0x4 and MSB 0x5.

How can I read the values from both adresses and store it in a variable??? Finally I want to write the values from the sensor to the LCD.

**summary of my questions:

  1. How can I read from an adress from a sensor
  2. How can I store it in a variable?**

I would be very glad if somebody could help me!!! :)

I´ve got got actually this syntax:

**//---------------------------------------------------------------------------------------- // include the library code: #include <LiquidCrystal.h> #include <SPI.h> #include <DSPI.h>

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//SPI Pins #define slaveSelect 10 //Chip Select Pin #define dataIn 11 //MOSI #define dataOut 12 //MISO #define spiClock 13 //SCK serial-clock

//Adressen #define Y_LSB 0x4 // Variable LSB von Y-Achse Beschleunigungssensor #define Y_MSB 0x5 // Variable MSB von Y-Achse -"- //#define temp_LSB 0x12; //#define temp_MSB 0x13;

char rev_in_byte; //int temp_in; unsigned long winkelY_LSB; unsigned long winkelY_MSB; unsigned long temp_winkel; unsigned long winkel;


//---------------------------------------------------------------------- void setup() {

pinMode(3,OUTPUT); // Versorgung 3.3 V

// set up the LCD's number of columns and rows: analogReference(DEFAULT);

lcd.begin(16, 2); // Print a message to the LCD. lcd.print(winkelY_LSB);

// byte clr;


pinMode(slaveSelect, OUTPUT); pinMode(dataOut, OUTPUT); pinMode(dataIn, INPUT); pinMode(spiClock, OUTPUT); digitalWrite(slaveSelect, HIGH);


Serial.begin(9600); delay(500);

byte test =0; }

//---------------------------------------------------------------------- void loop() {

if (Serial.available()>0)


analogWrite(3,255); // 255 für 3,3 V OUTPUT (PWM max. 8 bit)

float grad = float(winkelY_MSB);


lcd.setCursor(0, 1);





Jacob Christ

Sun, 28 Apr 2013 22:27:16 +0000

I think all you want to do is possible, but it would help if you posted the part number and manufacture for the sensor you are trying to use. A link to the datasheet would be good as well, but you may have trouble posting a link since you have not posted very often.


Jacob Christ

Tue, 30 Apr 2013 19:59:39 +0000

Your answer lies on page 22 of the link you sent me in a PM:

us8 chip_select = 7;  // set to what ever your chip select pin is wired to
void setup() {
void loop() {
  unsigned short int xaxis;

  SPI.transfer(0x45);  // Select address 5 (MSB) for read with odd parity bit
  xaxis = SPI.transfer(0x00);  // clock out dummy values to read back
  xaxis = xaxis &lt;&lt; 8;  // shift up the MSB
  SPI.transfer(0x40);  // Select address 4 (LSB) for read with odd parity bit
  xaxis |= SPI.transfer(0x00);  // clock out dummy values to read back

  Serial.print(xaxis, DEC);
  Serial.println(" ");
  delay(250); // do not overwhelm MPIDE terminal



Sun, 05 May 2013 11:34:25 +0000

Jacob thank you very much for your support.

but I have some questions:

  1. why do you use the adress0x45 & 0x40?? In the datasheet the adresses are 0x15 & 0x10 on page 22. ... cation.pdf

  2. Why do you store the adress 0x00 in the variable "xaxis"??

I tried every opportunity of adresses (0x10 & 0x15), but i couldn´s see any function.

Could it be that the clock speed is wrong? The senosr needs 8 MHz. My syntax is:

int spd; spd = 8000000; void setSpeed(uint32_t spd);

Is there something wrong?


Jacob Christ

Sun, 05 May 2013 17:23:51 +0000

Maybe we should set up a Google hangout to talk about this. I think you have some fundamental misunderstanding about how SPI works. Also code provided was not tested it was just based on the data sheet.