shab1r
shab1r

Reputation: 43

How to use I2C protocol with arduinos

I am trying to built an elevator/lift using 3 or more Arduino Uno's and 1 Mega (as master).

Arduino Uno (Slave):

I have connected a IR sensor, a button, a LED and a 7-segment display to the Uno.

If I press the button the LED will light up and stays like that until the IR sensor detects the elevator cage. Then the LED will turn off and the 7 -segment display will show the floor number.

Arduino Mega (Master):

The master is used for the stepper motor and a keypad.

What the master has to do is ask the slaves (in this case the Uno's) about whether the button is pressed or not.

Example scenario:

Cage is at floor 2 and is detected by IR senor. If I press the button at floor 2 the Mega must know that the cage is already there. And if I press the button at floor 3 the Mega must know that the cage is at floor 2 and that the button at floor 3 is pressed and it must control the motor to bring the cage to floor 3 and show on the 7-segment display.

I must use i2c.

Here is the slaves code:

#include<arduino.h>

const int dataPin = 11;  //  wire to 74HC595 pin 11
const int latchPin = 8; //  to 74HC595 pin 8
const int clockPin = 12; //  to 74HC595 pin 12

int nummers[6] = {126, 12, 182, 158, 204, 204}; //0, 1, 2, 3, 4, 5

int buttonvalue = 0;
int button = 2;
int buttonLed = 3;

// ir  sensor and irleds
int irLedGreen = 5;
int irLedRed = 6;
#define IR 4
int detect = 0;

void setup() {
    Serial.begin(9600);   
    
    //ir sensor
    pinMode(irLedGreen, OUTPUT);
    pinMode(IR, INPUT);
    pinMode(irLedRed, OUTPUT);

    //shift out
    pinMode(dataPin, OUTPUT);
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);

    //button
    pinMode(button, INPUT);
    pinMode(buttonLed, OUTPUT);
}

void loop(){
    
    buttonvalue = digitalRead(button);
    detect = digitalRead(IR);

    // ir sensor led. It will be green if it detects something else it will be red.
    if (detect == LOW) { // if if detects something do the following.
        digitalWrite(irLedGreen, HIGH);
        digitalWrite(irLedRed, LOW);
    } else {
        digitalWrite(irLedGreen, LOW);
        digitalWrite(irLedRed, HIGH);
    }
    
    // button is pressed
    if (buttonvalue != 0 ) { 
        digitalWrite(buttonLed, HIGH);
        Serial.println("button");
    } else if (detect == LOW) {
        digitalWrite(buttonLed, LOW);
        
        digitalWrite(latchPin, LOW); // prepare shift register for data
        shiftOut(dataPin, clockPin, MSBFIRST, nummers[4]); // send data
        digitalWrite(latchPin, HIGH); // update display
        
        Serial.println("obstakel");
    }
    
    digitalWrite(latchPin, LOW); // prepare shift register for data
    shiftOut(dataPin, clockPin, MSBFIRST, nummers[0]); // send data 
    digitalWrite(latchPin, HIGH); // update display
}

edit

This is a small project not a real elevator. I need your help as to how I can program the master.

Upvotes: 0

Views: 529

Answers (1)

Piglet
Piglet

Reputation: 28940

Open any websearch. Enter "arduino i2c"

Click the first Link

https://www.arduino.cc/en/Reference/Wire

Read the text. Under "Examples" find

Master Reader/Slave Writer: Program two Arduino boards to communicate with one another in a Master Reader/Slave Sender configuration via the I2C.

Open the link

https://www.arduino.cc/en/Tutorial/MasterReader

Read the text.

Upload the example code to your arduinos

Master

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

Understand the code, play with it. Then apply the acquired knowledge to your project.

Upvotes: 0

Related Questions