MPU6050 based motion detector

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

MPU6050 based motion detector

#1 Post by mackowiakp » 23 Apr 2022, 04:18

I made a system that signals the end of the washing machine's washing cycle.
I used the ESP-CAM module for this, without the camera module, of course.
As a sensor, a module based on MPU 6050 was used, mounted directly to the washing machine drum.
I compiled the FW (ESP_Easy_mega_20211225_custom_ESP32_4M316k) containing the MPU6050 driver and calibrated the sensitivities in each axis.
Generally unit works properly, configured as a motion detector.
However, sometimes all indications of acceleration in each axis are equal to 0, and for a long time. Obviously, this causes a false signal about the end of washing. Soft reboot does not improve the situation, only the power cycle causes the system to work normally again.
I noticed that the CPU load is very high (about 60%) even when all tasks related to MPU6050 are disabled.
Moreover, the maximum acceleration shown by the sensor is (+ or -) 32768 which is the maximum value for the sensor.
Question. Is the reason for the "hang" of the sensor, i.e. showing 0, is a problem in the MPU6050 driver (TESTING status), a known problem with I2C or exceeding the maximum value of indications allowed by the sensor?
What kind of forum colleagues have experience in this field?
Below is a screenshot of the device configuration. Dummy sensor contains the maximum measured acceleration value in each axis.
Screenshot_20220423_033346.png
Screenshot_20220423_033346.png (127.53 KiB) Viewed 7197 times

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: MPU6050 based motion detector

#2 Post by ThomasB » 23 Apr 2022, 04:36

I'm using a MPU6050 on a ESP8266 for sensing when the dryer stops tumbling. Has worked great for several years, no issues.

But I don't use it to sense the washing machine. That's because there are several places in the wash cycle where the machine is idle for several minutes (no vibrations during soak time). So instead I monitor the AC voltage on its timer using a optoisolator. This allows me to reliable detect when the washing machine is done. BTW, some washers have front panel status lights that can be monitored, but mine didn't.

Screenshot:
esp_laundry.jpg
esp_laundry.jpg (113.1 KiB) Viewed 7194 times
.
.
Youtube video:
https://www.youtube.com/watch?v=gVsYJ0QBaxY

- Thomas

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#3 Post by mackowiakp » 23 Apr 2022, 07:07

Yes, it is a very good solution and I had the same. My old washing machine had a two-color LED. It glowed red when the washing machine was running and green when it finished the cycle.
But I bought a new washing machine which has a 5 year warranty. So I can't solder anything to it or interfere with the design.
I attached the acceleration sensor to the drum with a plastic strap. So in case the washing machine breaks down, I will quickly dismantle it before the service visit.
As I wrote. Basically everything works but MPU6050 seems to crash from time to time.

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: MPU6050 based motion detector

#4 Post by ThomasB » 23 Apr 2022, 07:19

As I wrote. Basically everything works but MPU6050 seems to crash from time to time.
I'd suggest changing the ESP32 to an ESP8266. Mine is working well, good stability.

- Thomas

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#5 Post by mackowiakp » 23 Apr 2022, 07:31

I will try as a last resort because I soldered everything nicely and "stuffed" it into a small housing.
I ordered a new MPU6050 module because maybe it is the reason, but it has not yet arrived from China.
For now, I will try if hard-reset (via any free GPIO) is enough to restore proper operation, because it is just a matter of soldering one cable and a few lines of rule.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#6 Post by mackowiakp » 23 Apr 2022, 07:34

BTW Thomas. What is CPU usage in Your unit? In my case - as I wrote - 60%.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#7 Post by TD-er » 23 Apr 2022, 10:07

Do you have the option to test if you can find any I2C device in the I2C scan, when this happens?
To me it looks like it might be a "stuck" I2C bus.

In recent builds, there is some code to detect it and try to get out of it, but it is next to impossible to test it as it is hard to get the I2C bus to hang on purpose.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#8 Post by mackowiakp » 23 Apr 2022, 10:29

Yes. MPU6050 is shown as avaliable when I2C scan is run from ESP Tools menu when readouts are 0.
The option to try to reastart I2C is enabled.
Do You think that change ESP32 to ESP8266 will resolve problem?

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MPU6050 based motion detector

#9 Post by Ath » 23 Apr 2022, 10:30

TD-er wrote: 23 Apr 2022, 10:07 ... it is hard to get the I2C bus to hang on purpose.
Recently, I accidentally discovered that this can be easily simulated by pulling SDA to GND :P

As with previous experiments you did with I2C devices, how long is the wiring? Have you tried to use some twisted-pair (UTP?) cable, or even shielded (ESP-side of the shielding connected to GND) cable?
And there is always the option of lowering the I2C clock speed, by checking the "Force Slow I2C speed" checkbox on the device configuration page.
/Ton (PayPal.me)

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#10 Post by mackowiakp » 23 Apr 2022, 10:35

Clock speed is set to 100 KHz. The cable ESP - sensor is appx 1 m long. It is UTP Cat 4. I just had a piece of such cable.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#11 Post by TD-er » 23 Apr 2022, 11:32

Hmm 1m is not within specs of I2C.

How are the signals distributed over the twisted wires?
It is best to have SDA & GND in a pair and SCL & GND in a pair.
You probably also need some 3V3 sent to the sensor, so why not use both other pairs for 3V3 and GND both in each pair. (so GND and 3V3 are also twisted)

You may also want to tweak the current through the cable by changing the pull-up resistors and place the pull-up resistors close to the sensor.
This way you need the most amount of current through the cable to change the logic level and thus make it as immume to noise as possible.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#12 Post by mackowiakp » 23 Apr 2022, 14:51

1 m is max lengh of I2C cable for 100 kHz and 10 m for 10 kHz. Will try to lower frequency to 10 kHz.
I use one twisted pair for SDA/SCL and second for 3.3V/GND at this moment. Will change as You suggest.
Pull-up resistors (4,7 k) are solder directly to the sensor board and additionally 10 µF capacitor parallel to supply to avoid noise on supply cables.
But I am still bothering me why the problem only occurs (and only from time to time) when for a long time the acceleration indications are equal to (although they are probably higher) the maximum value that can be given by the internal 16 bit converter.
Maybe that's the problem? In any case, I will check with a different sample of the sensor as soon as I receive it.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#13 Post by TD-er » 23 Apr 2022, 16:50

Could of course also be a problem in the code of that plugin, but then you would expect that a reboot would fix it.
It is possible that some specific bit pattern is more likely to be sensitive to timing issues, where the host and the sensor disagree on what bit belongs to what clock pulse.

A lot of protocols have implemented special countermeasures to prevent these kind of issues.
For example the CD (as in those old fasioned shiny music carriers ;) ) does store 8 bits using 14 bits on the disc just to make sure no 2 zeroes are right after each other. (or ones, not 100% sure)

USB uses the same, where after 6 ones, a zero is inserted. This also makes the actual bandwidth variable.
I have once experienced this, where I had made an USB camera, but with full brightness, I could not meet the bandwidth requirement we needed. So then I patched the firmware to XOR all bytes with 0x5A (01011010) so you have almost a guarantee to get enough 1-0 transitions to meet the bandwidth requirements.

Anyway, it is possible that in your setup for whatever reason the timings of the SDA and SCL differ slightly. In most cases this will be just fine as the internal clock counter of the chip will be synced often enough.
But if there are too many bits in a row which do not switch the state of the SDA line, both clocks may get out of sync and thus either side is then waiting for a command to complete while the other is convinced there is nothing more to do.

A longer cable may make this worse, or picked up noise can also increase the chance of this happening.

Edit:
One other thing that may be different here...
What if this specific sensor uses clock stretching?
I know clock stretching was not implemented in the older ESP32 SDK, but it should be on the IDF4.4
So you could try to test with a test build of my IDF 4.4 pull request.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#14 Post by mackowiakp » 23 Apr 2022, 17:06

OK. But I think that lowering clock to 10 kHz help to resolve cable length potential issue and timings.
Can you point me a link to Your 4.4 SDK based build?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#15 Post by TD-er » 23 Apr 2022, 17:25

It is this PR: https://github.com/letscontrolit/ESPEasy/pull/3845
But I noticed its last GitHub Actions build was unsuccessful.
Will have a look at why it wasn't successful.

At least there seems to be a zip with binaries to download: https://github.com/letscontrolit/ESPEas ... 2206306414

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: MPU6050 based motion detector

#16 Post by ThomasB » 23 Apr 2022, 18:38

BTW Thomas. What is CPU usage in Your unit? In my case - as I wrote - 60%.
At the time I posted the screenshot %sysload% was 31%. This morning it is 29%.
Clock speed is set to 100 KHz. The cable ESP - sensor is appx 1 m long. It is UTP Cat 4. I just had a piece of such cable.
Perhaps the issue is not related to cable length. But it could be. Cable length related issues have caused frustration to many i2c users. Random i2c problems (crashes, bad data, i2c locking) are a symptom of this.

In a perfect world the 1M length would be reliable. But you will need to find best routing inside the machine (lowest noise route) and optimize the i2c pullups for clean clock/data edges and no refections. It is soooo much easier to use shorter cabling; If you do that there should be no need to worry about the demons that get invited when you use longer cables.
I attached the acceleration sensor to the drum with a plastic strap. So in case the washing machine breaks down, I will quickly dismantle it before the service visit.
I don't think you need to mount the sensor inside the machine (on the drum). Just mount it *anywhere* on the machine. The vibrations are everywhere on the machine, even the outside metal panels.

If you look at the demo video I posted you will see my "Laundry Buddy" mounted on my Dryer. It handles *both* the washer and dryer (small cable connects it from dryer to washer). The Nextion display, ESP8266, and MPU6050 are all in the same tiny enclosure. It slides on top of the dryer control panel and is held in place by a rare earth magnet hiding in the plastic enclosure.

Like your washing machine sensor, my dryer's end-of-cycle is detected when the vibrations stop. So no hard wiring to it. Removing the ESPEasy device before a warranty service call would be a tool-less five second effort.

Too bad my washer wasn't as easy. I had to modify its motor driven timer and add an opto board to the high voltage wiring. But it was out of warranty when I hacked it and I've been my own appliance repairman for many years.

- Thomas

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#17 Post by mackowiakp » 24 Apr 2022, 03:33

All this drives me to assumption that there is something wrong with my copy of MPU6050.
The problem is that I am waiting for a new delivery from another supplier.
I put the currently used sensor together with pull-up resistors, capacitor and soldered cables to the plastic bottle cap and filled it completely with epoxy resin. So it is not dismantleable.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#18 Post by mackowiakp » 24 Apr 2022, 05:00

At least there seems to be a zip with binaries to download: https://github.com/letscontrolit/ESPEas ... 2206306414
OK, there are several binaries for ESP32 containing MPU6050 driver like this:

Code: Select all

[root@Piotr Binaries]# ls
ESP_Easy_mega_20220422_custom_ESP32_4M316k.bin
ESP_Easy_mega_20220422_custom_ESP32_4M316k.factory.bin
ESP_Easy_mega_20220422_normal_ESP32_4M316k.bin
ESP_Easy_mega_20220422_normal_ESP32_4M316k.factory.bin
ESP_Easy_mega_20220422_test_A_ESP32_4M316k.bin
ESP_Easy_mega_20220422_test_A_ESP32_4M316k.factory.bin
ESP_Easy_mega_20220422_test_B_ESP32_4M316k.bin
ESP_Easy_mega_20220422_test_B_ESP32_4M316k.factory.bin
ESP_Easy_mega_20220422_test_C_ESP32_4M316k.bin
ESP_Easy_mega_20220422_test_C_ESP32_4M316k.factory.bin
ESP_Easy_mega_20220422_test_D_ESP32_4M316k.bin
ESP_Easy_mega_20220422_test_D_ESP32_4M316k.factory.bin
Which one You suggest to install?
I want to update FW via OTA. So - as I understand - first I have to install *.4M316.factory.bin file and then *.4M316.bin.
I am right?
My current running FW is ESP_Easy_mega_20211225_custom_ESP32_4M316k (own compilation)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#19 Post by TD-er » 24 Apr 2022, 09:28

The ones with "factory" are when flashing via serial.
The others are for flashing "OTA", via the browser.

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MPU6050 based motion detector

#20 Post by Ath » 24 Apr 2022, 09:37

If your current install allows OTA updates via the Tools page, then you can update through that method to any of the desired '4M316k' firmwares without factory in the filename.
The factory.bin files are only needed for the initial flash via serial (at address 0x0), as ESP32 requires a partitioning and a bootloader, and that is what's extra in those factory.bin files. No need to do that again, unless you would want to use a different partitioning scheme, (currently ESPEasy only offers the 4M316k partitioning for 4MB ESP32 units), or a new bootloader would be provided, but that's supplied by Espressif, and hasn't changed for years.
/Ton (PayPal.me)

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#21 Post by mackowiakp » 24 Apr 2022, 10:04

THX

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#22 Post by mackowiakp » 24 Apr 2022, 18:02

I just flashed via FW ESP_Easy_mega_20220422_custom_ESP32_4M316k.bin and it seams to not work properly. It was impossible to communicate with the unit.
So next I flashed my own compilation of FW (previously installed) with "factory" in name - ESP_Easy_mega_20211225_custom_ESP32_4M316k-factory.bin
Unexpectedly, it started properly with previously installed my compilation FW ESP_Easy_mega_20211225_custom_ESP32_4M316k.bin.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MPU6050 based motion detector

#23 Post by TD-er » 24 Apr 2022, 20:44

How did you flash it?
If you flashed it via serial, you needed the -factory bin.

And about the bootloader not changed in a while...
Well that's not entirely true.
If you experience issues like these, it is always best to flash via serial.
When flashing via serial, the flashing tools also look at the detected flash chip and set 2 bits in the bootloader section, regarding flash frequency and flash size.

mackowiakp
Normal user
Posts: 527
Joined: 07 Jun 2018, 06:47
Location: Gdynia/Poland

Re: MPU6050 based motion detector

#24 Post by mackowiakp » 29 Apr 2022, 01:52

Reducing the I2C clock frequency to 10kHz seems to have solved the problem. And the source of the problem was probably a long cable (1m), which is the limit for the frequency of 100 kHz, and the induced noise in this cable, generated e.g. by the washing machine's motor.
I still have to test it. But the real test is to just run the entire wash cycle, and many times over.
I do not intend to run washing machines aimlessly at today's electricity prices. Thus, the test is only carried out when laundry is actually needed. So testing will take a while, but the initial conclusions are as in the first sentence of this post.

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests