Skip to content

14. Networking and communications

In this week assignment we are supposed to make devices communicate between others through wires or wireless.

So I decided to use I2C communication to connect my Final Project ATmega board, with my ATtiny 44 board.

communication method

in my Atmega 328p I have already dedicated two pins for SCL and two for SDA which are are the necessary pins for I2C communication.

but for the Attiny there are no pins for SDA and SCL that shows in the Pinout.

But when I checked online I found out that the pins I used for my ISP have MOSI pin which is can be used as SDA and has SCK which can be used as SCL.

so I decided to program both boards separately then connect atmega boad to power through FTDI wire, then connect the VCC, GND, SDA, SCL pins on the Attiny 44 board to their corresponding pins on the Atmega board which means the Attiny is the slave and the Atmega is the Master that sends the signals and powers the ATtiny44.

coding.

inpired by Meha Code

Master

this code first initiates the transmission of signals to the slave board. then it send “x” to turn on the LED and then sends “y” to turn of the LED connected to slave board.

#include <Wire.h>

void setup()
{
  Wire.begin(); // join i2c bus
}

void loop()
{
  Wire.beginTransmission(1); // transmit to device #1
  Wire.write("x");// sends x to turn on LED
   delay(1500);
  Wire.write("y");// sends y to turn off LED  
   delay(1000);
  Wire.endTransmission(); // stop transmitting
}

slave

this code first sets up the pins for LED output then begins the establish the connection on slave 1. then initiates the value for the led to low when starting. then in the loop it says when receiving “x” turn on LED and when receiving “y” from Master turn off the LED.

#include <TinyWire.h>

int LED = 3;

void setup() {
  pinMode(LED, OUTPUT); //sets LED as output
  TinyWire.begin(1); //stablish an I2C connection on slave #1
  TinyWire.onReceive( onI2CReceive );//register event

    digitalWrite(LED,LOW);// Turn LED off
}

void loop() {
}
void onI2CReceive(){
  while(TinyWire.available()){ // while the connection is availabe
    if(TinyWire.read()=='x') // // check if the signal is x
    {
      digitalWrite(LED,HIGH ); // Turn LED on

    }
    else{
      digitalWrite(LED,LOW);// Turn LED off

    }
}
}