Arduino Tutorial => Multiple Slaves

Download arduino (PDF)

arduino

  • Getting started with arduino
  • Awesome Book
  • Awesome Community
  • Awesome Course
  • Awesome Tutorial
  • Awesome YouTube
  • Analog Inputs
  • Arduino IDE
  • Audio Output
  • Bluetooth Communication
  • Data Storage
  • Digital Inputs
  • Digital Output
  • Functions
  • Hardware pins
  • How Python integrates with Arduino Uno
  • How to store variables in EEPROM and use them for permanent storage
  • I2C Communication
    • Multiple slaves
  • Interrupts
  • Libraries
  • Liquid Crystal Library
  • Loops
  • MIDI Communication
  • PWM - Pulse Width Modulation
  • Random Numbers
  • Serial Communication
  • Servo
  • SPI Communication
  • Time Management
  • Using Arduino with Atmel Studio 7
  • Variables and Data Types

arduino

  • Getting started with arduino
  • Awesome Book
  • Awesome Community
  • Awesome Course
  • Awesome Tutorial
  • Awesome YouTube
  • Analog Inputs
  • Arduino IDE
  • Audio Output
  • Bluetooth Communication
  • Data Storage
  • Digital Inputs
  • Digital Output
  • Functions
  • Hardware pins
  • How Python integrates with Arduino Uno
  • How to store variables in EEPROM and use them for permanent storage
  • I2C Communication
    • Multiple slaves
  • Interrupts
  • Libraries
  • Liquid Crystal Library
  • Loops
  • MIDI Communication
  • PWM - Pulse Width Modulation
  • Random Numbers
  • Serial Communication
  • Servo
  • SPI Communication
  • Time Management
  • Using Arduino with Atmel Studio 7
  • Variables and Data Types
arduino I2C Communication Multiple slaves

Fastest Entity Framework Extensions

Bulk Insert Bulk Delete Bulk Update Bulk Merge

Example

The following example shows how the master can receive data from multiple slaves. In this example the slave sends two short numbers. The first one is for temperature, and the second one is for moisture. Please notice that the temperature is a float (24.3). In order to use only two bytes and not four (float is four bytes), I multiple the temperature in 10, and save it as a short. So here is the master code:

#include <Wire.h> #define BUFFER_SIZE 4 #define MAX_NUMBER_OF_SLAVES 24 #define FIRST_SLAVE_ADDRESS 1 #define READ_CYCLE_DELAY 1000 byte buffer[BUFFER_SIZE]; void setup() { Serial.begin(9600); Serial.println("MASTER READER"); Serial.println("*************"); Wire.begin(); // Activate I2C link } void loop() { for (int slaveAddress = FIRST_SLAVE_ADDRESS; slaveAddress <= MAX_NUMBER_OF_SLAVES; slaveAddress++) { Wire.requestFrom(slaveAddress, BUFFER_SIZE); // request data from the slave if(Wire.available() == BUFFER_SIZE) { // if the available data size is same as I'm expecting // Reads the buffer the slave sent for (int i = 0; i < BUFFER_SIZE; i++) { buffer[i] = Wire.read(); // gets the data } // Parse the buffer // In order to convert the incoming bytes info short, I use union union short_tag { byte b[2]; short val; } short_cast; // Parse the temperature short_cast.b[0] = buffer[0]; short_cast.b[1] = buffer[1]; float temperature = ((float)(short_cast.val)) / 10; // Parse the moisture short_cast.b[0] = buffer[2]; short_cast.b[1] = buffer[3]; short moisture = short_cast.val; // Prints the income data Serial.print("Slave address "); Serial.print(slaveAddress); Serial.print(": Temprature = "); Serial.print(temprature); Serial.print("; Moisture = "); Serial.println(moisture); } } Serial.println("*************************"); delay(READ_CYCLE_DELAY); } }

And now the slave code:

#include <Wire.h> #include <OneWire.h> #include <DallasTemperature.h> //===================== // This is the hard-coded address. Change it from one device to another #define SLAVE_ADDRESS 1 //===================== // I2C Variables #define BUFFER_SIZE 2 #define READ_CYCLE_DELAY 1000 short data[BUFFER_SIZE]; // Temprature Variables OneWire oneWire(8); DallasTemperature temperatureSensors(&oneWire); float m_temperature; // Moisture Variables short m_moisture; // General Variables int m_timestamp; void setup() { Serial.begin(9600); Serial.println("SLAVE SENDER"); Serial.print("Node address: "); Serial.println(SLAVE_ADDRESS); Serial.print("Buffer size: "); Serial.println(BUFFER_SIZE * sizeof(short)); Serial.println("***********************"); m_timestamp = millis(); Wire.begin(NODE_ADDRESS); // Activate I2C network Wire.onRequest(requestEvent); // Set the request event handler temperatureSensors.begin(); } void loop() { if(millis() - m_timestamp < READ_CYCLE_DELAY) return; // Reads the temperature temperatureSensors.requestTemperatures(); m_temperature = temperatureSensors.getTempCByIndex(0); // Reads the moisture m_moisture = analogRead(A0); } void requestEvent() { data[0] = m_temperature * 10; // In order to use short, I multiple by 10 data[1] = m_moisture; Wire.write((byte*)data, BUFFER_SIZE * sizeof(short)); }

Got any arduino Question?

Ask any arduino Questions and Get Instant Answers from ChatGPT AI: ChatGPT answer me! pdf PDF - Download arduino for free Previous Next This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow logo rip SUPPORT & PARTNERS
  • Advertise with us
  • Contact us
  • Cookie Policy
  • Privacy Policy
STAY CONNECTED

Get monthly updates about new articles, cheatsheets, and tricks.

Subscribe Cookie This website stores cookies on your computer. We use cookies to enhance your experience on our website and deliver personalized content. For more details on our cookie usage, please review our Cookie Policy and Privacy Policy Accept all Cookies Leave this website

Tag » Arduino I2c Master And Slave At The Same Time