Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino internal integrated circuit


May 15, 2021 Arduino


Table of contents


Internal Integrated Circuits (I2Cs) are serial data exchange systems for microcontrollers and next-generation specialized integrated circuits. /b10> Used when the distance between them is very short (receivers and transmitters are usually on the same printed circuit board). /b11> A connection is made by two wires. /b12> One for data transmission and one for synchronization (clock signal).

As the following illustration shows, one device is always the primary device. /b10> It performs an addressing from the chip before communication begins. T his allows a microcontroller to communicate with 112 different devices. /b12> Baud rates are typically 100 Kb/sec (standard mode) or 10 Kb/sec (slow Baud mode). /b13> A system with a Baud rate of 3.4 Mb/s has recently emerged. /b14> The distance between devices communicating via the I2C bus is limited to a few meters.


Arduino internal integrated circuit


The I2C pin of the board

The I2C bus consists of two signals - SCL and SDA. /b10> SCL is the clock signal, SDA is the data signal. /b11> The current bus host always generates a clock signal. /b12>Some from the device may force the clock to turn low to delay the primary device from sending more data (or request more time to prepare the data before the master tries to write it out). /b13> This is called "clock stretching".

Here are the pins for the different Arduino plates:

  • Uno, Pro Mini A4 (SDA), A5 (SCL)
  • Mega, Due 20 (SDA), 21 (SCL)
  • Leonardo, Yun 2 (SDA), 3 (SCL)

Arduino I2C

We have two modes - the main code and the code from - that use I2C to connect the two Arduino boards. It's:

  • Master Transmitter / Receiver Slave Main Transmitter / Slave Receiver
  • Master Receiver / Slave Transmitter Main Receiver / Slave Transmitter

Main transmitter/receiver

Let's now look at what is the main sender and from the receiver.

The main transmitter

The following functions are used to initialize the Wire library and add the I2C bus as the primary or from the device. /b10> This is usually called only once.

  • Wire.begin ( Address ) - ) our case, the address is a 7-bit slave address, because the host is not specified, it will join the bus as a host.

  • Wire.beginTransmission ( Address ) - Start sending data from the device to I2C at a given address.

  • Wire.write ( Value ) - A queue byte used to transfer from the primary device to the device (a call between beginTransmission() and endTransmission().

  • Wire.endTransmission() - Ends the transfer from the device starting with beginTransmission() and transmits bytes queued by wire.write().

Example

#include <Wire.h> //include wire library

void setup() //this will run only once { 
   Wire.begin(); // join i2c bus as master
} 

short age = 0; 

void loop() {   
   Wire.beginTransmission(2); 
   // transmit to device #2
   Wire.write("age is = ");
   Wire.write(age); // sends one byte
   Wire.endTransmission(); // stop transmitting
   delay(1000); 
}

From the receiver

Use the following functions:

  • Wire.begin ( Address ) - ) address is a 7-bit slave address.

  • Wire.onReceive (received data ) - a function called when data is received from the device from the primary device.

  • Wire.available() - Returns the number of bytes that Wire.read() can use to retrieve and should be called in the Wire.onReceive() handler.

Example

#include <Wire.h> //include wire library

void setup() {  //this will run only once
   Wire.begin(2); // join i2c bus with address #2
   Wire.onReceive(receiveEvent); // call receiveEvent when the master send any thing 
   Serial.begin(9600); // start serial for output to print what we receive 
}

void loop() {   
   delay(250); 
}

//-----this function will execute whenever data is received from master-----//

void receiveEvent(int howMany) { 
   while (Wire.available()>1) // loop through all but the last {
      char c = Wire.read(); // receive byte as a character
      Serial.print(c); // print the character
   }
}

Main receiver/from transmitter

Let's now look at what is the main receiver and from the transmitter.

The main receiver

The host is programmed to request and then read the bytes of data sent from the unique addressed machine Arduino.

Use the following functions:

Wire.requestFrom (address, bytes) - ) device is used to request bytes from the device. You can then retrieve bytes using the functions wire.available() and wire.read().

Example

#include <Wire.h> //include wire library void setup() { 
   Wire.begin(); // join i2c bus (address optional for master) 
   Serial.begin(9600); // start serial for output
} 

void loop() { 
   Wire.requestFrom(2, 1); // request 1 bytes from slave device #2
   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); 
}

from the transmitter

Use the following functions:

Wire.onRequest - The ) is called when the primary device requests data from the device from this.

Example

#include <Wire.h> 

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

Byte x = 0;

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(x); // respond with message of 1 bytes as expected by master
   x++; 
}