i2c between Atlas and Ballastboard

i2c Basics

  • In an attempt to keep the processing of the GPS and radio and the ballast drops seperate I've decided to create a second board running an AVR Atmega168 to control the new sensors and also control the actually ballast dropping mechanism. To communicate between the 2 AVRs I'm using i2c.
  • i2c relies on 2 wires; on the Arduino these are:
    • SDA (data line) is on analog input pin 4
    • SCL (clock line) is on analog input pin 5
    • GND is also required
  • An i2c library is included with the arduino environment as is known as the Wire library - it makes communicating between the 2 boards very easy. Information on the various functions can be found on the arduino website: Wire Lib Reference
  • i2c relies on a master/slave setup and to define master it is usually necessary to use some pullup resistors, as i2c is a function built into the AVR the library sets the internal pullups so all you need to do is connect the 2 AVRs directly.
  • Once the master and slave are set up using Wire.begin() and Wire.begin(2) you can begin to pass data between the 2 micros. The present setup the master (Atlas) sends a request to the slave (Ballastboard) for data, the slave then replies with the latest reading from the temperature sensor connected via one-wire. I originally tried to have the slave collect the temperature data when the master requested it however found that i2c timed out as it took too long so instead the slave collects the data every 5 seconds and updates a buffer which is then requested by the master.

Basic Example

  • borrowing from the Arduino forum here are the 2 examples that I originally used to test the connection:
  • i2c.master
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// master side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008

#include <Wire.h>

void setup()
{
  Wire.begin();	  // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  Serial.print("master sleeping...");
  delay(2000);
  Serial.println("go");
}

void loop()
{
  Wire.requestFrom(2, 1);    // request data from slave device #2

  while(Wire.available())
  {
    char c = Wire.receive(); // receive a byte as character
    Serial.print(c);	   // print the character
  }

  delay(100);
}
 
  • i2c.slave
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// slave side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008

#include <Wire.h>

void setup()
{
  Wire.begin(2);		    // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
  Serial.begin(9600);	     // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void requestEvent()
{
  static char c = '0';

  Wire.send(c++);
  if (c > 'z')
    c = '0';
}
 
missions/ballasthalo/flight3/i2c.txt · Last modified: 2009/10/12 07:38 by jamescoxon
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki