# Raspberry Pi Pico + ADXL345: Portable resonance measurement

**URL:** <https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757>\
**Category:** Configs\
**Created:** [January 14, 2022, 10:59am UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757 "2022-01-14T10:59:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 14, 2022, 10:59am UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/1 "2022-01-14T10:59:24Z")

</div>

The Raspberry Pi (RPi) Pico offers a convenient and cost efficient way to create a portable resonance measurement device that can be used across different Klipper printers.

# ADXL345/343 boards

There are different ADXL345 boards on the market and also the ADXL343 board is a valid alternative.  
Depending on the board layout and used components a few things should be considered when connecting the board.

## General

The boards can be mainly distinguished by potentially having a Voltage Regulator or a Level Shifter.

### Voltage Regulator

- The Voltage Regulator allows the board to be connected to either 3.3V or to 5V
- The ADXL chip itself requires 3.3V. The voltage regulator ensures that only 3.3V actually reach the chip
- In the following images, a Voltage Regulator is marked with a **green rectangle**

> ⚠ **WARNING:** Boards without such a Voltage Regulator must not be connected to 5V or the board will be destroyed

### Level Shifter

- Digital electronics, like MCUs (aka printer boards) or RPis require a certain voltage on their pins
- Typical voltages in 3D printing are either 3.3V (most common) or 5V
- A Level Shifter allows to translate between 3.3V and 5V or vice versa
- The ADXL family is in the 3.3V domain by default. This means that it outputs only 3.3V even when connected to 5V (see voltage regulator).
- A Level Shifter changes this: When the ADXL is connected to 5V (see Voltage Regulator) it will also output 5V on the board’s connecting pins
- In the following images, a Level Shifter is marked with a **yellow rectangle**

> ⚠ **WARNING:** RPIs and many MCU boards are designed for 3.3V only. A board with Level Shifter must not be connected to 5V and to a partner tolerant only to 3.3V. This will destroy the partner.

### I2C Boards

- Most boards use the SPI as communication bus. SPI is fast enough to deliver up to 3200 samples/s, as it is needed for resonance measurement
- Some boards seem to be forced into I2C mode (another bus type) with a pull-down resistor
- Such boards do not work for resonance measurement but can potentially be reworked by removing the pull-down resistor
- Refer to [ADXL345 Invalid ID · Issue #3637 · Klipper3d/klipper · GitHub](https://github.com/Klipper3d/klipper/issues/3637#issuecomment-816997710) with special thanks to @JanKolibri

> ⚠ **WARNING:** Avoid such boards, if you do not feel comfortable with soldering on such a board.

## ADXL345 with Voltage Regulator and Level Shifter

 ![adxl345_ldo_level_shift](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/ecbb81460c9c614656dbff83d4783b1ee989e253.jpeg)

## ADXL345 with Voltage Regulator

 ![adxl345_ldo](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/d782f328b4f1a73cb3f146bbcc6df34a1cc6914e.jpeg)

## ADXL345 without Voltage Regulator

 ![adxl345_none](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/161e25962b55499b712c2f324d63cc32258b9727.jpeg)

## ADXL343 with Voltage Regulator and Level Shifter

 ![adxl343_ldo_level_shift](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/c5b23d47e6fff2f51f290c33f532001fd3153df8.jpeg)

## ADXL345 with Voltage Regulator and forced to I2C

 ![adx345_i2c](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/3e45ef21733bc89101c91eafd9d1b45ee81b0b56.jpeg)

# Wiring

| ADXL345 pin | RPi Pico pin | RPi Pico pin name |
| --- | --- | --- |
| VIN (or VCC) | 36 | 3V3 Out |
| GND | 38 | GND |
| CS | 2 | SPI0 CSn (GP 1) |
| SDO | 1 | SPI0 RX (GP 0) |
| SDA | 5 | SPI0 TX (GP 3) |
| SCL | 4 | SPI0 SCK (GP 2) |

 ![Clipboard01](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/9c8eaa478031620380eda99f012b4046ddc96e2f.png)

# Compiling & flashing the firmware for the Pico

## Compile the firmware

```console
cd ~/klipper/
make menuconfig

```

![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/963e419f5d95fa556540a338308bca6a0b1d1aa6.png)

```bash
make

```

## Flash the firmware

1. Conncect the Pico to the regular Klipper Raspberry Pi (or other SBC) USB port, while holding down the `BOOTSEL` button
2. Given no other mass storage devices are existing, the Pico should register as block device `/dev/sda`
3. Mount the block device and copy the Klipper firmware file to it

```bash
sudo mount /dev/sda1 /mnt
sudo cp out/klipper.uf2 /mnt
sudo umount /mnt

```

1. After un-mounting, the Pico should automatically reboot with the new firmware

# Configuring Klipper

1. Get the correct `serial` path with `ls /dev/serial/by-id/*`
2. Add the following to the `printer.cfg` file:

```auto
[mcu pico]
serial: /dev/serial/by-id/usb-Klipper_rp2040_mySerial

[adxl345]
spi_bus: spi0a
cs_pin: pico:gpio1

[resonance_tester]
accel_chip: adxl345
probe_points:
    100,100,20 # an example

```

1. Restart Klipper with the `RESTART` command.

> ❕ **Important:** The first measurement, after freshly connecting the RPi Pico or restarting Klipper, will fail. This is due to some issues with the SPI initialization. Subsequent measurements will work.

# Available SPI buses on the RPi Pico

It is possible to connect multiple ADXL boards to one RPi Pico:

| ADXL345 pin | RPi Pico pin name | spi0a | spi0b | spi0c | spi1a | spi1b |
| --- | --- | --- | --- | --- | --- | --- |
| VIN (or VCC) | 3V3 Out | 36 | 36 | 36 | 36 | 36 |
| GND | GND | 38 | 38 | 38 | 38 | 38 |
| CS | CSn | 2 (GP1) | 7 (GP5) | 22 (GP17) | 12 (GP9) | 17 (GP13) |
| SDO | SPI RX | 1 (GP0) | 6 (GP4) | 21 (GP16) | 11 (GP8) | 16 (GP12) |
| SDA | SPI TX | 5 (GP3) | 10 (GP7) | 25 (GP19) | 15 (GP11) | 20 (GP15) |
| SCL | SPI SCK | 4 (GP2) | 9 (GP6) | 24 (GP18) | 14 (GP10) | 19 (GP14) |

**Notes:**

- Header is the name of the bus, values given as `pin-number (GPIO number)`
- Pin definition is based on [klipper/src/rp2040/spi.c at 02d5f9754fc7f9e493c8bc5a6418e2a9ea9d7ae1 · Klipper3d/klipper · GitHub](https://github.com/Klipper3d/klipper/blob/02d5f9754fc7f9e493c8bc5a6418e2a9ea9d7ae1/src/rp2040/spi.c#L14-L28)
- I left out the buses `spi0d` and `spi1c` as I could not match them to the Pico Pin-Out diagram. So theoretically, there may be even 2 more buses
- Untested. Any feedback is appreciated

---

<div class="post-metadata">

**Author:** ![glabifrons](https://avatars.discourse-cdn.com/v4/letter/g/9e8a1a/32.png) [@glabifrons](https://klipper.discourse.group/u/glabifrons)\
**Post date:** [January 16, 2022, 4:38pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/2 "2022-01-16T16:38:07Z")

</div>

See my post on Reddit for a nice snap-together (printable) case for the Pico (and an alternate wiring/config for this setup).

> **[r/klippers - Raspberry Pi Pico as a secondary MCU for resonance testing working!](https://www.reddit.com/r/klippers/comments/owqvo2/raspberry_pi_pico_as_a_secondary_mcu_for/)**
>
> 50 votes and 44 comments so far on Reddit

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 16, 2022, 5:28pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/3 "2022-01-16T17:28:37Z")

</div>

@glabifrons thanks for sharing.  
One comment: There are many different ADXL boards out there. Some are only 3.3V tolerant.  
Your wiring will potentially kill such boards as you are using a 5V pin

---

<div class="post-metadata">

**Author:** ![glabifrons](https://avatars.discourse-cdn.com/v4/letter/g/9e8a1a/32.png) [@glabifrons](https://klipper.discourse.group/u/glabifrons)\
**Post date:** [January 16, 2022, 6:21pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/4 "2022-01-16T18:21:28Z")

</div>

@Sineos Very good to point out!  
Yes, mine is a 5V-tolerant version.

Edit: I edited my Reddit post (in bold) to warn people about the wiring. Thanks!

---

<div class="post-metadata">

**Author:** ![w00sh](https://avatars.discourse-cdn.com/v4/letter/w/b5ac83/32.png) [@w00sh](https://klipper.discourse.group/u/w00sh)\
**Post date:** [January 16, 2022, 11:36pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/5 "2022-01-16T23:36:41Z")

</div>

I configured everything as per instructions, however I’m getting an error when querying the ADXL345. ‘got f2 vs e5’

I know that the PICO and ADXL345 are working because I can access it via microPython (via SPI) just fine.

Few questions:  
Where does spi0a come from? Given gpio notation, it seems to me it should be pico:spi0? Could it be that my other mcu SPI is being used?  
EDIT: as per reddit linked above CS pin determines mcu being used, so I suppose the correct SPI port is being opened…  
How can I bypass device ID detection?  
Is there anything else I can try to troubleshoot this?

EDIT2: turns out I just needed to give it some time before spi starts working.

For reference it appears it took about 30 seconds for SPI port to be available for some reason:

```auto
00:30:02 !! Lost communication with MCU 'pico'
00:30:09 $ FIRMWARE_RESTART
00:30:34 // Klipper state: Ready
00:30:42 $ ACCELEROMETER_QUERY
00:30:42 // Invalid adxl345 id (got f2 vs e5).
// This is generally indicative of connection problems
// (e.g. faulty wiring) or a faulty adxl345 chip.
00:30:42 !! Invalid adxl345 id (got f2 vs e5).
00:31:04 $ ACCELEROMETER_QUERY
00:31:05 // adxl345 values (x, y, z): 2677.215450, -1988.788620, 15910.308960

```

Heh, I take it back. It appears that first access after reboot results in an error. No matter the time that have passed.

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 17, 2022, 11:21pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/6 "2022-01-17T23:21:34Z")

</div>

Initial post amended by further available SPI buses. Mind the notes

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 28, 2022, 3:47pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/7 "2022-01-28T15:47:19Z")

</div>

Initial post amended by information on various board configurations

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 29, 2022, 4:30pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/8 "2022-01-29T16:30:11Z")

</div>

@glabifrons: For your guide: There is another potential pitfall with connecting to 5V. Some boards have a Level Shifter which will bring the output to 5V. In this case not the ADXL might get destroyed but the Pico.

---

<div class="post-metadata">

**Author:** ![ERikO](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@ERikO](https://klipper.discourse.group/u/ERikO)\
**Post date:** [November 8, 2022, 6:06pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/9 "2022-11-08T18:06:12Z")

</div>

Just reporting in that your instructions are still spot on.

---

<div class="post-metadata">

**Author:** ![George](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/george/32/3585_2.png) [@George](https://klipper.discourse.group/u/George)\
**Post date:** [December 7, 2022, 2:54pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/10 "2022-12-07T14:54:26Z")

</div>

To connect 2 ADXLs I use the following pins. For the second ADXL you will need to use GPIO8, GPIO9, GPIO10 and GPIO11 according to the colors. Red and Black are the same.

 ![2_adxls](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/4/4c00abf0a7fccefb9080190690087854dbdeb088.webp)

Klipper Configuration

```auto
[mcu pico_adxl345]
serial: /dev/serial/by-id/usb-Klipper_rp2040_use_your_id-xxxxx

[adxl345 head]
cs_pin: pico_adxl345:gpio1
spi_bus: spi0a
axes_map: -z, x, -y #update your axes mapping

[adxl345 bed]
cs_pin: pico_adxl345:gpio9
spi_bus: spi1a

[resonance_tester]
probe_points: 115, 115, 30 #update your position
accel_chip_x: adxl345 head
accel_chip_y: adxl345 bed

```

For pico case I use [this](https://www.printables.com/model/226610-raspberry-pi-pico-case).  
For bed mount I use [this](https://www.printables.com/model/128294-ender-3-v2pro-adxl345-bed-mount).

---

<div class="post-metadata">

**Author:** ![Creo2005](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Creo2005](https://klipper.discourse.group/u/Creo2005)\
**Post date:** [December 8, 2022, 10:04am UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/11 "2022-12-08T10:04:08Z")

</div>

![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/7/7153f97b04caf4c627daf3d88f74dc33165c5967.jpeg)  
Adxl345 with raspbery chip 🙂

---

<div class="post-metadata">

**Author:** ![ChrisInEdmonton](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/chrisinedmonton/32/3512_2.png) [@ChrisInEdmonton](https://klipper.discourse.group/u/ChrisInEdmonton)\
**Post date:** [January 17, 2023, 11:38am UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/12 "2023-01-17T11:38:33Z")

</div>

@Sineos , I’d like to take a stab at updating the [Klipper Measuring Resonances](https://www.klipper3d.org/Measuring_Resonances.html) documentation to reflect the information you provided here as it was really helpful to me. I’d love to use some of the images you posted in this thread.

I believe that means you’d have to release the images either to the public domain or under [Klipper’s license, the GPL3](https://github.com/Klipper3d/klipper/blob/master/COPYING). Do I have your consent? You are certainly under no obligation to release your images and even if you don’t, I very sincerely thank you for posting this really helpful information.

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [January 17, 2023, 12:38pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/13 "2023-01-17T12:38:41Z")

</div>

I’m a big fan of the [WTFPL](https://en.wikipedia.org/wiki/WTFPL) - Do What the Fuck You Want To Public License 😉

But of course. As the official support board of Klipper, all my contributions here are automatically considered as common to the project and as such I would consider them under the same GPL v3.

Appreciated that you are asking 👍

---

<div class="post-metadata">

**Author:** ![hybridview](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/hybridview/32/3895_2.png) [@hybridview](https://klipper.discourse.group/u/hybridview)\
**Post date:** [February 2, 2023, 10:01pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/14 "2023-02-02T22:01:07Z")

</div>

Removed original response since my problem did not have anything to do with this post.  
With the help of this info, I was able to do this with a MPU9250 sensor and PICO as well. I used a short run of twisted pair wire taken from a cat5e cable. It’s important to note that most spooled ethernet wire is solid core wire that is not meant to be flexed repeatedly. It will cause problems in this kind of application. Patch cables (like those you get with new cable modems) typically have stranded wire.

---

<div class="post-metadata">

**Author:** ![olemanclimber37](https://avatars.discourse-cdn.com/v4/letter/o/58956e/32.png) [@olemanclimber37](https://klipper.discourse.group/u/olemanclimber37)\
**Post date:** [February 7, 2023, 10:10pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/15 "2023-02-07T22:10:40Z")

</div>

Sineos asked for confirmation on using the other available SPI buses. I managed to mess up spi0a and spi0b by getting the wires wrong and filling adjacent holes with solder. Anyway, I got spi0c working just fine. Config change was obvious -

```auto
[adxl345]
spi_bus: spi0c
cs_pin: pico:gpio17

```

---

<div class="post-metadata">

**Author:** ![olemanclimber37](https://avatars.discourse-cdn.com/v4/letter/o/58956e/32.png) [@olemanclimber37](https://klipper.discourse.group/u/olemanclimber37)\
**Post date:** [February 9, 2023, 11:41am UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/16 "2023-02-09T11:41:12Z")

</div>

I am adding my notes on the errors returned by the Pico based on what I learned in making so many mistakes in the hope that it may help others -

Send: ACCELEROMETER\_QUERY  
Recv: !! Invalid adxl345 id (got ff vs e5)  
This is the error I got when I did not have any wires connected to the correct GP pins.

Send: ACCELEROMETER\_QUERY  
Recv: !! Invalid adxl345 id (got 00 vs e5)  
This is the error I got when I did not have the correct cs\_pin identified in the config file.

Send: ACCELEROMETER\_QUERY  
Recv: !! Invalid adxl345 id (got f2 vs e5)  
Known problem see [ADXL345 on Raspberry Pi Pico stopped after specific commit · Issue #4712 · Klipper3d/klipper · GitHub](https://github.com/Klipper3d/klipper/issues/4712) I get this the first time I access the Pico after plugging it in, but thereafter it works

Send: ACCELEROMETER\_QUERY  
Recv: !! Invalid adxl345 id (got 8f vs e5)  
From [https://github.com/Klipper3d/klipper/issues/3637](https://github.com/Klipper3d/klipper/issues/3637) if you get this error it may be a grounding issue.

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [February 9, 2023, 12:03pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/17 "2023-02-09T12:03:04Z")

</div>

Thanks for sharing this. I’m not sure though, if there is a real system behind this. I have seen various error messages all across for various reasons.  
Especially nasty are these where basically everything is fine but the sensor is unhappy because the cable is not made of pure gold with a triple screen made of Kryptonite

---

<div class="post-metadata">

**Author:** ![olemanclimber37](https://avatars.discourse-cdn.com/v4/letter/o/58956e/32.png) [@olemanclimber37](https://klipper.discourse.group/u/olemanclimber37)\
**Post date:** [February 10, 2023, 1:46pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/18 "2023-02-10T13:46:01Z")

</div>

Very true - correlation is not causation. If nothing else it is a list of almost everything that can be done wrong. The only thing I may have done right is have a relatively short Cat 5e cable between the accelerometer and the Pico ( its about 300 mm long). The USB cable is plenty long to reach the keystone on the back of the printer. I got about half the noise readings compared to the long cable that I originally used to connect directly to the Pi GPIO. Although again I have not done enough research to know why the noise levels changed and it could be something else.

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [June 16, 2023, 3:23pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/19 "2023-06-16T15:23:08Z")

</div>

Split this topic on June 16, 2023, 3:23pm UTC

A post was split to a new topic: [Use Pico ADXL measurements outside of Klipper](https://klipper.discourse.group/t/use-pico-adxl-measurements-outside-of-klipper/9007)

---

<div class="post-metadata">

**Author:** ![clee](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/clee/32/1581_2.png) [@clee](https://klipper.discourse.group/u/clee)\
**Post date:** [July 3, 2023, 3:20pm UTC](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757/20 "2023-07-03T15:20:10Z")

</div>

Sorry for the necro, but I thought I’d chime in to confirm that the Adafruit ADXL34 **3** works great with Klipper, if you’d prefer to save some money. I wired an Adafruit ADXL343 up to a Pico and used it to run `SHAPER_CALIBRATE` on my Trident successfully.

[Next page](https://klipper.discourse.group/t/raspberry-pi-pico-adxl345-portable-resonance-measurement/1757.md?page=2)
