I set this up only as a demo for my self to see it works and what i need to do. A POC in other words.
I have two Mega2560s. Both are powered by the same, large 6VDC battery. The SLC / SDA wires are in correctly between the two boards. The "Master" board has a USB cable to the desk top to monitor the Serial.print() calls.
The set up should approximate a data PUSH set up (as opposed to a data PULL system that would involve interrupt handlers, etc).
When I run the program I see constant gibberish from Serial.println(). I have fiddled with baud speed and other parameters but see see garbage.
The opening "This is master" etc appears correctly in the Arduino Serial Monitor so i know my baud / etc is correct. But the reading of the data from slave to master is trash.
I have the feeling I am missing just a single "small" thing that I am overlooking.. Its frustrating I can't spot it.
Below is my code.
Any help is appreciated. Thank you!
#include <Wire.h>
#define nFor_I2C_PULL_Demo_Master_or_Slave 2 // 1 is master, 2 is slave.
void setup()
{
pinMode( 13 , OUTPUT ) ;
digitalWrite( 13, LOW ) ;
Serial.begin( 57600 );
#if nFor_I2C_PULL_Demo_Master_or_Slave == 1
Serial.println( F( " ======= THIS IS Master ============" ) ) ;
Wire.begin( 1 );
#endif
#if nFor_I2C_PULL_Demo_Master_or_Slave == 2
Serial.println( F( " ======= THIS IS Slave ============" ) ) ;
Wire.begin( 2 );
#endif
#if nFor_I2C_PULL_Demo_Master_or_Slave == 2
// just transmitt all the time
while (1 )
{
digitalWrite( 13 , HIGH ) ;
Wire.beginTransmission( 1 ); // transmit to device #1
Wire.write( "ABCDEFGHI" ); // sends
Wire.endTransmission(); // stop transmitting
delay(500);
digitalWrite( 13 , LOW ) ;
delay(500) ;
}
#endif
#if nFor_I2C_PULL_Demo_Master_or_Slave == 1
while (1)
{
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
while (Wire.available())
{
// slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.println( c ) ;
delay(20) ;
}
}
#endif
}
//*********************************************** M A I N L O O P ***************************************************************
void loop()
{
}
You can't have the slave continuously sending data over the I2C, it need to have an interrupt handler to respond to the requestFrom(), and needs to send exactly six bytes of data since that is what the master is requesting.
Yes Vin No: 7V is not the minium. 5V is. I have ran 20 Megas with various sensors attached to each off the identical model battery with out any distortion, corruption or issues. The volatge is fine for just two Megas attached with no sensors.
The Mega 2560 can be powered via the USB connection or with an external power supply. The power source is selected automatically.
External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the GND and Vin pin headers of the POWER connector.
The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may become unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.
The slave should have a function registered with onRequest() to send the data in response to requestFrom(). This function is called by the interrupt handler in the Wire library.
When converting the above statement into codes we get --
void setup()
{
Wire.onRequest(sendEvent); // sendEvent is the name of the ISR()
}
void loop()
{
}
void sendEvent()
{
Wire.write(dataByte); //byte to go into Master's I2C Buffer over the I2C Bus
}
Is there any way for the Slave to know how many data bytes the Master has requested via the requestFrom() function? (If I call requestFrom() a function, then what would be the name of Wire.requestFrom() -- method?)
No, and there is no way for the master to know how many bytes the slave actually sent. requestFrom() always reads the number of bytes specified, if the slave sends less then the data line is released by the slave, is pulled high, and the master reads in 1 bits as if valid data were being sent.