i have sensor trying communicate on spi using arduino uno. first time using spi , i'm experiencing problems i'm hoping due misunderstanding on part. have written simple code perform 1 basic operation: turn on sensor's fan. arduino being powered through 2.1mm barrel jack 1.5a dc adapter. 5v, gnd, mosi, miso, ss , sck pins on ardunio connected corresponding pins on sensor. sensor receives power arduino, drawing 250ma (with fan on).
the manufacturer provides list of commands corresponding each function of sensor. turn on fan, command list gives following information:
command byte: 0x03
bytes: 0x03, 0x00
bytes in: 0xf3, 0x03
time between current , next byte: 1.7ms, na
note: 1ms adequate delay between command byte , following byte
based on understanding of this, turn on fan, need send command byte (0x03), wait 1.7ms , send byte (0x00). code doing follows:
#include <spi.h>
const int chipselectpin = 10;
void setup() {
serial.begin(9600);
spi.begin();
pinmode(chipselectpin, output);
spi.setclockdivider(spi_clock_div32); //set clock 500khz
spi.setdatamode(spi_mode1);
spi.setbitorder(msbfirst);
delay(10000); // let sensor settle 10 seconds
digitalwrite(chipselectpin, low);
indata = spi.transfer(0x03); //command byte fan on
serial.println(indata);// value returned should 0xf3
delay(2);
indata = spi.transfer(0x00); //turn on fan
digitalwrite(chipselectpin, high);
serial.println(indata);// value returned should 0x03
}
void loop() {
}
when send command byte, returned value (which gets printed terminal) 243 corresponds expected hex value of 0xf3. however, following command not turn on fan , second returned value 231 not correspond expected hex value of 0x03.
i don't think it's hardware issue because @ least 1 of returned values correct , because sensor work when used different platform. experimented different delays between bytes. if has insights i'd appreciate help.
the manufacturer provides list of commands corresponding each function of sensor. turn on fan, command list gives following information:
command byte: 0x03
bytes: 0x03, 0x00
bytes in: 0xf3, 0x03
time between current , next byte: 1.7ms, na
note: 1ms adequate delay between command byte , following byte
based on understanding of this, turn on fan, need send command byte (0x03), wait 1.7ms , send byte (0x00). code doing follows:
#include <spi.h>
const int chipselectpin = 10;
void setup() {
serial.begin(9600);
spi.begin();
pinmode(chipselectpin, output);
spi.setclockdivider(spi_clock_div32); //set clock 500khz
spi.setdatamode(spi_mode1);
spi.setbitorder(msbfirst);
delay(10000); // let sensor settle 10 seconds
digitalwrite(chipselectpin, low);
indata = spi.transfer(0x03); //command byte fan on
serial.println(indata);// value returned should 0xf3
delay(2);
indata = spi.transfer(0x00); //turn on fan
digitalwrite(chipselectpin, high);
serial.println(indata);// value returned should 0x03
}
void loop() {
}
when send command byte, returned value (which gets printed terminal) 243 corresponds expected hex value of 0xf3. however, following command not turn on fan , second returned value 231 not correspond expected hex value of 0x03.
i don't think it's hardware issue because @ least 1 of returned values correct , because sensor work when used different platform. experimented different delays between bytes. if has insights i'd appreciate help.
i realised although sensor needs 5v power, uses 3.3v logic whereas arduino uses 5v logic. have intel galileo can configured use 3.3v logic think i'll try that.
Arduino Forum > Using Arduino > Sensors > SPI: problems communicating with sensor
arduino
Comments
Post a Comment