So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Dynamo
#91353 Hello, I would like to use a gyroscope (gy-521) and a rotary encoder (ky-040). But when i come to connect it I have noticed that people usually connect it to the same pins (D1 and D2) . I would like to know if it is possible to use both modules with the esp8266 and if possible how to connect them.
Thank you
User avatar
By QuickFix
#91361 Yes, you can (of course you can ;) ), but you have to give us something to go on first:
  • What ESP are you planning to use (please don't say it will be an ESP-01... pretty please)?
  • What have you tried yourself up to now?
  • Do you have a specific reason to use other GPIO's than commonly used?
The GY-521 uses I2C (so that's two GPIO's needed for the gyro) and the rotary encoder uses 2 GIO's for the turning pulses and (optionally) 1 GPIO extra if you want to use the button-press option: it's not possible to share GPIO's with these two modules, so you would have to use 2 + 2 (+ 1) = 4 (or 5) GPIO's to have both connected and working.
(Note: although the rotary module says that it needs 5V you can feed it with 3.3V of the ESP without problems, since it actually is just a couple of switches).

Things to watch out for: not all GPIO's are the same, some can not be used as is without getting in trouble at boot (GPIO0, GPIO2 and GPIO15 control the boot state) and there are also other GPIO's that are less than ideal to use: for an overview, see this page on Random Nerd Tutorials.com. :idea:
User avatar
By Dynamo
#91362 I am planning to use the ESP8266 ESP-12E. I'm waiting for the module (encoder) to arrive, but I wanted to know beforehand how to use it, but reading on the internet I must have misunderstood the information.

So I can connect the rotary encoder to other GPIOs.

I would like to know your opinion if there could be problems using the encoder and the gy-521 , as the encoder will make a lot of interference when rotating, right?

Thanks for the quick reply.
User avatar
By QuickFix
#91364
Dynamo wrote:I am planning to use the ESP8266 ESP-12E.

I want to suggest you start with a development board, like a NodeMCU, first before trying to get a bare module working, especially when your new. :idea:
Once you got the hardware and software working as intended, you can opt to design your own schematics and PCB using any of the available ESP modules.

Dynamo wrote:I would like to know your opinion if there could be problems using the encoder and the gy-521 , as the encoder will make a lot of interference when rotating, right?

I've only played with an encoder once (I personally only use normal buttons), but getting it working properly can be tricky: in my experience using an interrupt would be the most reliable option, but I believe there are a couple of topics on the forum that deal with this subject.
Depending on the type of project you have to consider using either hardware or software de-bouncing though, because with either (encoder of normal switches) you will get bounced. ;)

Talking I2C is easy: just include the Wire-library, set the GPIO's the device(s) is/are connected to and talk.

To check the existence/address of (each) I2C device on the bus you can use an I2C scanner sketch like below:
Code: Select all#include "Wire.h"

void setup(){
  Serial.begin(115200);
  while(!Serial){} // Waiting for serial connection
 
  Serial.println();
  Serial.println("Start I2C scanner ...");
  Serial.print("\r\n");
  byte count = 0;
 
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission(i);
    if (Wire.endTransmission() == 0)
      {
      Serial.print("Found I2C Device: ");
      Serial.print(" (0x");
      Serial.print(i, HEX);
      Serial.println(")");
      count++;
      delay(1);
      }
  }
  Serial.print("\r\n");
  Serial.println("Finish I2C scanner");
  Serial.print("Found ");
  Serial.print(count, HEX);
  Serial.println(" Device(s).");
}

void loop() {}
Last edited by QuickFix on Tue May 11, 2021 7:05 am, edited 1 time in total.