I2C between Spark and Uno

Without adding all my code to the post, I can say I’m just passing a single number from the spark core to an uno. The numbers all pass over as “-1” though. For example, performing some simple math on the core, passing a single digit result to the Uno and printing it to the serial port. Printing the values from the serial port on the core displays the correct values. On the uno the output is “-1”.
???

  1. Are there pullup resistors?

  2. Which is the master and which is the slave?

  3. There's some posts here which might be useful :smile:

Core is the master; Uno is the slave.

I don’t have pull up resistors. Do I need them for I2C?

Yup :slight_smile:

Here’s the code I’m using. I’m pulling the time from a time server, processing the time into individual digits and then sending over I2C to my Uno. I’m using the Uno to print to serial or an lcd. I decided to have the Uno handle the output because I’m eventually going to display it on color display. Handling the code for the display is eaier on the Uno.

On the core:
UDP UDP;

char string[ 17 ] = { “” };

int hour, minute, second, hd1, hd2, md1, md2, sd1, sd2;

unsigned int localPort = 123;
unsigned int timeZone = -5;

unsigned int serverNum = 0;
unsigned long timeout;

const char timeServer[] = “pool.ntp.org”;

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

void setup()
{
UDP.begin(localPort);
Serial.begin(9600);
delay(1000);
Serial.println(“NTP-clock”);
Wire.begin(); // join i2c bus (address optional for master)
}

void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available

delay(1000);
if ( UDP.parsePacket() ) {
	UDP.read(packetBuffer, NTP_PACKET_SIZE);  // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = (packetBuffer[40] << 8) + packetBuffer[41];
    unsigned long lowWord = (packetBuffer[42] << 8) + packetBuffer[43];
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    secsSince1900 += timeZone*60*60;

    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears; 

    hour = (epoch % 86400L) / 3600;         
    minute = (epoch % 3600) / 60;
    second = (epoch % 60);
    hd2 = (hour % 10);
    hd1 = ((hour-hd2)/10);
    md2 = (minute % 10);
    md1 = ((minute-md2)/10);
    sd2 = (second % 10);
    sd1 = ((second-sd2)/10);

    Serial.print ("epoch   ");
    Serial.println (epoch);
    Serial.print (hour);
    Serial.print (":");
    Serial.print (minute);
    Serial.print (":");
  //  if (second <= 10) {Serial.print ("0");}
    Serial.println (second);
 //   
 // Serial.println (hourdig1);
 // Serial.println (hourdig2);
 // Serial.println (mindig1);
 // Serial.println (mindig2);
 // Serial.println (secdig1);
 // Serial.println (secdig2);
  //  Serial.print (":");
  //  Serial.print (minute);
 //   Serial.print (":");
 //   Serial.print (secdig1);
 //   Serial.println (secdig2);

Wire.beginTransmission(0x2c); // transmit to device #4
Wire.write(“time is”); delay (10); // sends five bytes
Wire.write(hd1); delay (10); // sends one byte
Wire.write(hd2); delay (10);
Wire.write(md1); delay (10); // sends one byte
Wire.write(md2); delay (10);// sends one byte
Wire.write(sd1);delay (10); // sends one byte
Wire.write(sd2); delay (10); // sends one byte
Wire.endTransmission(); // stop transmitting

    }

while ( UDP.parsePacket() ) {               // clean-up buffer
	UDP.read(packetBuffer, NTP_PACKET_SIZE);  // read the packet into the buffer
}

//delay(600000); // wait ten minutes before asking for the time again
delay(10000); // wait ten seconds before asking for the time again

}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(const char *address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
UDP.beginPacket(address, 123);
UDP.write(packetBuffer, NTP_PACKET_SIZE); //NTP requests are to port 123
UDP.endPacket();

}

on the Uno:
// Wire Slave Receiver
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the “Wire Master Writer” example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial lcd = SoftwareSerial(0,6);

void setup()
{
Wire.begin(0x2c); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
lcd.begin(9600);
}

void loop()
{
//lcd.print(“test”);
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
//lcd.write(0xFE);
//lcd.write(0x58);
delay(10);

while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print©; // print the character
}
int hourdig1 = Wire.read(); // receive byte as an integer
int hourdig2 = Wire.read(); // receive byte as an integer
int mindig1 = Wire.read(); // receive byte as an integer
int mindig2 = Wire.read(); // receive byte as an integer
int secdig1 = Wire.read(); // receive byte as an integer
int secdig2 = Wire.read(); // receive byte as an integer

  lcd.println (hourdig1);
  lcd.print (hourdig2);
  lcd.print (mindig1);
  lcd.print (mindig2);
  lcd.print (secdig1);
  Serial.println (secdig2);

}

I’m no expert in I2C and only worked with it once on a Temperature sensor.

So the other :spark: elites are gonna be giving you better advice.

What i would do for a start is to just write a simple test code to make sure I2C is working between the core and uno before proceeding. :smile: