1

I try to run an atmega328p as a i2c slave. even with the bare minimum the slave acts as its not wired at all: NACK is transmitted after request (see logic analyzer).

The master should read a constant value in this example. iam using the wire.h lib. pullup resistors on SDA, SCL 4.7kOhm.

master.ino: esp8266 (working, sends requests)

#include <Wire.h>
#define DEFAULT_SLAVE_ADDRESS 0x0A
void setup()
{
 Wire.begin(); // join i2c bus as master
 Serial.begin(9600);
 Serial.println("\n Hello");
 delay(2000);
}
void loop()
{
 //Wire.beginTransmission(DEFAULT_SLAVE_ADDRESS);
 //Wire.write(0);
 //Wire.endTransmission();
 //delay(10);
 Wire.requestFrom(DEFAULT_SLAVE_ADDRESS, 1);
 Serial.println(Wire.read());
 delay(1000);
} 

slave.ino: atmega328p

#include <Wire.h>
#define F_CPU 8000000UL
volatile uint8_t nr;
void setup()
{
 Wire.begin(0x0A); // join i2c bus with address #4
 //Wire.onReceive(receiveEvent); // register event
 Wire.onRequest(requestEvent);
}
void loop()
{
}
//void receiveEvent(int howMany)
//{
// while(0 < Wire.available()) // loop through all but the last
// {
// int x = Wire.read(); // receive byte as an integer
// }
//}
void requestEvent()
{
 nr = 99; 
 Wire.write(nr); 
}

Serial output from master: (got no val)

-1
-1
...

Logic analizer output: nack

Wiring on atmega: VALUE-PIN

  • VCC 7
  • GND 8
  • SCL 28
  • SDA 27

enter image description here

anyone got an idea whats missing?

Edit: removed receiveEvent(), added CPU speed and it worked

asked Nov 16, 2021 at 18:31
5
  • You seem to have a problem only with the read operation at the moment. I would remove the onReceive part to troubleshoot the problem and test if the example code works (arduino.cc/en/Tutorial/LibraryExamples/MasterReader). Commented Nov 17, 2021 at 0:39
  • esp8266 is usually 3.3V while atmega328p is 5V - do your hardware setup take it in account? And have the voltage on SDA,SCL the right values for HIGH and LOW on BOTH master and slave? Commented Nov 17, 2021 at 3:05
  • 1
    Can you make your 328P's loop() do something, like blink an led so that you know the 328P is operational and at the correct frequency? Commented Nov 17, 2021 at 14:59
  • @SimSon thanks for your reply. when i removed the receiveEvent it worked. i figured out the the problem is the Wire.available() function on the atmega. Commented Nov 17, 2021 at 20:20
  • @gilhad the ATmega can be operated at 3.3V, I guess that's the case here. The ATmega also NACKs the read header, which means the address has been transmitted successfully. That doesn't make it look like a hardware issue. Commented Nov 17, 2021 at 20:30

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.