Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example for SPI doesn't work. #12

Open
mianqi2016 opened this issue Nov 10, 2023 · 23 comments
Open

example for SPI doesn't work. #12

mianqi2016 opened this issue Nov 10, 2023 · 23 comments

Comments

@mianqi2016
Copy link

I tried on Arduino UNO, it was always prompt: ADXL345 not connected.

@wollewald
Copy link
Owner

@mianqi2016 , in a separate e-mail you have told me that you are using Arduino Pin 10 as CS Pin. Have you adjusted my example sketch accordingly? You need to either change:
#define CS_PIN 5
to
#define CS_PIN 10

Or you take the sketch as is and connect pin 5 to CS.

@mianqi2016
Copy link
Author

mianqi2016 commented Nov 22, 2023 via email

@wollewald
Copy link
Owner

wollewald commented Nov 22, 2023

Of course there might be different modules out there. But in theory they all should be able to use SPI. My modules look lke one which you find in my blog:

https://wolles-elektronikkiste.de/en/adxl345-the-universal-accelerometer-part-2

What is the label on the IC? And can you provide a photo of your circuit maybe I see something!?

I can only say it works on my side which makes finding the issue difficult

@mianqi2016
Copy link
Author

mianqi2016 commented Nov 24, 2023 via email

@wollewald
Copy link
Owner

?? Something went wrong with your last comment.

@wollewald
Copy link
Owner

Yes, it's just the copy of the notification of my last comment. I was not sure why you uploaded this as a comment here on GitHub. I thought maybe something has gone lost. But not so important.

Still I would be interested to know the label on the ADXL345 IC on your module and also a photo of your circuit might be helpful, since I have no explanation why it doesen't work. If there would be a general issue with SPI reading you can be sure there would be lots of issues here on GitHub and comments on my blog. Therefore it must be something specific which is different on your side. And I am keen to find out what this could. I like problem solving!

@mianqi2016
Copy link
Author

mianqi2016 commented Nov 28, 2023 via email

@mianqi2016
Copy link
Author

mianqi2016 commented Nov 28, 2023 via email

@mianqi2016
Copy link
Author

mianqi2016 commented Nov 28, 2023 via email

@wollewald
Copy link
Owner

Still a miracle. "345B" is the correct label. Basically what you and I do can't be so different. I have tried it again and it works fine. Here's a photo of my test circuit:

adxl345_test

In the example sketch I have only changed the CS pin from 5 to 10. As a result I see this:

Screenshot 2023-11-28 173734

So, all good. Since you tried more than one module we can also exclude this to be the problem. Have checked the cables?

What else you could do is to try another library. I tested this Sparkfun library:

https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library

and this example:

Example: SparkFun_ADXL345_Example

It worked out of the box without any changes to my circuit. After few seconds you should see "Inactivity" on your monitor. If you want to see the x,y,z values you need to uncomment some lines.

The other thing you could do is show what your Arduino "says" during compiling and upload. Maybe I can see something there. To get the full information you could go into File -> Preferences and apply the following settings:

image

This could tell me if there's e.g. any interaction with another library.

@mianqi2016
Copy link
Author

`FQBN: arduino:avr:uno
Using board 'uno' from platform in folder: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6
Using core 'arduino' from platform in folder: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6

Detecting libraries used...
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /dev/null
Alternatives for ADXL345_WE.h: [[email protected]]
ResolveLibrary(ADXL345_WE.h)
-> candidates: [[email protected]]
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard -I/home/moonriverzf/Arduino/libraries/ADXL345_WE/src /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /dev/null
Alternatives for Wire.h: [[email protected]]
ResolveLibrary(Wire.h)
-> candidates: [[email protected]]
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard -I/home/moonriverzf/Arduino/libraries/ADXL345_WE/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /dev/null
Alternatives for SPI.h: [[email protected]]
ResolveLibrary(SPI.h)
-> candidates: [[email protected]]
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard -I/home/moonriverzf/Arduino/libraries/ADXL345_WE/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI/src /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /dev/null
Using cached library dependencies for file: /home/moonriverzf/Arduino/libraries/ADXL345_WE/src/ADXL345_WE.cpp
Using cached library dependencies for file: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src/Wire.cpp
Using cached library dependencies for file: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src/utility/twi.c
Using cached library dependencies for file: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI/src/SPI.cpp
Generating function prototypes...
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard -I/home/moonriverzf/Arduino/libraries/ADXL345_WE/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI/src /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/preproc/ctags_target_for_gcc_minus_e.cpp
/home/moonriverzf/.arduino15/packages/builtin/tools/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/preproc/ctags_target_for_gcc_minus_e.cpp
Compiling sketch...
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard -I/home/moonriverzf/Arduino/libraries/ADXL345_WE/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire/src -I/home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI/src /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp -o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp.o
Compiling libraries...
Compiling library "ADXL345_WE"
Using previously compiled file: /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/ADXL345_WE/ADXL345_WE.cpp.o
Compiling library "Wire"
Using previously compiled file: /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/Wire/utility/twi.c.o
Using previously compiled file: /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/Wire/Wire.cpp.o
Compiling library "SPI"
Using previously compiled file: /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/SPI/SPI.cpp.o
Compiling core...
Using precompiled core: /tmp/arduino/cores/arduino_avr_uno_d19ae04455812a0c91928c14c9496ad0/core.a
Linking everything together...
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-gcc -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.elf /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/sketch/ADXL345_SPI_basic_data.ino.cpp.o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/ADXL345_WE/ADXL345_WE.cpp.o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/Wire/Wire.cpp.o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/Wire/utility/twi.c.o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/libraries/SPI/SPI.cpp.o /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/../../cores/arduino_avr_uno_d19ae04455812a0c91928c14c9496ad0/core.a -L/tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44 -lm
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.elf /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.eep
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy -O ihex -R .eeprom /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.elf /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.hex

Using library ADXL345_WE at version 2.2.5 in folder: /home/moonriverzf/Arduino/libraries/ADXL345_WE
Using library Wire at version 1.0 in folder: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/Wire
Using library SPI at version 1.0 in folder: /home/moonriverzf/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI
/home/moonriverzf/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-size -A /tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.elf
Sketch uses 9714 bytes (30%) of program storage space. Maximum is 32256 bytes.
Global variables use 724 bytes (35%) of dynamic memory, leaving 1324 bytes for local variables. Maximum is 2048 bytes.
"/home/moonriverzf/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude" "-C/home/moonriverzf/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-P/dev/ttyACM0" -b115200 -D "-Uflash:w:/tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.hex:i"

avrdude: Version 6.3-20190619
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

     System wide configuration file is "/home/moonriverzf/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
     User configuration file is "/home/moonriverzf/.avrduderc"
     User configuration file does not exist or is not a regular file, skipping

     Using Port                    : /dev/ttyACM0
     Using Programmer              : arduino
     Overriding Baud Rate          : 115200
     AVR Part                      : ATmega328P
     Chip Erase delay              : 9000 us
     PAGEL                         : PD7
     BS2                           : PC2
     RESET disposition             : dedicated
     RETRY pulse                   : SCK
     serial program mode           : yes
     parallel program mode         : yes
     Timeout                       : 200
     StabDelay                     : 100
     CmdexeDelay                   : 25
     SyncLoops                     : 32
     ByteDelay                     : 0
     PollIndex                     : 3
     PollValue                     : 0x53
     Memory Detail                 :

                              Block Poll               Page                       Polled
       Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
       ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
       eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
       flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
       lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
       signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

     Programmer Type : Arduino
     Description     : Arduino
     Hardware Version: 3
     Firmware Version: 4.4
     Vtarget         : 0.3 V
     Varef           : 0.3 V
     Oscillator      : 28.800 kHz
     SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "/tmp/arduino/sketches/D83F640C345AB6B96BC52F5DAB2A3D44/ADXL345_SPI_basic_data.ino.hex"
avrdude: writing flash (9714 bytes):

Writing | ################################################## | 100% 1.56s

avrdude: 9714 bytes of flash written

avrdude done. Thank you.`

@mianqi2016
Copy link
Author

There is something different: when I uploaded SparkFun_ADXL345_Example.ino, there is
......
*** TAP ***
*** FREE FALL ***
*** INACTIVITY ***
*** ACTIVITY ***
*** DOUBLE TAP ***
*** TAP ***
*** FREE FALL ***
*** INACTIVITY ***
*** ACTIVITY ***
*** DOUBLE TAP ***
*** TAP ***
*** FREE FALL ***
*** INACTIVITY ***
*** ACTIVITY ***
*** DOUBLE TAP ***
*** TAP ***
*** FREE FALL ***
......

flashing, it seems that there was interrupt all the way.

@wollewald
Copy link
Owner

All the information that is displayed during compilation and upload looks OK. The relevant parts are identical with mine.
The Sparkfun example should only give you *** INACTIVITY*** as long as the ADXL345 is not moved.
Since two libs do not work the issue does not seem to be related to the code.
I am running out of ideas what the problem could be other than a damaged ADXL345. I assume you have checked the wiring more than once.
Do you have the chance to test another ADXL345 module, preferably from another supplier?

@mianqi2016
Copy link
Author

mianqi2016 commented Apr 8, 2024

ok, I'll try it, your UNO and ADXL345 looks price lower than mine in my market here, maybe I was too caution to have bought the expensive one. :-)

@mianqi2016
Copy link
Author

mianqi2016 commented Apr 8, 2024

@mianqi2016
Copy link
Author

It was said "You should not be having any pullups for SPI communication.", while I saw also pullups on your module, why did it work properly?

1 similar comment
@mianqi2016
Copy link
Author

It was said "You should not be having any pullups for SPI communication.", while I saw also pullups on your module, why did it work properly?

@wollewald
Copy link
Owner

Hi @mianqi2016 , I can't tell you why my modules work. They just do. You have seen my wiring on the photo. And it works with my spi example sketch as is, apart from the CS pin change to 5. I think this kind of module is most common. If there would be a general problem, I think I would have seen more issues here on GitHub. So I have really no idea why it doesn't work on your side. It's difficult to solve a problem when I can't reproduce it.

@wollewald
Copy link
Owner

Hi, I flipped through the datasheet and couldn't find a hint that pull-ups should not be used for SPI. Did you find it?

But I found another thing you could try. Maybe the SPI clock is too fast. You could change the speed from 8000000 to 5000000 here:

https://github.com/wollewald/ADXL345_WE/blob/main/src%2FADXL345_WE.cpp#L31

Not very optimistic that this will help since the sparkfun lib has implemented a clock rate of 5000000, and this lib also does not work, but it's done quickly and, therefore, worth a try.

@mianqi2016
Copy link
Author

ADXL345-Application-Circuit-Diagram
I tested, this is my module wiring diagram, so I think it would only work in SPI 3-wire mode, how to use your library in this mode?

@wollewald
Copy link
Owner

Sorry, the library does not support the SPI 3-Wire, and I do not intend to implement this mode in foreseeable time. The reason is that no one asked for this, although the lib is in place since 3 years. I need to prioritise my capacity. But I a always happy to have contributers!

@wollewald
Copy link
Owner

Just to add: The problem is the SDO pin. Apart from that my module is module is comparable.
SCL and SDA are pulled up with 4.7 kohm, CS is pulled up with 10 kohm. But SDO is pulled down 4.7 kohm on my module and on yours it's directly connected to GND via a 0 ohm resistor which is from my perspective a strange idea. I can't see any advantage in doing this.
As I said I won't implement 3-Wire SPI or at least not in the near future. If you don't find a solution a pragmatic solution would be to remove R4 and add an external pull-down resistor. Then it should work. But of course it's on your own risk!
Meanwhile I have found such a module that you are using. I have ordered it and will try this "hardware hack". It should arrive end of this week, then I will try and share my experience.

@wollewald
Copy link
Owner

wollewald commented Apr 12, 2024

I have received the module which has SDO connected to GND. After removing the 0 ohm resistor R4 SPI worked. I have updated readme accordingly:
https://github.com/wollewald/ADXL345_WE#if-spi-does-not-work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants