Understanding the Arduino Mega I2C pins electronics

I am trying to check whether there are any resistors missing from my Arduino set up.

Basically, I have connected two GY-521 gyroscopes to my Arduino Mega 2560 using I2C (addressing each sensor separatedly by connecting one of them to VCC and the other to GND through their AD0 pins). This is the Fritzing schematic:

The thing is, I don't have an amazing grasp on electronics and I don't know if I should be connecting the SCL and SDA cables to an external pull-up resistor. As far as I know, the Wire.h library that I used to establish the I2C protocol automatically activates the internal pull-up resistors from the pins 20 and 21 (here, lines 88 and 89).

I'm guessing those two lines are referring to the 50k ohm resistor that is integrated in the ATmega2560 chip and not the 10k ohm resistors that are integrated into the circuit and cannot be disabled.

Am I about to fry my sensors or is my set up alright?

Here is my code, for reference:

#include <Wire.h>

//I2C adressing
#define MPU 0x68
#define MPU2 0x69

int16_t AcX, AcY, AcZ, AcX2, AcY2, AcZ2;

int LED = 52;

const int FC = 13;
int state_FC = 0;
int cont_FC = 0;
int prestate_FC = 0;

const int FIX = 12;
int state_FIX = 0;


void setup() {

  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);

  Wire.begin();
  Wire.beginTransmission(MPU2);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  
  Serial.begin(9600);

  pinMode(FC, INPUT);
  pinMode(FIX, INPUT);

  pinMode(LED,OUTPUT);

}

Sorry I cannot help you the Frizzy Picture is useless, no real information. There are no links to "Technical information" on the hardware items, no power source. Azon links are basically useless they do not have enough technical info. You may get lucky but I would recommend something in the 4.7K range pull up resistors for SCL and SDA. "50K" will not reliably be enough current. What 10K resistors are you talking about.

Your I2C setup looks fine although I dont know why you are poweroing the gyros from 3V3.

Why have you chosen to use switches with pull-downs for inputs? There are important benefits in using the arrangement of the switch to ground with a pull-up. (thats why internal pull-ups are provided)

The honest answer is that I've done all that because it's pretty much the first Arduino thing I do on my own and I'm a bit lost.

Should I not be powering the gyros at 3.3V? Thought I'd break them if I powered them with 5V.
Here is the datasheet, sorry I forgot to link it before.

Also, about the switch to ground with a pull-up, what are the benefits of connecting a pull-up resistor versus connecting a pull-down one? I thought it was just to prevent the floating state problem.
And I'm guessing I could just set the input pin for the button to pinMode(button_pin, INPUT_PULLUP) and forget about external resistors, right?

Thanks for the help, it is very appreciated.

I'm talking about the 10kOhm resistors that are built into the Mega 2560 board for the SDA and SCL pins.
Here is an extract from the official scheme:

Unfortunately, that data sheet is for the main mpu6050 chip only. Your sensor boards contain other components, and we need to know what they are and how they are connected in order to answer your questions.

This may be it:

The page says

Vcc

Provides power for the module, can be +3V to +5V. Typically +5V is used

It also shows 4K7 resistors connecting the SDA & SCL pins to 3.3V, not 5V, which is strange.

MPU6050-Circuit_0

With those 4K7 pull-ups to 3.3V on both modules, plus the 10K pull-up to 5V on the Mega, the SCL & SDA lines will idle at little above 3.3V I think.

1 Like

Welcome to the world of microcontrollers and particularly Arduino. You have achieved a great forward motion by realizing that there is much to learn.

Rather than regurgating somewhat confusing information, I ask you to read:
Pull Up Resistors | Working with I2C Devices | Adafruit Learning System

If you have question, come back and we can discuss more in depth because you will have a better understanding, mainly terminology.

Leaving out pullup in I2C will not damage your Arduino... in fact, some sensor boards have I2C termination resistors which is "nice" but may complicate using multiple sensors on the same I2C channel; or not if the resistors are a higher resistance than recommended for single-ended devices. There is lots of "poetic license" used with break-out boards.

As you are a self-proclaimed newbee, below are some articles and sites you may find benefitial in your early days of experimenting:

1 Like

No, that is the Fritzing breadboard view. For the schematic, you will need to select schematic view from the menu, then arrange the components and draw in the connections. Fritzing will draw some thin straight lines to remind you what is connected to where in the breadboard view, so you don't miss any connections.

To make your schematic more neat and readable, use the "ground" and "5V" or "3.3V" power symbols. Place as many of these as you like, wherever you like them, to avoid having long ground and power lines snaking all over the schematic. See how they are used in the schematic of the sensor boards I posted.

1 Like

Welcome to the forum.

I hope you don't get disappointed by the confusing posts. I will try to make a few things clear, instead of adding to the confusing.

The circuit

I suggest to put the buttons with one leg to GND as johnerrington wrote. It is common practice for sensors and buttons. When the project is put in a box and the wires are added, then a ground wire to everything is simpler and safer and better to reduce noise.

Old versions of the Arduino Mega board or some clones of the Mega board have trouble with pin 13 as a input. Can you use a other pin for the button ?

I made your circuit in Wokwi: Untitled project - Wokwi Arduino and ESP32 Simulator

Investing your time to learn to make a schematic is worth it. I'm using KiCad.

The sketch

Where is the rest of the sketch ? I want to try your sketch in Wokwi. Wokwi does not care about voltage levels.

The I2C bus

To be direct: It will not work, at least not in a reliable way.
Solutions: A I2C level shifter, or a 3.3V Arduino board, or a sensor module that is compatible with 5V Arduino boards.

Can you see what the value of the pullup resistors on the module is ?
afbeelding
Are those resistors on your modules also 2k2 ?

The Arduino Mega board has onboard 10k pullup resistors to 5V, and then the ATmega2560 chip has internal pullup to 5V.
You might already have too much pullup. The sink current (to pull a signal low) should not exceed 3mA according to the I2C standard.

The MPU-6050 is a 3.3V chip. Never apply 5V to AD0. The I2C bus is less critical, but you should apply a I2C bus with 3.3V signals to the chip.
However, if you power the module with 3.3V to VCC, then the onboard voltage regulator drops it to 3.2V. So the MPU-6050 runs at a low voltage. The Arduino Mega board needs 3.5V at SDA and SCL to see it as a HIGH level.
You can try 5V or 3.3V in any combination to any pin or any pullup resistor, but that is not a good solution. You can solve the voltage level conflict on the I2C bus with a I2C level shifter.
The I2C level shifters have also pullup resistors, you might have to remove those 2k2 on the sensor module.

Fake

The genuine MPU-6050 is noisy and outdated. Your MPU-6050 is probably a counterfeit. You can buy a real and modern chip at Adafruit. Adafruit makes their modules compatible for 5V Arduino boards: https://www.adafruit.com/category/641

1 Like

Here's a good app note on I2C bidirectional level shifters:

https://cdn-shop.adafruit.com/datasheets/an97055.pdf

Looking at Figure 2, it appears you just need two N-channel mosfets to do the shifting. You already have pullups to 3.3V on your sensor modules, and you have pullups to 5V on the Mega. So all you would need is the mosfets. I suspect something like a 2N7000 would work, which you hopefully can find in a through-hole version like a TO92 package.

But then the question is whether the pullups should be removed from one of the sensor modules. If you leave them in place on both sensors, then the effective parallel resistance is 1/2 of what the module makers assume you should have, and too much current might be needed to draw those lines low.

1 Like

Thanks for the advise, I will definitely be doing this for my future projects.

I am very sorry for the late response, I kind of left this project to the side when I realized that I was missing some fundamental understanding of electronics. But I gave it a second chance recently and your post is by far the most helpful, so I'm very thankful.

I'll be answering you below:

Indeed I can, done.

It's missing some code because I failed to provide it. If you are still interested, I can send it to you. Thank you vey much for investing your time in remaking my project in Wowki, it helped me see it more clearly.

It says '222', just as in the picture you attached. So yeah 2k2 ohms

Quick recap, just to see if I get it: I have 2k2 from the on-board resistors from the MPU6050 + 10k from the on-board pin resistors in the Arduino + the 50something k pullup resistor integrated in the ATmega2560, because the Wire.h library activates them by default and I have not touched the lines of code dedicated to deactivating these pullups. Right?

Got it, thanks. Having in mind that I don't own a level shifter, the optimal solution for me given the circumstances should be powering the AD0 pin with 3,3v and the VCC pin with 5v, right?

It probably is, given that it's from one of these cheap kits. However, noise is not much of a concern for the use I wanna give them, so I'll try to complete this project with the ones I have and buy some decent Adafruit ones if the latter does not seem feasible.

Again, thanks for your response, it is very much appreciated.

  • c

Not gonna lie, I had to look up what 'bidirectional level shifter' and 'mosfet' were, hahaha. I hope this is a valid indicator of my knowledge about electronics.

I will research it more deeply and try to implement it onece I fully understand it, thank you very much!

The schematics I found indicate that the resistors are 2K2:

I guess the scheme from above is correct because the SM resistors in my MPU6050 have a '222' label written on top of them:

The Mega has onboard 10k pullup to 5V and the module has 2k2 pullup to 3.3V. The resulting level is about 3.6V. Hopefully that is just the right voltage to connect the MPU-6050 SDA and SCL directly to the Mega board.

With two modules, the total sink current is:
5V/50k + 5V/10k + 3.3V/2k2 + 3.3V/2k2 = 3.6 mA
That is above the 3mA which is specified by the I2C standard.
It probably will work, but you could remove those 2k2 from one of the modules.

Yes, correct.
Apply GND or 3.3V to AD0.
Apply 5V to VCC, because the voltage regulator on the sensor module has a voltage drop.

1 Like

That seems like a bad design choice. Often 10K are used, perhaps on the assumption that a few more devices on the bus also with 10K pull-ups will add up to something in the right range. 4K7 and 3K3 are also commonly used because they are recommended for 5V and 3.3V busses. But 2K2... seems a little low!

1 Like

I'll answer you both here because both messages are regarding the 2k2 reistors:

And

Assuming that the 50k pull-up resistors are indeed 50k, because I read somewhere (sorry, I cannot cite the site because I forgot where I saw it) that these resistors can range anywhere from 20k to 50k. If it's something like 20k I'd be leaving even more room for error. But, from what I've gathered, these resistors are not pulled up by default. Anyhow, I'd still be left with ~3.5mA, which still surpases the 3mA constraint.

I'll think about removing these SM resistors.

Got it, thanks!

Thank you both, I think I've gathered all I needed. Cheers!

-c

The MPU6050 datasheet mentions V_low = 0.6V @5mA.
So it can sink 5mA, and still be in the I2C specs.
But true, better not to push things to the limit.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.