In this week I'll try two methods of communication between two nodes, which are the serial bus and the I2C communication protocol. For the group assignment, we will connect two projects together to send messages between them.

Softwares used:

Resources:

This week assignment content:


15.1. Group assignment.

15.1.1. Send a message between two projects.

For this week the group assignment is to send a message between two projects. Click here to enter the group assignment page.


15.2. Individual assignment.

Design, build, and connect wired or wireless node(s) with network or bus addresses.

15.2.1. Serial Bus (Arduino UNO + ATtiny44)

In this example I will connect Arduino UNO to my ATtiny44 board that I have created previously in week 7 and communicate with them trough serial bus. Steps are explained in details below:

15.2.1.1. Step 1: Set Arduino UNO as a programmer:

  • Connect Arduino UNO to laptop USB port through USB cable. serial_bus_1
  • Click on File > Examples > 11.ArduinoISP > ArduinoISP. serial_bus_2
  • Set your Board option to Arduino UNO and choose the correct Port. serial_bus_3
  • Upload the current code. When the code is uploaded, you will get the following message: serial_bus_4

15.2.1.2. Step 2: Program the ATtiny44 board:

  • Connect ATtiny44 board to Arduino UNO through ISP connector.

    AVR ISP 6-pins Header layout: AVR-ICSP
    • Arduino Pin 13 ---> SCK
    • Arduino Pin 12 ---> MISO
    • Arduino Pin 11 ---> MOSI
    • Arduino Pin 10 ---> RESET
    • Arduino 5V ---> VCC
    • Arduino Ground ---> GND
    serial_bus_5
  • Set your Board option to ATtiny44 and choose the correct Port. If you don't have the ATtiny44 board shown in the boards list, then you should upload its library to the boards manager (follow steps explained in week 9). serial_bus_6

  • Upload the following code. The output is sent to "PA3" becuase in my ATtiny44 board, LED is connected to PA3 (physical pin: 10).
    #include <SoftwareSerial.h>
    SoftwareSerial mySerial(PA1,PA0); //RX, TX
    int v=0; 
    int nodeid=3;
    
    void setup() {
      mySerial.begin(9600); 
      pinMode(PA3, OUTPUT); // white led
    }
    
    void loop() {
      while (mySerial.available () == 0 ) {}
      v = mySerial.parseInt();
      mySerial.println(v);
      if(v == nodeid)
    {
      digitalWrite(PA3,HIGH);
      delay(500);
      digitalWrite(PA3,LOW);
      delay(500);
      digitalWrite(PA3,HIGH);
      delay(500);
      digitalWrite(PA3,LOW);
    }
    else
    {
      digitalWrite(PA3,LOW);
     }
    }
  • When the code is uploaded, you will get the following message: serial_bus_7

15.2.1.3. Step 3: Program the Ardunio UNO board:

  • Set your Board option to Arduino UNO and choose the correct Port. serial_bus_3
  • Upload the following code. The output is sent to pin "13", which is the digital pin that the on-board LED hard wired to. It’s the one with an “L” next to it.

    int v=0; 
    int nodeid=1;
    
    void setup() {
      Serial.begin(9600); 
      pinMode(13, OUTPUT); // white led
    }
    
    void loop() {
      while (Serial.available () == 0 ) {}
      v = Serial.parseInt();
      Serial.println(v);
      if(v == nodeid)
    {
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);
      delay(500);
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);
    }
    else
    {
      digitalWrite(13,LOW);
     }
    }
  • When the code is uploaded, you will get the following message: serial_bus_8

15.2.1.4. Step 4: Serial monitor:

  • Connect ATtiny44 board bus pins to Arduino UNO Rx, Tx, 5V, GND pins and disconnect the ISP header. serial_bus_9
  • Open the Serial Monitor from the Tools menu or by pressing Ctrl + Shift + M on your keyboard. When the serial monitor is opened, change the baud rate to "9600". serial_bus_10
  • Type "1" to flash the LED on the Arduino UNO and "3" to flash the LED on the ATtiny44 board.

15.2.1.5. Problem faced:

Both LEDs on Arduino UNO and ATtiny44 didn’t work because I have connected ATtiny44 Tx pin to Ardunio Rx pin and vice versa. I didn’t get an error message, but there were no physical results as well as no output on the serial monitor.


15.2.2. I2C (Arduino UNO + ATtiny44)

My second try for this week assignment is using I2C communication protocol. It stands for Inter-Integrated Circuit. Also it's called 2-Wire Communication. In I2C, data are transmitted between devices using Serial Data Line (SDA) and Serial Clock Line (SCL). It can support the communication between multiple masters and multiple slaves devices at the same time.

To do the second test using I2C serial communication, I have used my ATtiny44 board that I have created before as a master board and Arduino UNO as the slave board for my program. Steps are explained below:

15.2.2.1. Step 1: Set Arduino UNO as a programmer:

First I set Arduino UNO as the programmer for my ATtiny44 board as explained previously in Serial Bus step 1.

15.2.2.2. Step 2: Add the ATtiny44Core board library:

To run the code, I need to install the ATtinyCore library. To do that, add the following URL to the board manager "Additional Boards Manager URLs" list.

http://drazzy.com/package_drazzy.com_index.json
  • Go to File >> Preferences >> enter the above URL in "Additional Boards Manager URLs. Preferences Preferences_URL
  • Then go to Tools >> Boards >> Boards Manager.
  • Select "ATTinyCore by Spence Konde" and click "Install". core
  • Now you should have the ATtiny44 under the ATtinyCore boards, select as your board. tinyCore

15.2.2.3. Step 3: Program the ATtiny44 board (Master):

Then connect the ATtiny44 board ISP connector to Arduino UNO as mentioned previously in Serial Bus step 2. Upload the following code, which tells the program to send "1" through I2C serial wire when the push button (connected to pin PA2) is pressed.

#include <Wire.h>
#define button PA2
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();
}
}

15.2.2.4. Step 4: Program the Ardunio UNO (Slave):

Upload the following code to the slave board, which tells the program to send a high signal to pin 13 (LED) of the slave board.

#include <Wire.h>
#define led 13
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);
}
}

15.2.2.5. Step 5: I2C connection:

Connect the two boards as the following configuration: i2c

Note:
Blue line is SCL
Green line is SDA

15.2.2.6. Final result:

15.2.2.7. Problem faced:

If the ATtiny44 core library is not installed correctly, the following error message will appear: error


15.2.3. I2C (ATtiny84 + ATtiny44)

My third try was using the same method of communication in the second try, which is I2C. But using ATtiny84 micro-controller board that I made for final project instead of Arduino UNO as the salve board.

15.2.3.1. Step 1: Set Arduino UNO as a programmer:

First I set Arduino UNO as programmer as explained previously in Serial Bus step 1.

15.2.3.2. Step 2: Program the ATtiny44 board (master):

Connect ATtiny44 board to Arduino UNO through ISP connector as mentioned in Serial Bus step 2. Set your Board option to ATtiny44, Clock to external 20 MHz, and choose the correct Port.

Connection: serial_bus_5

Upload the same code that was uploaded in I2C step 3. Which tells the program to send "1" through I2C serial wire when the push button (connected to pin PA2) is pressed.

Code:

#include <Wire.h>
#define button PA2
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();
}
}

15.2.3.3. Step 3: Program the ATtiny84 (Slave):

Connect ATtiny84 board to Arduino UNO through ISP connector as mentioned before. Upload the following code to the slave board, which tells the program to send a high signal to PA2 (LED) of the slave board.

Connection: serial_bus_111

Code:

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

15.2.3.4. Step 4: I2C connection:

Connect the two boards, which are the ATtiny84 and ATtin44 boards as explained previously in 15.2.2.5. Step 5: I2C connection. and observe that the LED that connected to ATtiny84 will be ON when the push button on the attiny44 board is pressed.

connection

15.2.3.5. Final result:

15.2.3.6. Problem faced:

When I was trying to upload ArduinoISP code to Arduino UNO, I got an error and the code won't uploaded.

error_uploading

To solve this problem, I have used another Arduino UNO to burn bootloader of my Arduino UNO. Connection between the two Arduino UNOs is shown below:

ArduinoUNOtoUNO_ISP2

connectionISP

After burning bootloader successfully, the following message will be shown and you can use the Arduino UNO again. burn bootloader


⤧  Next post 16. Molding and casting ⤧  Previous post 14. Invention, Intellectual Property and Business Models