0

I want to interface with an AD7314 Temperature Sensor. I want to only read the temperature value from the sensor and output the value on to 8 LEDs connected to the Arduino. But so far I have not been able to read from the sensor. The datasheet says that the temperature readings are in 10 bit 2's complement, so I plan to convert to 8 bit before putting it out on the LEDs. Here is my code so far.

I am new to SPI and clearly don't understand the temperature sensor's datasheet. Please can anyone elaborate or explain where I am going wrong?

Note: I have chosen pin A0 (PIN 23 of ATMEGA328P) as slave select and not pin 10

#include <SPI.h>

byte val;

void setup (void) {
  pinMode(0, OUTPUT); //LED 1
  pinMode(1, OUTPUT); //LED 2
  pinMode(5, OUTPUT); //LED 3
  pinMode(6, OUTPUT); //LED 4
  pinMode(7, OUTPUT); //LED 5
  pinMode(8, OUTPUT); //LED 6
  pinMode(9, OUTPUT); //LED 7
  pinMode(10, OUTPUT);//LED 8
  pinMode(11, OUTPUT); // MOSI
  pinMode(12, INPUT); //MISO
  pinMode(13, OUTPUT); //CLOCK PIN
  digitalWrite(A0, HIGH);  // ensure Slave select stays high
  SPI.begin ();
  Serial.begin(9600);
} // end of setup

void loop (void) {
  // enable Slave Select
  digitalWrite(A0, LOW); // enable communication with slave
  delay (0.425);// wait for conversion
  val = SPI.transfer(0x00);// read from sensor
  Serial.println(val); // display reading on serial monitor
  // disable Slave
  digitalWrite(A0, HIGH);
} 

1 Answer 1

2
delay (0.425);// wait for conversion 

You can't delay a fraction of a millisecond. You can use delayMicroseconds though:

delayMicroseconds(425);

However, you don't need to sleep at all.

The conversion clock for the part is internally generated so no external clock is required except when reading from and writing to the serial port. In normal mode, an internal clock oscillator runs the automatic conversion sequence. A conversion is initiated every 400 ms. At this time, the part wakes up and performs a temperature conversion. This temperature conversion typically takes 25 ms, at which time the part automatically shuts down. The result of the most recent temperature conversion is available in the serial output register at any time.

You have your chip select pin backwards:

To ensure that the serial port is reset properly after power-up, the CE must be at a logic low before the first serial port access. The serial clock is active only when CE is high. For correct data synchronization, it is important that the CE be low when the serial port is not being accessed

So it should start LOW, and be set HIGH before you read, and back to LOW afterwards.

You need to read 2 bytes, not one, then combine them in the right way to make a proper 8-bit value:

digitalWrite(A0, HIGH);
uint8_t b1 = SPI.transfer(0x00);
uint8_t b2 = SPI.transfer(0x00);
digitalWrite(A0, LOW);

int8_t = (b1 << 1) | (b2 >> 7);

The reason for that is that the first bit of the first byte is a "leading zero" so you need to discard it, then add the first bit of the second byte as the last bit of your value, discarding the remainder of the second byte, since you specified only an 8-bit resolution in your question.

6
  • Just for my clarification, i have 2 questions: 1) uint8_t SPI.transfer(0x00), does this statement mean that the value being read is an unsigned integer and not a byte why so ? 2) SPI.transfer(0x00), does the 0x00 mean that value being read or transferred is 0 (decimal) regardless ? Commented Dec 27, 2017 at 20:41
  • @user7187418 uint8_t is an 8 bit unsigned value. Better known as a byte. The 0x00 is the value being sent by SPI which happens (and is ignored) at the same time as receiving. It's how SPI works.
    – Majenko
    Commented Dec 27, 2017 at 20:43
  • could i replace the 0x00 with an unsigned integer variable, lets say x1 or would that actually discard the value being read ? Commented Dec 27, 2017 at 20:51
  • You can replace it with whatever you like. It will make no difference. The ADC chip ignores it. With SPI you can only transmit and receive at the same time, so to receive you must send something, even if it's ignored.
    – Majenko
    Commented Dec 27, 2017 at 20:52
  • If the output was to be changed from 8 bit to 6 bit out, would the bit shift change to (b1<<0) | (b2<<5) ? (i am new to bit shifting and digital logic, if that helps) Commented Dec 27, 2017 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.