FabLab UAE My Fab Academy Journey

14. Networking and Communications


# Goal:


To design, build, and connect wired or wireless node(s) with network or bus addresses



# Tasks:


1- Upload the master code of ATmega328 board in Arduino IDE.
2- Upload the slave code of ATtiny44 board in Arduino IDE.
3- Connect the boards using wires and run the master code.



# Procedures:


There are different communication protocols, but here I focused on Inter intergrated circuit (I2C) communication. This protocol can handle multiple masters and multiple slaves. I2C require two wire to create a connection because it has two signal: Clock signal (SCL) and Data signal (SDA). In this week, I decided to use one master and one slave. I used the ATmega328 board that was designed in the Output Devices week as the master board and the ATtiny44 board that was designed in the Electronics Design week as the slave board.


a. ATmega328 Board- Master:


To use the I2C communication process, I used " Wire.h" in Arduino IDE. I started by using "Wire.begin()" command in the setup part to start the Wire library and join the I2C bus. I left this command empty to indicate that this is the master code. In the void loop section, I used the "Wire.beginTransmission(1)" function to begin a transmission to the first I2C slave device. This library allows each master to have up to 1008 slaves, so writing the number of the slave is important. Then, I used "Wire.write()" command to send two letters: N and F. To ends a transmission to a the slave device, I used "Wire.endTransmission()" command.


                            
// Master board: ATmega328 board      
#include Wire.h // Wire.h library
void setup() {
Wire.begin(); // To initialize the Wire library and join the I2C bus.
}

void loop() {
Wire.beginTransmission(1); // To begin a transmission to the first I2C device.
Wire.write("N"); // To send the state of the button.
delay(500);
Wire.write("F");
delay(500);
Wire.endTransmission(); // To ends a transmission to a the device
}

b. ATtiny44 Board- Slave:


To use the I2C communication process, I downloaded " TinyWire.h" and added it to the libraries in Arduino IDE since the previous library cannot be used for ATtiny microcontrollers. I created the code so that the LED in ATtiny44 board is on when the master board transmit letter N. So, I started by stating the pin number in which the LED is connected to the microcontroller. In the setup section, I declare the LED as an output using "pinMode" command. Then, I used "TinyWire.begin()" command to join the I2C bus in the first slave device. Then, I created a function to be called when the slave device receives a transmission using "TinyWire.onReceive()" command. In this function, I ensure there is a connection between the master and the slave by using "TinyWire.available()" command. After that, I allowed the code to read the transmitted byte using "TinyWire.read()". If the byte was the letter N, then the LED in the ATtiny44 board turn on.


// The Slave board is ATtiny44 board 
#include TinyWire.h // TinyWire.h library.
#include twi.h
int LED = 8 ; // The microcontroller Pin connect to the LED.
void setup() {
pinMode(LED, OUTPUT); // LED is an output.
TinyWire.begin(1); // Join I2C with device
TinyWire.onReceive(ledevent); // To register a function to be called when a device receives a transmission.
}
void loop() {
}
void ledevent(){
while(TinyWire.available()){ // To ensure that the connection is availabe
if (TinyWire.read() == 'N'){
digitalWrite(LED,HIGH); // Turn on the LED
}
else {
digitalWrite(LED,LOW); // Turn off the LED
}
}
}

c. Connection between the boards:


I uploaded the slave code on the ATtiny44 board using FabTinyISP. Then, I uploaded the master code on the ATmega328 board using FTDI cable. To connect the two boards, I used female-to-female wires to connect GND, VCC, SDA, and SCL in both boards. In ATmega328 board, there are two dedicate pins labelled as SCL and SDA. However, there are no dedicated SCL and SDA pins as shown below in the ATtiny44 pinout. So, I connected the SDA wire to PA6 and the SCL wire to PA4 and ran the master code.




Unfortunetly, the LED light is not that clear as you can see below. However, It turn on and off.




So, I used a regular blink code in Arduino IDE to ensure that ATtiny44 echo board using FabTinyISP without connecting it to the master board. After ensuring that the LED in the secondery board is working properly, I checked ATmega328 board and upload the previous codes to try to get the LED light to be clear. During this process, I burned my processor in the master board and since Fablab UAE is re-locating to another building I couldn't find another processor. Thus, I used Arduino Uno board as the master board instead. After many trials, the problem was solved by inserting "Wire.endTransmission" statement after "Wire.beginTransmission" and "Wire.write" statments. Also, I used numbers instead of sending letters to turn on LED


                            
// Master board: Arduino Uno board      
#include Wire.h // Wire.h library
void setup()
{
Wire.begin(); // join i2c bus
}
void loop()
{
Wire.beginTransmission(1); // transmit to device #1
Wire.write('1'); // send to turn on LED
Wire.endTransmission(); // stop transmitting
delay(500);
Wire.beginTransmission(1); // transmit to device #1
Wire.write('2'); // send to turn off LED
Wire.endTransmission(); // stop transmitting
delay(500);
}


For the secondery board, I used the following code:


// The Slave board is ATtiny44 board 
#include TinyWire.h // include TinyWire.h library
int LED = 8 ; // The microcontroller Pin connect to the LED
void setup() {
pinMode(LED, OUTPUT); // LED is an output.
TinyWire.begin(1); // Join I2C with device
TinyWire.onReceive(ledevent); // To register a function to be called when a device receives a transmission.
}
void loop() {
}
void ledevent(){
while(TinyWire.available()){ // To ensure that the connection is availabe
if(TinyWire.read() == '1'){
digitalWrite(LED,HIGH); // Turn on the LED
}
else {
digitalWrite(LED,LOW); // Turn off the LED
}
}
}








Group Assignment

The link of the group assignment page is here.

# Goal:


Send a message between two projects.



# Task:


Create a master and slave codes for ATmega328 board and ATtiny44 board, repectively. While changing the slave board to be the second.



# Procedures:


I created another master and slave codes using "Wire.h" and "TinyWire.h" in Arduino IDE. After uploading the following codes to the boards, I connected their GND, VCC, SDA, and SCL using wires.


a. ATmega328 Board- Master:


                            
#include Wire.h                   // Wire.h library. 
void setup() {
Wire.begin(); // To initialize the Wire library and join the I2C bus.
}
void loop() {
Wire.beginTransmission(1); // To begin a transmission to the first I2C device.
Wire.write("Y"); // To send the message.
delay(100);
Wire.write("Z"); // To send the message.
delay(100);
Wire.endTransmission(1); // To ends a transmission to a the device.
delay(1000);
Wire.beginTransmission(2); // To begin a transmission to the second I2C device.
Wire.write("X"); // To send the message.
delay(100);
Wire.write("M"); // To send the message.
delay(100);
Wire.endTransmission(2); // To ends a transmission to a the device.
}

b. ATtiny44 Board- Slave:


                            
#include TinyWire.h                   // TinyWire.h library for ATtiny microcontrollers 
#include twi.h
int LED = 8 ; // The microcontroller Pin connect to the LED.
void setup() {
TinyWire.begin(2); // Join I2C with device address 2
TinyWire.onReceive(ledevent); // To register a function to be called when a device receives a transmission
pinMode(LED, OUTPUT); // LED is an output.
}
void loop() {
delay (100);
}
void ledevent(){
while(TinyWire.available()){ // To ensure that the connection is availabe
if (TinyWire.read() == 'M'){
digitalWrite(LED,HIGH); // Turn on the LED
delay (1000);
digitalWrite(LED,LOW); // Turn off the LED
}
}
}



# Challenges:
I did face one major problem, which is the slight turn on of the LED in the ATtiny board.