13networking

Exercise 13. Networking & Communications

Assignment: Networking & Communications

Learning Outcomes

Evidence

Process

The following resources helped in understanding an I2C serial bus and writing code:

Testing with Arduinos

By this point my computer was no longer programming any of my boards so I used two Arduinos to start while I sought help in getting my computer in working order (and eventually used a different computer) to program my boards.
problem uploading
The following diagrams from the I2C Instructables helped in wiring.
fritzing diagram
schematic

I downloaded the wire libary necessary for I2C communication and sent the master/slave codes that I'd found to my two Arduinos.
original code

Next I modified the codes and sent this.

I2C Boss

#include <Wire.h>
 
int LED=13;
int x = 255;   // maximum 255
 
void setup() {
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Master
  Wire.begin();
}
 
void loop() {
  digitalWrite(LED, HIGH);
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x
  Wire.endTransmission();    // stop transmitting
  delay(10);
  digitalWrite(LED, LOW);
  delay(20);
}

I2C Worker

#include <Wire.h>
 
int LED = 6;
int x = 0;
 
void setup() {
  Serial.begin(9600);
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9);
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}
 
void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}
 
void loop() {
  //Serial.print(x);
  //Serial.print("\n");
  digitalWrite(LED, HIGH);
  delay(x);
  digitalWrite(LED, LOW);
  delay(x);
}


Once I was able to program my boards with another computer I tested with my modified Satshakit, first as the board receiving messages and
blinking the LED, and then as the board sending the message.
setup satshakit

Preparing a second Arduino-like board

Since for my final project I need a 2nd Arduino-like board, I decided to make Luciano's Barduino. It has a much smaller footprint than my modified Satshakit and I was able to mill it successfully on the first go.
milled traces
milled board
bom
barduino hero

I burned the bootloader and successfully ran Blink on my board.

Programming

Although it is possible to create a serial bus between two Arduinos without pins A4 and A5, these pins are necessary to create an I2C serial bus. I discovered as I was looking at the schematic and board diagram for the Barduino that it doesn't have traces to these pins.
a4 and a5

First I set up my satshakit and Pilar's to communicate with each other.
i2c bus
master
slave
Then I decided that rather than redesign, mill, and stuff another board, I would connect wires to the A4 and A5 pins on my Barduino's microcontroller. This was very tedious work but worth the effort.
soldering wires

The original code that I found tells the receiving board if value received is 0 (x == '0') to blink one set of code with a shorter delay and if the value received is 3 (x == '3') to blink another set of code with longer delay. I changed my code so that there is a 1 to 1 relationship with the value of x. The highest number I can send with this code is 255 (1 byte of data) but I can change 'int x' value to manipulate blinking delay.

I2C Boss

#include <Wire.h>
 
int LED=13;
int x = 255;   // maximum 255
 
void setup() {
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Master
  Wire.begin();
}
 
void loop() {
  digitalWrite(LED, HIGH);
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x
  Wire.endTransmission();    // stop transmitting
  delay(10);
  digitalWrite(LED, LOW);
  delay(20);
}

The receiving board has the address of "9" in both the Boss and Worker codes. If I were to connect these two boards with others, the Boss code would send the message out to all the boards but only my Barduino would act upon.

Files

Barduino
I2C Code