Go Down

Topic: First arduino app using accelerometer (Read 364 times) previous topic - next topic

Jiboo

Hi,

I' m new to eletronic dev. But I got some experience from school courses.
This is my first usage of my accelerometer with my arduino.
https://www.sparkfun.com/commerce/product_info.php?products_id=8658
https://www.sparkfun.com/commerce/product_info.php?products_id=9220

As in the sample, I' m trying to read the 0x0F register first.

This is how i connected the accelerometer to Arduino.
CS -> Pin 2
MISO (SD0) -> Pin 3
MOSI (SDA) -> Pin 4
SCL -> Pin 5
GND -> Pin 6
VCC -> Pin 7

And here is my sketch :
Code: [Select]
#include <Wire.h>

#define LED_PIN 13

#define ACC_VCC 7
#define ACC_GND 6
#define ACC_CS 2
#define ACC_SD0 3
#define ACC_ADR 0x38

void acc_init()
{
 pinMode(LED_PIN, OUTPUT);
 digitalWrite(LED_PIN, HIGH);
 
 pinMode(ACC_VCC, OUTPUT);
 pinMode(ACC_GND, OUTPUT);
 pinMode(ACC_SD0, OUTPUT);
 pinMode(ACC_CS, OUTPUT);
 
 digitalWrite(ACC_GND, LOW);
 digitalWrite(ACC_VCC, HIGH);
 digitalWrite(ACC_SD0, LOW); // So adress is 0011100X = 0x38
 digitalWrite(ACC_CS, HIGH); // So i enable I2C mode
 
 // Request the 0x0F register
 Wire.beginTransmission(ACC_ADR);
 Wire.send(0x0F);
 Wire.endTransmission();
 
 // read the response
 Wire.requestFrom(ACC_ADR, 1);
 while(!Wire.available());
 byte whois = Wire.receive();
 Serial.print(whois, DEC);

 digitalWrite(LED_PIN, LOW);
}

void setup()
{
 Wire.begin();
 Serial.begin(4800);
 acc_init();
}

void loop()
{
}


The LED do not turn off, so the accelerometer do not reply.
Soed someone see anything wrong?

Thanks in advance for your replies.
Bye,
Jiboo.

AWOL

#1
Jun 19, 2009, 02:00 pm Last Edit: Jun 19, 2009, 02:02 pm by AWOL Reason: 1
First, whilst debugging, it's probably simpler to power the accelerometer direct from the Arduino's supply, rather than trying to power it from logic pins.
Just makes things easier and doesn't rely on your software.


Check your wiring - SDA and SCL are on the analogue pins, IIRC.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Jiboo

Thanks, after more reading about I2C, i figured out that the bus need to be pulled up... I just can' t do this now, so i tried with SPI..

And  I got it to work ^^

This is my sketch (Just read the 0x0F register, but should help people with this hardware).

This sketch will look really "retarded" to you guys, cause i' m using "software" spi, but i couldn' t understand anything from the ATmega328 SPI chapter...

Bye,
Jiboo.
Code: [Select]
#define ACC_VCC 9
#define ACC_GND 8
#define ACC_SCL 7
#define ACC_MOSI 6
#define ACC_MISO 5
#define ACC_CS 4

void acc_setup()
{
 pinMode(ACC_VCC, OUTPUT);
 pinMode(ACC_GND, OUTPUT);
 pinMode(ACC_SCL, OUTPUT);
 pinMode(ACC_MOSI, OUTPUT);
 pinMode(ACC_MISO, INPUT);
 pinMode(ACC_CS, OUTPUT);
 
 digitalWrite(ACC_GND, LOW);
 digitalWrite(ACC_VCC, HIGH);
 
 digitalWrite(ACC_CS, HIGH);
 digitalWrite(ACC_SCL, HIGH);
}

inline void spi_output_bit(byte pValue)
{
 digitalWrite(ACC_SCL, LOW);
 digitalWrite(ACC_MOSI, pValue);
 delay(1);
 digitalWrite(ACC_SCL, HIGH);
 delay(1);
}

inline void spi_output_byte(byte pValue)
{
 byte lMask;
 for(lMask = 0x80; lMask != 0x00; lMask = lMask>>1)
 {
   spi_output_bit(pValue & lMask);
 }
}

inline byte spi_input_byte()
{
 byte lReturn = 0;
 byte lIndex;
 for(lIndex = 7; lIndex != 0xFF; lIndex--)
 {
   digitalWrite(ACC_SCL, LOW);
   delay(1);
   lReturn += digitalRead(ACC_MISO)<<lIndex;
   digitalWrite(ACC_SCL, HIGH);
   delay(1);
 }
 return lReturn;
}

byte acc_read_whoami()
{
 byte lReturn;
 digitalWrite(ACC_CS, LOW);
 spi_output_byte(0x8F); // read bit + no adress inc. + 0x0F
 lReturn = spi_input_byte();
 digitalWrite(ACC_CS, HIGH);
 return lReturn;
}

void setup()
{
 Serial.begin(4800);
 acc_setup();
 Serial.print(acc_read_whoami(), HEX);
}

void loop()
{
}


Go Up