How to add Blynk to my MPU-6050 "Movement Detector"

Hi guys, I’m using an MPU-6050 as a movement detector. Yes I guess it’s overkill but I’m a complete noob, not only to this wonderful app called Blynk but to all aspects of coding too. So please be gentle with me, as I’m here to learn, hopefully.

I’ve been trying to add Blynk to my code and I need help. I did the help center and then the sketch builder and learnt a little, but not enough.

The MPU-6050 gives me Pitch and Roll data, zero’d at startup. I use this data to turn on an LED when the pitch or roll is +5 or -5. A lit LED = movement detected.

What I’d like to do is send the pitch and roll data to 2 Visual Displays and if the pitch or roll is + or - 5 then light their corresponding LED’s in the app. I also want to send a message to an LCD display in the app “MOVEMENT DETECTED” for example.

This is it doing its thing. So where do I start?


const int analogPin = A0;
const int ledPin = 13;
const int threshold = 400;

const int MPU = 0x68;
int t = 0,dt = 1;
int AcX,AcY,AcZ,GyX,GyY,GyZ,tmp;
int AcXo,AcYo,AcZo,GyXo,GyYo,GyZo;
float roll = 0,pitch = 0,rollgy = 0,pitchgy = 0,rollac = 0,pitchac = 0,Ax,Ay,Az,Gx,Gy,Gz,gain = 0.95;
void MPUconfig(int Addr,int data)
void MPUread()
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.requestFrom(MPU,14);  // request a total of 14 registers<<8|;  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)<<8|;  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)<<8|;  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)<<8|;  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)<<8|;  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)<<8|;  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)<<8|;
void offset()
  int ax = 0, ay = 0,az = 0,gx = 0,gy = 0,gz = 0,i;
ax = AcX;
ay = AcY;
az = AcZ;
gx = GyX;
gy = GyY;
gz = GyZ;
      ax = (ax+AcX)/2;
      ay = (ay+AcY)/2;
      az = (az+AcZ)/2;
      gx = (gx+GyX)/2;
      gy = (gy+GyY)/2;
      gz = (gz+GyZ)/2;
AcXo = ax;
AcYo = ay;
AcZo = 16384-az;
GyXo = gx;
GyYo = gy;
GyZo = gz;
void setup() 
  pinMode(ledPin, OUTPUT);

void loop()
  t = millis();
  Ax = (float)(AcX-AcXo)/16384;
  Ay = (float)(AcY-AcYo)/16384;
  Az = (float)(AcZ-AcZo)/16384;
  Gx = (float)(GyX-GyXo)/131;
  Gy = (float)(GyY-GyYo)/131;
  Gz = (float)(GyZ-GyZo)/131;
  pitchgy = (Gy * ((float) dt/1000)) + pitch;
  rollgy = (Gx * ((float) dt/1000)) + roll;
  pitchac = atan2(Ax,Az) * (float)(180/PI);
  rollac =  atan2(Ay,Az) * (float) (180/PI);
  roll = gain * rollgy + (1-gain)* rollac;
  pitch = gain * pitchgy + (1-gain) * pitchac; 
  Serial.print(" ");
  Serial.println(" ");
  dt = millis()-t;

  if ( (abs(pitch) > 5) || (abs(pitch) < -5) || (abs(roll) > 5) || (abs(roll) < -5)) {
  digitalWrite(ledPin, HIGH);
} else {
  digitalWrite(ledPin, LOW);


This code will need a LOT of restructuring to move all of that stuff out of void loop() so that it will work well with Blynk.

I saw that you asked the guy what board he was using for the earthquake detector and I assume his response put you off?
It would be much easier to change his code to work with your ESP board than restructure the code above.

The guy with the earthquake thingy is using an Arduino Uno connected to an ESP8266. The only purpose of his ESP8266 is to act as a Wi-Fi modem for his Uno board (a bit crazy because the ESP board on its own is much more powerful than the Uno.
Use the Sketch Builder to create two different versions of a simple code (blink an led for example). Create one version for the ESP8266 and another for Uno+ESP8266. Compare the two versions and you’ll see that the Uno+ESP code used software serial to communicate with the ESP.
Strip the code that relates to the Uno+ESP our of the earthquake sketch and replace it with the code that established the Wi-Fi & Blynk connection from one of your working sketches. Change the GPIO pins to suit your hardware connections and Bob’s your uncle.


1 Like

@PeteKnight Well that’s disapointing. Once I’d taken delivery of the 4 WeMos D1 Pro’s I’ve brought and one of them was fitted to my bike. I was hoping to be able to view the Pitch and Roll data in the app on my phone when someone else was riding my bike. :cry:

Now i know why I’ve failed so miserably at trying to do this over the past 24 hours. :confounded:

Okay, a bit of ‘scope creep’ from the initial project brief then :grinning:
How would the ESP fitted to the bike connect to the internet to log/shop this data in Blynk? You could use a mobile phone with a personal hotspot enabled, but you’d neeed to Ruth use Blynk provisioning, Wi-Fi Manager or have some code that tried a second Wi-Fi SSID if the first disconnected - unless your personal hotspot had the same SSID and password as your home Wi-Fi.

I still think that using the earthquake code as a basis to add a few more bells and whistles once it’s running would be a better starting point than the code you posted above.


1 Like

That was my plan, to set up a hotspot on the mobile phone of whoever was riding my bike, with the same SSID and Password as my home WiFi. I also planned to do the same at work with a WiFi extender so my bike logs in there too.

Being able to view the pitch and roll is just for fun, especially on track days. Be great to add accelerometer data to, even log each race. Be a great opportunity to use the share option(half a cup of coffee @Gunner :wink:) to so friends could see what each other was doing.

I thought you may have caught on to what it was from our previous conversion in my other thread. :wink:

If you have your phone mounted on the bike, then you can use it’s built in sensors… and Blynk’s ready made commands to read them. Introduction - Blynk Documentation

No good for guarding in the garage, but then a simple motion trip switch on a digital pin would work as well.

That’s not allowed, even a Go Pro has to be securely bolted in specific positions if they allow it, most don’t.

Oh well, it was worth a try, wasn’t aware Blynk is difficult to integrate into existing code. From what I’d seen others do I thought it was easy. I guess I’ll have to stick to using my old Yale IR Shed Alarm and continue to exaggerate my track day abilities. :wink:

My understanding is that the “garage” is within range of the home WiFi so Blynk will protect the motorbike.

Once you are familiar with Blynk and the code you want to use then it’s a very simple process of putting the two together.

Yes, but not using the phone’s built in sensors… unless he keeps it on the bike. “Oh, look, free bike AND phone:stuck_out_tongue_winking_eye:

Using the MPU.

@Shadeyman as far as I can tell you want to build a project with 2 main features:

  1. Alarm on the “bike” when located at home.
  2. Telemetry for race days.

1 is easy enough. For 2 you will have to consider:

  1. Does the track have WiFi that will cover the whole of the circuit.
  2. Would the race organisers accept an MPU, ESP and maybe Pi Zero W (server) on the bike. You have indicated they will not accept a phone on the bike so they might not accept these components either.
1 Like

@Costas Yes, that pretty much sums it up.

Some tracks do have WiFi but I’d not rely on that. Use a mobile phone’s hot spot, tucked away in the riders leathers wouldn’t be a problem.

An MPU-6050 and a Wemos D1 Pro inside a small project box fastened securely to the frame of the bike would not be an issue, no different to a standard bolt on bike alarm. They just don’t like stuff attached to the exterior of the bike(or car).

Then your phone would still work for use of it’s GPS and accelerometer… added bonus of no need for the MCU to even be there, that could sit at home (or more likely the shed, hooked up to any other sensors for it’s guard duty purposes) and record the data from the phone.

As long as you stay on the bike as it goes down the track :wink: then even with it in your pocket, you will get enough accuracy of the general acceleration and speed.

As for the shed part… again with the MCU mounted in it, but perhaps using ultrasonic and/or PIR sensors for movement as well as a dedicated BT beacon sensor (attached to the bike) or even short range RF transmitter… and giving alarm if the bike is moved out of range of the MCU.

Just think of all those sensors and code :stuck_out_tongue:

@Gunner But that would require my phone to be with the bike all the time, that leaves me with no phone so I can’t view the data?

My idea is to give whoever rides my bike a specific SSID and password to use for their hotspot, that way the bike logs in automatically …

Ah, I see… you let others ride your bike… very generous :wink: Yes, I was looking at it as a case of you being the only rider and the security part primarily while in the shed.

Then the only real difference there is mounting the MCU somewhere in the frame and running off the bike battery system. And perhaps using a 2nd dedicated (aka older) phone for the sensor part? but with the MCU on the bike, then adding sensors is not the issue… I was just thinking of the easier way of reading said sensors by using a phone.

We ride each others bikes, and always brag how much better our own bikes are. :sweat_smile:
Would be great to be able to compare each others riding styles(on my bike) by watching the data live and comparing it afterwards. If it works I’m pretty sure I could sell the idea to the other lads. Have you seen the prices of some of the Racelogic stuff?(drinks are on me. :wink: ).

Not sure the others would want to be carrying old phones, don’t want to go down that avenue.

You already have a MPU sensor, so not disparaging your use of it… was just thinking out of the box. I use older phones for dedicated, non-phone purposes all the time.

Just more spitballing… I was thinking that one could also remove the phone battery, rig up for external power (for safety - I have done that for after the battery got all fat and hot headed :stuck_out_tongue_winking_eye: ), drop in a cheapish data only SIM card and secure the whole thing permanently as another ‘gadge’ in a vehicle that offers touch display, WiFi hotspot and sensor package all in one. I am pondering along those lines with an older cellular data capable tablet… as a Blynkified, multi-function display/control center, for my truck.

After deciding I wanted to learn about coding only 3 weeks ago I chose to purchase several cheap sensors for me to play with, that included 4 MPU’s. Why 4 I hear you ask, they were cheap, and surprisingly accurate. As were all the others.

Again, I don’t like the idea of using an old phone, pretty sure the lads at the track wouldn’t be very impressed either. I’m sure just giving them a simple SSID and password for their hotspot would be much more appealing.

Use an iPhone… +10 ‘prestige’ points with the lads :stuck_out_tongue_winking_eye:

I know, I know… just yanking your chain… no more silly ideas from the peanut gallery, got it :wink: