Fab Academy 2017
Learn how to make ALMOST anything!

Communications and Networking


This week's task was to design and build a wired or wireless network connecting at least 2 processors

To be honest, I had no previous background around Networking. So I had to start from the very begining and understand what is networking and what's is serial and parallel commnications.

After a little research, I came to find 2 nice links that explain the basics:

Other than usual information research, it's really beneficial to look at previous students work, especially if you have no idea where to start (like me :D). Those were few of previous students' work that really helped very much:

 

Planning for work:

In this task, I wanted to imitate the circuit I'm going to use in the final project. The basic function is one main board that reads the input and then operates 3 other node output circuits of flashing LEDs. The input I tried is Push Button and that is used to flash a LED on the receiver board.

 

Implementation:

Since I had no previous experience, I followed Joseph's steps and went to try serial communication between 2 arduinos first.

The basic idea for this is to communicate 2 arduino board through communicating TX, RX and GND pins, send a character through the first board "Master", receive it through the second board "Slave" and flash the LED.

This is how you communicate the 2 arduino boards.

 

The next step I did was trying the code as it is.

Master Code

                    //Master

                   void setup() {
                   Serial.begin(9600);
                  }

                   void loop() {
                    Serial.println ('H');
                    delay (4000);
                    Serial.println ('L');
                    delay (4000);
                   }
   
                    
 

Slave Code

                    //slave

                    const int ledPin = 13;          // the pin that the LED is attached to
                    int incomingByte;              // a variable to read incoming serial data into

                    void setup() {
                     
                    Serial.begin(9600);           // initialize serial communication:
                    pinMode(ledPin, OUTPUT);     // initialize the LED pin as an output:
                    }

                    void loop() {
                    
                    if (Serial.available() > 0) {   // see if there's incoming serial data:
                     
                     incomingByte = Serial.read();  // read the oldest byte in the serial buffer:
                     
                     if (incomingByte == 'H') {     // if it's a capital H (ASCII 72), turn on the LED:
                     digitalWrite(ledPin, HIGH);
                     }
                     
                     if (incomingByte == 'L') {    // if it's an L (ASCII 76) turn off the LED:
                     digitalWrite(ledPin, LOW);
                     }
                     }
                     } 
                    

It worked perfectly!

 

Modifications:

The next step was to modify the code to do the function of flashing a LED (on the slave board) using a push button (on the Master board)

To develop that code, I needed to go back and read little bit about Serial commands Arduino provides.

The one I later used are:

1. Serial.Begin: This serves to initiate the serial communication.

2. Serial.write: This serves to write a byte to serial ports.

3. Serial.available: This serves to show the available data in the buffer.

4. Serial.read: This serves to read the data in the buffer.

5. The library SoftwareSerial, (later on with the fabricated boards).

 

As for the push button, the connection was pretty simple. One side to the GND, the other side to 5V and Pin 8 as shown in the picture.

 

And this is the code.

Master Code

                       //Master

                        int Button = 8;
 
                        void setup() {
                        Serial.begin(9600);
                        pinMode (Button, INPUT);
                        }

                       void loop() {
                       if (digitalRead (Button) == 1) {
                       Serial.write('B');                   
                       }
                       }
 
                    
 

Slave Code

                    //slave

                    const int ledPin = 13;                     // the pin that the LED is attached to
                    int incomingByte;                         // a variable to read incoming serial data into

                    void setup() {
                    
                    Serial.begin(9600);                       // initialize serial communication:
                    pinMode(ledPin, OUTPUT);                  // initialize the LED pin as an output:
                    
                    }


                    void loop() {
                    
                    if (Serial.available() > 0) {             // see if there's incoming serial data:
                     incomingByte = Serial.read();            // read the oldest byte in the serial buffer:
                     
                     if (incomingByte == 'B') {               // if it's a capital H (ASCII 72), turn on the LED:
                     digitalWrite(ledPin, HIGH);
                     }

                     delay (3000);
                     digitalWrite (ledPin, LOW);
                     }
                     }  
                    
 

Here the video showing its operation.

 
   

Final Project Work:

After implementing the 2 circuits of the input and the output, The major challenge was in the coding part.

When Adding the Software Serial library for the code, I discovered 2 major issues:

1. The master circuit code wasn't stable.

2. The slave circuit code size became much bigger than the size of the ATTiny44 memory.

I. Master Code stability:

The Code was fluctuating and also was sending continuous characters when a sensr is touched, and sometimes, it didn't read the change of sensors. Something similar to the shown pictures.

       
 

To solve this issue, after a lot of trials, we came up with the following:

1. Adding a "flag" and adding it as a condition in the If loop: That would allow the code to send the character only one time, so gives it more stability.

 

2. Re-arranging the code: That was pretty tricky as we use "If" and "Else If". When a sensor reads, the microcontroller doesn't proceed to implement the rest of the code and doesn't go into the rest of the loops. So What I basically did was to re-arrange the code to start with the 3 Sensors combinations check, the 2 sensors together and then single sensors checks. So basically, rearranged it backward.

And that was the same for Both Circuits (Input and Output).

 
 

II. The code size:

When I developped the code using Arduino C, The code of slave board was very big! While compiling, it showed that the code was taking 126% of the memory!

I had 2 options to solve that:

1. To modify the software serial library.

2. To write the code in C language.

I went for the Second Option, as it wasn't just Software serial library only the issue, but also something like "digitalWrite" was taking a big space in the code.

And with the help of one of my colleagues, we developed the code into C language. And to do that, going back to Embedded Programming week and the ATTiny datasheet was very useful, as I have to look up the Ports and whether they are 8-bits or 4-bits ports.

And That was it!

And Here is how the circuits connected together

   

Downloads:

1. Introductory Code.

2. Board Code.