Hello,
I would like to connect two ADXL345 gyroscopes to my Arduino Uno. This is for a music-project, which should finish soon.
Unfortunately, the proposed patches p.e. here:
do nor work ,even that i installed all libraries. But i do not use Adafruit sensors.
In the forums, I found this:
“Normally, you set the chip select pin LOW to talk to a device. But, that only applies to SPI devices, NOT I2C devices. I2C devices have (to have) unique addresses.“ and: “As long as a separate chip select line is used for each one, there is no problem attaching more than one of them to the Arduino. Select which one to read from by setting its chip select pin low (while the others are high).“
But, how to program this?? I’ve tried without success.
So, as i am not a specialist I would be really happy about support!
This is the program for one sensor, which works very well.
(I just need the X,Y and Z values).
:
#include <Wire.h> // Wire library - used for I2C communication
int POT1 = 1;
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out; // Outputs
void setup() {
Serial.begin(9600); // Initiate serial communication for printing the results on the Serial monitor
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
}
void loop() {
POT1 = analogRead(1);
// === Read acceleromter data === //
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
X_out = X_out; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_out = Y_out;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_out = Z_out; // I ve removed the divisions in the original patch in order to get a wider value range
Serial.print("P= ");
Serial.print(POT1, DEC);
Serial.print(" Xa= ");
Serial.print(X_out);
Serial.print(" Ya= ");
Serial.print(Y_out);
Serial.print(" Za= ");
Serial.println(Z_out);
delay(10);
}
To select the module I2C address, connect the SDO/ALT ADDRESS pin on the module to either 3.3V or GND.
Post a link to the product page for the sensor you have.
The sensors are 3.3V devices, and require logic level shifters for SDA and SCL if used with the 5V Uno. If you connect the sensor to the Uno without them, don't expect them to function correctly, and you might even destroy the sensors, or output pins on the Arduino.
Thank you for your answer!
Please appologize that i reply so late: i was on tour, and finally played my concert with just one gyroscope in combination with 1 potentiometer. Yes, I se, you are right, the data sheet mentions 3,3V. But my product page mentions 3-5 V. So, what is right? I was using 5V in these days for 1 gyroscope and the pot., and it worked. This is the product link (sorry, in German, but with pictures):
I will next week start to work on your advices, and in case, it does not work out, it would be nice to stay in contact.
The sensor has a 5V to 3.3V regulator, so it can be powered by 5V, but the Input/Output is 3.3V. You should use a 5V to 3.3V logic level shifter with a 5V Arduino.
Better: use a 3.3V Arduino, and avoid the problem.
Dear jremington, dear administrators,
Please don't close this forum-page until end of March.
I have almost succeeded in connecting the two gyroscopes and am trying to find out the remaining question myself.
Many thanks and best regards,
AnnK
Dear jremington,
It works! Thanks again!
I connected the 2 gyroscopes to the BD-LLC level shifter
(Bi-Directional Logic Level Converter Hookup Guide - SparkFun Learn) and used at the end codes you and Hardiner85 posted on this forum page: Connecting 2 ADXL345 Accelerometer sensors over I2C.
One of my issues were recurring avr-dude and connection errors, which all resulted finally from the fact that I worked inaccurately when soldering and plugging in connections without realizing it. A very nice gentleman in the only remaining electronics store in Berlin then gave me the saving tip ( i would like to share with all those over 50 ): wear strong reading glasses, don't move, then solder or plug in. So, all avr dude errors are gone, it works!