Friday, October 23, 2026

Vibration detecting with the MPU6050

For an index to all my stories click this text.

When I was working at my project for the Caravan/Mobile-home leveler (previous story on this weblog) I had an idea. What if we could use the data from the MPU6050 to detect vibration or mechanical movement.

The breadboard setup.

This is the same setup as I used in my stories about the caravan / mobile-home leveling aid.



This should not pose any problems and you can easily build this on a breadboard.
Just make sure that the SCL pin from the MPU6050 is attached to pin 5 (D1) of the ESP8266 and the SDA pin is attached to pin 4 (D2).

As this is so simple you can use any other microcontroller, like an ESP32 or Raspberry Pi Pico, to build this project. You even do not need to make alterations in the program except for the pin numbers.

MicroPython program

from machine import Pin, I2C
import time
import math
from lucsmpulib import init_mpu, get_data

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # esp8266
init_mpu(i2c)

totz = 0
i = 0

# Calculate the average value of z
for i in range (20):
    x, y, z = get_data(i2c)
    totz = totz + z

average = totz / 20
print(average)

# use a margin
great = average + 0.017
less = average - 0.017


while True:
        x, y, z = get_data(i2c)
        if z > great or z< less:
            print("vibration detected ",z)
            time.sleep(.5)
        time.sleep(.2)


The idea is simple.

First a measurement is done 20 times and from that the average value for z is calculated.

Then we add a margin to the average value and call that great and we subtract the same value from average and call that less.
I used a margin of 0.017 but you will need to do some tests and adjust it to your own circumstances.

Inside the while loop we constant read the value of z and test if it is greater then the value of great or smaller as the value of less and if that is the case: vibration is detected.

Each time after vibration is detected we wait for .5 seconds to make sure everything is settled down before we take another reading.

This program is just a framework. You need to write additional code to make this useful like adding a led that lights up when vibration is detected. Or send a notification to your phone. I am sure you can find some purpose for this.

The MPU6050 library

To use this program you will need my own MPU6050 library. I discussed that in the story about the caravan / mobile-home leveling aid. But for your convenience I add it here:

'''
Luc Volders library for MPU6050
'''

from machine import Pin, I2C
import time

def init_mpu(i2c, address=0x68):
    i2c.writeto_mem(address, 0x6B, b'\x00')
    time.sleep_ms(100)
    i2c.writeto_mem(address, 0x19, b'\x07')
    i2c.writeto_mem(address, 0x1B, b'\x00')
    i2c.writeto_mem(address, 0x1B, b'\x00')
    i2c.writeto_mem(address, 0x1C, b'\x00')

def read_data(i2c, addr, address=0x68):
    high = i2c.readfrom_mem(address, addr, 1)[0]
    low = i2c.readfrom_mem(address, addr + 1, 1)[0]
    value = high << 8 | low
    if value > 32768:
        value = value - 65536
    return value

def get_data(i2c):
    readx = read_data(i2c, 0x3B) / 16384.0
    ready = read_data(i2c, 0x3B + 2) / 16384.0
    readz = read_data(i2c, 0x3B + 4) / 16384.0

    return readx, ready, readz


That's all for now.
Till next time


Luc Volders


Saturday, October 10, 2026

Web Apps simplified - My new book is now available

For an index to all my stories click this text

Something new......................................

 


At this moment available through Lulu.com.

In a few weeks also available through Amazon.com

For those who do not know what web apps are. Here is a (very) short description on what they are. And I am very exited about this.

As you know an app that works on Android will not work on IOS and certainly will not work on Windows, MacOS or Linux. They all have their own languages and operating systems. 

But if you build a Web App it runs on EVERY system.

This book shows how to build web apps. Write your program once and it runs on all devices and is easy to test, and to distribute (no app stores needed) to friends, family, club members and customers.

This is about getting your freedom back as a developer.
And it saves loads of time: develop once, run everywhere.

I bet you know about Apple's restrictions for developers. And Maybe you already know about the restrictions Google already is imposing and going to expand on developers.
Web Apps beats these. 

The book is NOW available through Lulu.com (click here)
And in a few weeks it will also be available through Amazon

For those that want to learn more about this subject. Just keep following this weblog as I will be posting more on web apps in upcoming stories.

This sure is fun !!!

Luc Volders



Thursday, October 1, 2026

Quick testing of webpages with Netlify

For an index to all my stories click this text

At the moment I am writing a lot of small Javascript programs. The reason is that I am heavily into PWA's. For those in the dark: A PWA is a program that runs on desktop computers (Windows, Linux and Apple) and also on tablets (Android and IOS) and mobile phones (Android and IOS). So you write one program and it runs on all systems !!! That saves a lot of developing time. Magic !!

Next to that you do not need a developer account (25USD once for Google and 99USD yearly for Apple), you do not need to publish through the app stores. And becoming a new developer with Google means you need to find 12 testers that test your app for 14 days !!  Even better you do not need to comply to their rules !!!
PWA's means getting your freedom as a developer back !!!

The programming language for PWA's is Javascript.
Actually I am writing a book about this. If you are interested in a book on this subject drop me a note. 

So I am writing a lot of small Javascript programs. And I want to test them.
Testing Javascript is actually easy. Just put it into an HTML file and open it in your browser. It will work immediately. And opening the developer console in your browser (More tools==>Web Developer Tools==>Console) you can even see what's wrong if there is a bug in your code.

Sidenote.

Javascript is a fun language which is not very difficult to learn. And you get instant results in your webbrowser. The language is useful for all kinds of projects, including IOT projects of which you find several on this weblog.
To make programming in Javascript easier I collected over 500 tips and tricks and bundled them in a book. The book is distributed world-wide by amazon.com



Click here to learn more about this book or order it


And sometimes I want to test these programs running from a real webserver.
In 2024 I wrote a story on a small reaction game I developed. It is pure vanilla Javascript (no frameworks and no libraries) and works surprisingly well. The source code for the game is in the story. You can find it here: 
https://lucstechblog.blogspot.com/2024/03/javascript-reaction-game.html

I used Github to store the html code and turn the Github repositry into a webpage. You can do the same. I have written how to do that in this story:  
https://lucstechblog.blogspot.com/2023/07/using-github-for-building-website.html

But to do so the first step is to create a Github repository. Then upload your html code. And then turn that repository into a website. All in all this takes a bit of time. And in the end you have a lot of repositories that you might not need so you have to delete them all again.

And then I found an easy way to get a temporary website.

Netlify Drop

Netlify is a company that offers a lot of webservices. And one of these services is Netlify Drop.
You can use this for free and you even do not have to log in !!!!


Just put your HTML file with CSS and Javascript in a folder on your computer. I made a folder called React-game (while writing this story). 
For those wondering: I am running Linux on my computer (actually a Raspberry Pi5) but don't worry this will work on Windows equally well !! And as I am not getting sponsored I do not have a Mac so I can not test that.


In that folder I put my reaction game HTML file. Do remember to rename the HTML file to index.html because every websites starting point is the file index.html.

Next open your webbrowser (I am using Firefox) and visit:
https://app.netlify.com/drop


Drag your folder with your test program to the webpage and you are done !!!


The folder is uploaded to Netlify and after a few seconds (depending on the size of your program and the number of files) the screen alters.

Your site is now alive. The name is at the top in a different color (green-blue) and in this example is:
https://dynamic-bunny-771e26.netlify.app

You can copy this name and put it in your browsers address bar. You can also just click on the name and the site will open.


The site is password protected. But in this case that is just a way to keep bots out cause the password for each site is the same: My-Drop-Site
The password was shown in the first screen where it mentioned that your site is alive !!

Just paste that name in and press the submit button and your site opens and can be tested.





Try it yourself.

Create a new folder on your computers storage.
Then copy the code from my reaction game from here:
https://lucstechblog.blogspot.com/2024/03/javascript-reaction-game.html
Or use your own HTML creation.
Just paste it in any editor and then save it as index.html in the just created folder.
Drag the folder to https://app.netlify.com/drop and you are off to the races.

Any Caveats ????

Well yes there are some things to consider.

First the name of your site is not something you will easily remember. In this example like you can see it is: https://dynamic-bunny-771e26.netlify.app/
So copy and store that name somewhere if you need to use it frequently.

And that is the other caveat: your site will only be available for one (1) hour.
Now one hour does not seem a lot, but remember that a single html file is loaded in the memory of your browser and will stay there operational until you close the browsers tab.

The sunny side

Your code will be removed after an hour. So anybody looking at this website now will get a page not found error or likewise. So your code is rather safe.,

You can use this many times a day for testing different websites. So you can test a lot, before making a real production site for it.

Long-time usage.

Sometimes a one hour test period is not enough.
If you need more just go to the Netlify webpage: https://www.netlify.com/
Sign up. Start a new project and dump your folder in it.
The, this way, created site will last for ever.

Netlify will grant you (at this moment) 300 credits per month for free. Each website you create this way will cost 15 credits. So effectively you can create 20 websites each month however........

Every GB of data that is transferred will cost you 10 credits. So if you have loads of people visiting your test-site that will cost a lot of credits. For small sites like my reaction game (which is only 1K) a GB will go a long way.

When you run out of credits your sites are put on hold till the next month.
Of course you can have paid subscriptions with a lot more credits.

Concluding

For quickly testing small websites this is a great solution.
The one hour limit is in my test phase not a problem and for some long time testing purposes I have made an account and applied some test sites that way.

The only reservation I have is that this is again a cloud based service and I have seen quite a few of these services suddenly limiting the free tier or stopping alltogether.
So to be really safe I am building my own webserver on a humble Raspberry Pi3. But that's for another story.


Till next time
have fun

Luc Volders