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
anyone got an idea whats missing?
Edit: removed receiveEvent()
, added CPU speed and it worked
onReceive
part to troubleshoot the problem and test if the example code works (arduino.cc/en/Tutorial/LibraryExamples/MasterReader).loop()
do something, like blink an led so that you know the 328P is operational and at the correct frequency?Wire.available()
function on the atmega.