user86159: I am trying to make an observatory through 2 GY-521 gyroscopes and using the outputs I have to make a motor move, but I have a problem.
When I move one of the two gyroscopes the outputs change, but then as soon as I leave it it returns to the initial value, the data I see via an ESP8266 on an internet site, this is the code:
#include <Wire.h>
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

char ssid[] = "Vodafone-A61356555";            // your network SSID (name)
char pass[] = "YgtHnkJHNpFAcPfH";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received

WiFiEspServer server(80);

int MPU = 0x69;
int MPU1 = 0x69;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int16_t AcX1, AcY1, AcZ1, Tmp1, GyX1, GyY1, GyZ1;
int gyro_state = 2;
int gyro_state_1 = 3;
int RED_LED = 8;
int GREEN_LED = 9;

void setup(){
  pinMode(gyro_state, OUTPUT);
  pinMode(gyro_state_1, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(gyro_state, HIGH);
  digitalWrite(gyro_state_1, HIGH);
  Wire.begin(); 
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}

void loop() {
  // listen for incoming clients
  WiFiEspClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
          
          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            //"Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "Refresh: 5\r\n"
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("<br>\r\n");
          client.print("Accelerometro Telescopio X: ");
          client.print(AcX);
          client.print("  Accelerometro Cupola X: ");
          client.print(AcX1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Accelerometro Telescopio Y: ");
          client.print(AcY);
          client.print("  Accelerometro Cupola Y: ");
          client.print(AcY1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Accelerometro Telescopio Z: ");
          client.print(AcZ);
          client.print("  Accelerometro Cupola Z: ");
          client.print(AcZ1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Giroscopio Telescopio X: ");
          client.print(GyX);
          client.print("  Giroscopio Cupola X: ");
          client.print(GyX1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Giroscopio Telescopio Y: ");
          client.print(GyY);
          client.print("  Giroscopio Cupola Y: ");
          client.print(GyY1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Giroscopio Telescopio Z: ");
          client.print(GyZ);
          client.print("  Giroscopio Cupola Z: ");
          client.print(GyZ1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("Temperatura Telescopio: ");
          client.print(Tmp);
          client.print("  Temperatura Cupola: ");
          client.print(Tmp1);
          client.print("<br>\r\n");
          client.print("<br>\r\n");
          client.print("</html>\r\n");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(100);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
  digitalWrite(gyro_state, HIGH);
  digitalWrite(gyro_state_1, LOW);

  do {
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 14, true);       // request a total of 14 registers
    AcX = Wire.read() << 8 | Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
    AcY = Wire.read() << 8 | Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ = Wire.read() << 8 | Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    Tmp = Wire.read() << 8 | Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
    GyX = Wire.read() << 8 | Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY = Wire.read() << 8 | Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ = Wire.read() << 8 | Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

    Tmp = Tmp / 340 + 36.53;
    
    Serial.println("First Gyro");
    Serial.println(GyZ);

    digitalWrite(gyro_state_1, HIGH);
    digitalWrite(gyro_state, LOW);
    delay(1000);

  } while(digitalRead(gyro_state)== HIGH);

  do {
    Wire.beginTransmission(MPU1);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU1, 14, true);       // request a total of 14 registers
    AcX1 = Wire.read() << 8 | Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
    AcY1 = Wire.read() << 8 | Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ1 = Wire.read() << 8 | Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    Tmp1 = Wire.read() << 8 | Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
    GyX1 = Wire.read() << 8 | Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY1 = Wire.read() << 8 | Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ1 = Wire.read() << 8 | Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

    Tmp1 = Tmp1 / 340 + 36.53;
    
    Serial.println("Second Gyro");
    Serial.println(Gy1);

    digitalWrite(gyro_state, HIGH);
    digitalWrite(gyro_state_1, LOW);
    delay(1000);


  } while(digitalRead(gyro_state_1) == HIGH);

  if (GyY > GyY1) {
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
  }
  else if (GyY < GyY1) {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  
  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

Output:
First Gyro
1503
Second Gyro
3730
First Gyro
1558
Second Gyro
-162
First Gyro
1498
Second Gyro
-164

Can anyone explain to me why once I stop moving the gyroscope, the value returns to the initial position? And if it is possible to convert to degrees?

user57952: The GY-521 and the MPU6050 are the same functionally, and you need to use one of the several libraries to get what you want.  It appears you are retrieving angular rate values, but what you want is relative heading. The angular rate will return to zero (or whatever the initial value was) once it stops moving, but the heading will (with proper treatment) stay constant.
I have used the MPU6050 in several projects with success, using Jeff Rowberg’s I2CDevLib library, and although getting everything set up is complicated, I recommend it.  See this post for basic setup.
Also, I note that most of your program has nothing at all to do with the gyroscope.  I recommend you use the smallest and simplest possible program to work out the hardware issues before adding the more complex bits.

86159: <p>I am trying to make an observatory through 2 GY-521 gyroscopes and using the outputs I have to make a motor move, but I have a problem.</p>
<p>When I move one of the two gyroscopes the outputs change, but then as soon as I leave it it returns to the initial value, the data I see via an ESP8266 on an internet site, this is the code:</p>
<pre><code>#include &lt;Wire.h&gt;
#include &quot;WiFiEsp.h&quot;
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include &quot;SoftwareSerial.h&quot;
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

char ssid[] = &quot;Vodafone-A61356555&quot;;            // your network SSID (name)
char pass[] = &quot;YgtHnkJHNpFAcPfH&quot;;        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received

WiFiEspServer server(80);

int MPU = 0x69;
int MPU1 = 0x69;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int16_t AcX1, AcY1, AcZ1, Tmp1, GyX1, GyY1, GyZ1;
int gyro_state = 2;
int gyro_state_1 = 3;
int RED_LED = 8;
int GREEN_LED = 9;

void setup(){
  pinMode(gyro_state, OUTPUT);
  pinMode(gyro_state_1, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(gyro_state, HIGH);
  digitalWrite(gyro_state_1, HIGH);
  Wire.begin(); 
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&amp;Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(&quot;WiFi shield not present&quot;);
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print(&quot;Attempting to connect to WPA SSID: &quot;);
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println(&quot;You're connected to the network&quot;);
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}

void loop() {
  // listen for incoming clients
  WiFiEspClient client = server.available();
  if (client) {
    Serial.println(&quot;New client&quot;);
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' &amp;&amp; currentLineIsBlank) {
          Serial.println(&quot;Sending response&quot;);
          
          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            &quot;HTTP/1.1 200 OK\r\n&quot;
            //&quot;Content-Type: text/html\r\n&quot;
            &quot;Connection: close\r\n&quot;  // the connection will be closed after completion of the response
            &quot;Refresh: 5\r\n&quot;
            &quot;\r\n&quot;);
          client.print(&quot;&lt;!DOCTYPE HTML&gt;\r\n&quot;);
          client.print(&quot;&lt;html&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Accelerometro Telescopio X: &quot;);
          client.print(AcX);
          client.print(&quot;  Accelerometro Cupola X: &quot;);
          client.print(AcX1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Accelerometro Telescopio Y: &quot;);
          client.print(AcY);
          client.print(&quot;  Accelerometro Cupola Y: &quot;);
          client.print(AcY1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Accelerometro Telescopio Z: &quot;);
          client.print(AcZ);
          client.print(&quot;  Accelerometro Cupola Z: &quot;);
          client.print(AcZ1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Giroscopio Telescopio X: &quot;);
          client.print(GyX);
          client.print(&quot;  Giroscopio Cupola X: &quot;);
          client.print(GyX1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Giroscopio Telescopio Y: &quot;);
          client.print(GyY);
          client.print(&quot;  Giroscopio Cupola Y: &quot;);
          client.print(GyY1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Giroscopio Telescopio Z: &quot;);
          client.print(GyZ);
          client.print(&quot;  Giroscopio Cupola Z: &quot;);
          client.print(GyZ1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;Temperatura Telescopio: &quot;);
          client.print(Tmp);
          client.print(&quot;  Temperatura Cupola: &quot;);
          client.print(Tmp1);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;br&gt;\r\n&quot;);
          client.print(&quot;&lt;/html&gt;\r\n&quot;);
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(100);

    // close the connection:
    client.stop();
    Serial.println(&quot;Client disconnected&quot;);
  }
  digitalWrite(gyro_state, HIGH);
  digitalWrite(gyro_state_1, LOW);

  do {
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 14, true);       // request a total of 14 registers
    AcX = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3B (ACCEL_XOUT_H) &amp; 0x3C (ACCEL_XOUT_L)
    AcY = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3D (ACCEL_YOUT_H) &amp; 0x3E (ACCEL_YOUT_L)
    AcZ = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3F (ACCEL_ZOUT_H) &amp; 0x40 (ACCEL_ZOUT_L)
    Tmp = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x41 (TEMP_OUT_H) &amp; 0x42 (TEMP_OUT_L)
    GyX = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x43 (GYRO_XOUT_H) &amp; 0x44 (GYRO_XOUT_L)
    GyY = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x45 (GYRO_YOUT_H) &amp; 0x46 (GYRO_YOUT_L)
    GyZ = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x47 (GYRO_ZOUT_H) &amp; 0x48 (GYRO_ZOUT_L)

    Tmp = Tmp / 340 + 36.53;
    
    Serial.println(&quot;First Gyro&quot;);
    Serial.println(GyZ);

    digitalWrite(gyro_state_1, HIGH);
    digitalWrite(gyro_state, LOW);
    delay(1000);

  } while(digitalRead(gyro_state)== HIGH);

  do {
    Wire.beginTransmission(MPU1);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU1, 14, true);       // request a total of 14 registers
    AcX1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3B (ACCEL_XOUT_H) &amp; 0x3C (ACCEL_XOUT_L)
    AcY1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3D (ACCEL_YOUT_H) &amp; 0x3E (ACCEL_YOUT_L)
    AcZ1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x3F (ACCEL_ZOUT_H) &amp; 0x40 (ACCEL_ZOUT_L)
    Tmp1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x41 (TEMP_OUT_H) &amp; 0x42 (TEMP_OUT_L)
    GyX1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x43 (GYRO_XOUT_H) &amp; 0x44 (GYRO_XOUT_L)
    GyY1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x45 (GYRO_YOUT_H) &amp; 0x46 (GYRO_YOUT_L)
    GyZ1 = Wire.read() &lt;&lt; 8 | Wire.read();  // 0x47 (GYRO_ZOUT_H) &amp; 0x48 (GYRO_ZOUT_L)

    Tmp1 = Tmp1 / 340 + 36.53;
    
    Serial.println(&quot;Second Gyro&quot;);
    Serial.println(Gy1);

    digitalWrite(gyro_state, HIGH);
    digitalWrite(gyro_state_1, LOW);
    delay(1000);


  } while(digitalRead(gyro_state_1) == HIGH);

  if (GyY &gt; GyY1) {
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
  }
  else if (GyY &lt; GyY1) {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to
  Serial.print(&quot;SSID: &quot;);
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print(&quot;IP Address: &quot;);
  Serial.println(ip);
  
  // print where to go in the browser
  Serial.println();
  Serial.print(&quot;To see this page in action, open a browser to http://&quot;);
  Serial.println(ip);
  Serial.println();
}
</code></pre>
<p>Output:</p>
<pre><code>First Gyro
1503
Second Gyro
3730
First Gyro
1558
Second Gyro
-162
First Gyro
1498
Second Gyro
-164
</code></pre>
<p>Can anyone explain to me why once I stop moving the gyroscope, the value returns to the initial position? And if it is possible to convert to degrees?</p>
 57952: <p>The GY-521 and the MPU6050 are the same functionally, and you need to use one of the several libraries to get what you want.  It appears you are retrieving angular rate values, but what you want is relative heading. The angular rate will return to zero (or whatever the initial value was) once it stops moving, but the heading will (with proper treatment) stay constant.</p>
<p>I have used the MPU6050 in several projects with success, using Jeff Rowberg’s I2CDevLib library, and although getting everything set up is complicated, I recommend it.  <a href="https://www.fpaynter.com/2019/10/basic-arduino-mpu6050-gy-521-test/" rel="nofollow noreferrer">See this post</a> for basic setup.</p>
<p>Also, I note that most of your program has nothing at all to do with the gyroscope.  I recommend you use the smallest and simplest possible program to work out the hardware issues <em>before</em> adding the more complex bits.</p>
