Atmega328P Standalone I2C Problem

Hey Guys,
I don’t understand life anymore since I tried everything to make this work:
The Atmega328P running on a breadboard as an 8mhz internal configuration and it is bootloaded/programmed by an Arduino Uno as ISP. Programming for Blink Sketch works fine.
Now the situation is as follows:
-I try to connect the AtMega to an I2C LCD
5V – Vcc
GND – GND
Pin 20 (A5 ) – SCL
Pin 19 (A4) – SDA
-Using the ‘Hello World Example’, text should appear when uploaded and connected.
-Using an I2C scanner sketch, an LED on D13 should light up when it detects an I2C device.
All of the above works using an Arduino Uno but does not when using the Breadboard AtMega

Thanks in advance

Florian

PS: Code
“Hello World example”
Arduino Uno: LCD shows the the text that is sent to it
AtMega Standalone: LCD does nothing

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  lcd.print("Ywrobot Arduino!");
   lcd.setCursor(0,2);
  lcd.print("Arduino LCM IIC 2004");
   lcd.setCursor(2,3);
  lcd.print("Power By Ec-yuan!");
}


void loop()
{
}

I2C Scanner from Arduino Website:
Arduino Uno: writes to Serial Monitor that a device is found at adress 0x27, LED lights up when LCD is connected, goes out when disconnected.
AtMega Standalone: (Cannot check serial monitor); LED does not light up when LCD is connected.

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      digitalWrite(13,HIGH);
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0){
    Serial.println("No I2C devices found\n");
    digitalWrite(13,LOW);
  }
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

Do you have pull ups on the I2C lines?

Not on all tests, but i tried 5.1k pullups on SDA and SCL on the breadboard.
I thought the AtMega had internal pullups.

Only if you turn them on. And even then, I'm not sure they work with I2C. For that you should dig through the datasheet.

It works now, dammit! the breadboard I am using is probably very useless as the rows sometimes lose contact. But if I put the AtMega into my circuit, it will not work either.
The circuit is a perfboard (EBike Motor Controller) which is the Motherboard. A Daughterboard is connected with an ethernet cable. Can I somehow increase the "stability" of the apparatus? Can I decrease the I2C frequency to counteract the effects of the parasidic capacities/inductances? Or bypass caps? I am using 4.3k pullups on the Motherboard btw. Should I post circuit diagram?

I2C is already pretty slow. But how long is that ethernet cable (and which wires did you use for what)? I2C is NOT designed for length. More than 30cm should be avoided.

The daughterboard is meant to be mounted at the handlebar (I am building an electric bicycle) and the Mainboard (motor controller/brain unit) and right now they are connected through an Ethernet cable between 1 and 2 metres long.
There are only 2 slaves on the daughterboard. An I2C LCD that is only updated 5-10 times a second and an AtTiny from which 1 byte is read from 10-20 times a second.

With 1 or 2 meters you’re out about 3 to 7 times the length a where I2C is designed for… You can try lower value pull ups but it is kind of like using a hammer to drive in a screw. It’s just not the right tool.

As long as you stay within the standard I2C specs of 3mA/400pF.

That means <= 6meters of Cat-5/6 if you put SCL/GND on one pair and SDA/GND on another pair.

I run standard 100kHz I2C with ~4k7 pull up on both ends of ~5meters Cat/6 wire 24/7 without errors.
Leo…

Wawa:
As long as you stay within the standard I2C specs of 3mA/400pF.

That only keeps it in spec as in driveable. Does not include coupling of noise.

But yeah, you can extend it a bit. Use a shielded cable, use the right wires in a twisted pair cable and keep it away from noisy crap :slight_smile: But yeah, motors are very good at generating that :confused: Stupid mechanics...

Now what should I do??? :confused:
Besides measuring the capacitance

What I know is I'll put bypass caps on the supply of the daughterboard. And Arduino

Do you want a best approach (a) or a might work approach (b)?

For (a), switch to a proper line driver. There are long range I2C drivers. Or use a controller in the handlebar as well and use RS485 or something

For (b), connect SDA and SCL to a different pair and the other wires to GND. Use a shielded cable and connect the shield on one side to GND. And I think I would go to 1k for the pull ups but I did no calculations for it.

Haha well bc I don't want to wait for a new chip to arrive I'll try b) for now. I only use the colored cables of the pairs so the white wires are NC bc I bodged the Ethernet Jack onto my perfboard. NC counts for the shielding as well. Probably a bad job. If everything strikes I can still directly hard wire the boards together and limit the cable to 30cm