Skip to content

Networking and communications

This week’s group assignment is to Send a message between two projects

I^2C

Our master board has attiny44 20 MHZ clock from Abdullah hello word borad and our slave board had also attiny44 20 MHZ clock from Fatima hello word borad

Connection

First we have to know the SCL and SDA pins from the datasheet

Then we connected the board as shown

Note that R1 = R2 = 4.7k ohm

Programming

NOTE

From the picture we know that by installing that attiny core we can make the wire.h library work.

Arduino ide settings

Master code

#include <SoftwareSerial.h>
#include <Wire.h>

#define button 2

void setup() {
Wire.begin();
pinMode(button, INPUT);
}
void loop() {
int data;
data = digitalRead(button);
if (data == 1)
{
Wire.beginTransmission(8);
Wire.write(1);
Wire.endTransmission();
}
else {
Wire.beginTransmission(8);
Wire.write(0);
Wire.endTransmission();
}
}

Slave code

#include <Wire.h>
#define led 2
void setup() {
Wire.begin(8);
Wire.onReceive(receiveEvent);
pinMode(led, OUTPUT);
}
void loop() {
delay(100);
}
void receiveEvent(int howMany) {
int x = Wire.read();
if (x == 1)
{
digitalWrite(led, HIGH);
}
if (x == 0)
{
digitalWrite(led, LOW);
}
}

Using RX TX

We used the same boards however we changed the connections and the code

connection

We just plugged the 2*6 wire which contain RX, TX. The transmition of the first should be connecter to the other reciver so we made that in the code

Programming

Master code

#include<SoftwareSerial.h>
SoftwareSerial mySerial(PA4, PA5);
#define button PA3
int state = 0;
int buttonState = 0;

void setup() {
  pinMode(button, INPUT);
  mySerial.begin(38400); 
}
void loop() {
 if(mySerial.available() > 0){ 
    state = mySerial.read(); 
 }

 // Reading the button
  buttonState = digitalRead(button);
 if (buttonState == LOW) {
   mySerial.write('1'); 

 }
 else {
   mySerial.write('0');

 }  

}

Slave code

#include<SoftwareSerial.h>
SoftwareSerial mySerial(PA5, PA4);
#define ledPin PA3
int state = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  mySerial.begin(38400);
}
void loop() {
 if(mySerial.available() > 0){ 
    state = mySerial.read(); 
 }
 // Controlling the LED
 if (state == '1') {
  digitalWrite(ledPin, HIGH);
  state = 0;
 }
 else if (state == '0') {
  digitalWrite(ledPin, LOW); 
  state = 0;
 } 
}