Objective:
In this week's assignment we had to make our boards communicate between others through wires or wireless.



So I decided to use I2C communication to connect my Final Project ATmega328 board, with my ATtiny 45 board. and make it blink
I am going to be using my Final Project board which is the one that i made in the Output devices week
, with a simple Attiny45 board to make it blink through the At328p board. I Already have free SDA and SCL pins on my At328p board, but the attiny does not have dedicated SDA and SCL pins, from reading the Pinout of attiny45 i found there are pins that could be used to communicate between At328p and Attiny45 through the SCK and MOSI of the Attiny45.







I programed each board separately, then powerded atmega328p board through FTDI wire, and connected the VCC, GND, SDA, SCL pins on the Attiny 44 board to their corresponding pins on the Atmega328p board. the Attiny is the slave and the Atmega328p is the Master that sends the signals and powers the ATtiny45.

The Code was referenced by Abdallah Alsafadi's Work

Master

this code initiates the connection between the Master board and 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(750);
  Wire.write("y");// sends y to turn off LED  
   delay(500);
  Wire.endTransmission(); // stop transmitting
}

slave

this code defines the pins for LED output then begins connection upon recieving the signal from the Master. 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

    }
}
}

Video




Files

Attiny45 Board
Attiny45 Schematic



Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.