×
Computer Controlled Cutting Electronics Production 3D Scanning and Printing Electronics Design Computer-Controlled Machining Embedded Programming Molding and Casting Input Devices Output Devices Networking and Communication Interface and Application Programming Mechanical & Machine Design

Group Assignment: Networking and Communication

For this week's group assignment we had to send a message between two projects.

I²C

I²C is a serial protocol for two-wire interface to connect low-speed devices like microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces and other similar peripherals in embedded systems. The I²C bus is a very popular and powerful bus used for communication between a master (or multiple masters) and a single or multiple slave devices. Each I²Cslave device needs an address. I²C uses only two wires: SCL (serial clock) and SDA (serial data).

Programming Master/Slave

We have developed a small interpreter that switches the LEDs on and off. For example, if you enter 00, the LEDs on both boards would turn off and if you enter 11, the LEDs on both microcontroller boards would light up. We used I²C to transfer the values. We connected the board from Hakan Zayin and Alessandra Crotty to demonstrate it.

Master Code

#include <Wire.h>
  
            #define LEDPin 5 // define the LED pin 
            String bsend;

            void setup() {
              Wire.begin();        // join i2c bus (address optional for master)
              Serial.begin(9600); //here we decalre Serial at 9600 baudrate for the input
              pinMode(LEDPin,OUTPUT);// set LED pin as an output
            }
            
            void loop() {
              if (Serial.available() > 0) // If we have some input
              {
                bsend = Serial.readString(); // we read it as String
                /*
                  So one thing I have to mention here, we have write a BIN input(4bit)
                */
                
                Serial.println(bsend); // debugging
                send(bsend); // call the send(input) function
              }
            
            }
       void send(String input){

           if(input.charAt(0) == '1'){ // okay here we check the character on 2 places (master and slave)
                   Wire.beginTransmission(11); // transmit to device (slave)
                  Wire.write(1);              // sends 1
                  Wire.endTransmission();    // stop transmitting
            
           }else if(input.charAt(0) == '0' ){ //if it's 0, transmit 0
                   Wire.beginTransmission(11); // transmit to device (slave)
                  Wire.write(0);              // sends 0
                  Wire.endTransmission();    // stop transmitting
            
            }
            //---------------------------------------------------------------
              
              if(input.charAt(1) == '1'){ //  Master board turn on or off LED
                  digitalWrite(LEDPin,HIGH);
              }else if(input.charAt(1) == '0' ){
                  digitalWrite(LEDPin,LOW);
              }
              
       }
          

Slave Code

#include <Wire.h>
            #define LEDPin 5 // again define the LED on pin 5
            void setup() {
              // put your setup code here, to run once:
              Wire.begin(11);                /* join i2c bus with address 13, if you see to the code on the top, we have defined 3 divices
               with 11 as S1, 12 as S2 and 13 as S3. So you have to change this value according to the device */
              Wire.onReceive(receiveEvent); // register event
              pinMode(LEDPin,OUTPUT); // set LED pin as an output
              Serial.begin(9600); //for debugging
              Serial.println("init");//for debugging
            }
            
            void loop() {
              delay(100);// we have to assign the delay, so that we can hear work with the input
            
            }
            void receiveEvent(int howMany) {
            
              
              if (Wire.available()) { // the same thing as in serial
                byte c = Wire.read(); // receive byte as a character
                Serial.println(c); // debug
                 if (c == 1){ // if the character equals to 1
                  digitalWrite(LEDPin,HIGH); // turn on the led
                 }else if(c == 0){ // els if the character equals to 0 
                  digitalWrite(LEDPin,LOW); // turn the led off
                 }
              }
            }
          

Download Files

Code Files

Master & Slave Code