In this blog, we will be discussing the Wire Library.
Wire Library is used for communication between two boards in the I2C Communication Protocol.
In Arduino Uno, the Analog Pins A4 and A5 are used for this purpose.
A4 = SDA ( Data Bus )
A5 = SCL ( Clock Bus )
The basic functions used in the Wire Library are as follows...
1.Wire.begin() or Wire.begin(Slave_Address)
Wire.begin() intializes I2C communication as an Master Device.
Wire.begin(Address) initializes I2C communication as a Slave Device.
The Address is of 7 bit and thus at max, we can connect 127 devices in this protocol.
The Address is generally specified in the datasheet.
The address up to 8 is reserved by the manufacturer and thus can't be used.
2. Wire.requestFrom(Slave_Address, No. of bytes,'TRUE'/'FALSE')
It is used by the Master to request slave for data bytes.
If TRUE a stop message is sent after the request releasing I2C bus.
If FALSE a restart message is sent after the request.
3. Wire.beginTransmission(Slave_Address)
It basically begins the transmission process.
4. Wire.endTransmission('TRUE'/'FALSE')
It basically ends the transmission process.
If TRUE a stop message is sent after the transmission releasing I2C bus.
If FALSE a restart message is sent after the transmission.
5. Wire.write(Data)
This function is used for sending the data.
The data could be a value, string or an array.
The syntax for the same is as shown...
---Wire.write(value)...sends value as a byte
---Wire.write(string)...sends strings as a series of bytes
---Wire.write(array, length)...sends the specified no. of bits (=length) of the array
6. Wire.available()
This function returns the number of bytes available for reading
7. Wire.read()
This function is used for receiving the data.
8. Wire.SetClock(Clock_Frequency)
This function is used for setting the clock frequency for I2C communication.
The by default frequency is...100 kHz (i.e. Standard Mode)
The other supported frequencies are...
Fast Mode = 400 kHz
Fast Plus Mode = 1 MHz
High-Speed Mode = 3.4 MHz
Refer to the datasheet to know whether your board supports these modes or not.
9. Wire.onRecieve(handler)
This function basically directs the data when received by a slave to a function i.e called handler.
The handler should be of type...
----- void handler_name(int bytes_read)
i.e. return value = void and argument = int
10. Wire.onRequest(handler)
This function basically directs to a function (called handler)when the request is received.
The handler should be of type...
----- void handler_name()
i.e. return value = void and argument = void
Wire Library is used for communication between two boards in the I2C Communication Protocol.
In Arduino Uno, the Analog Pins A4 and A5 are used for this purpose.
A4 = SDA ( Data Bus )
A5 = SCL ( Clock Bus )
The basic functions used in the Wire Library are as follows...
- begin()
- requestFrom()
- beginTransmission()
- endTransmission()
- write()
- available()
- read()
- SetClock()
- onRecieve()
- onRequest()
Now lets start discussing about these functions...
1.Wire.begin() or Wire.begin(Slave_Address)
Wire.begin() intializes I2C communication as an Master Device.
Wire.begin(Address) initializes I2C communication as a Slave Device.
The Address is of 7 bit and thus at max, we can connect 127 devices in this protocol.
The Address is generally specified in the datasheet.
The address up to 8 is reserved by the manufacturer and thus can't be used.
2. Wire.requestFrom(Slave_Address, No. of bytes,'TRUE'/'FALSE')
It is used by the Master to request slave for data bytes.
If TRUE a stop message is sent after the request releasing I2C bus.
If FALSE a restart message is sent after the request.
3. Wire.beginTransmission(Slave_Address)
It basically begins the transmission process.
4. Wire.endTransmission('TRUE'/'FALSE')
It basically ends the transmission process.
If TRUE a stop message is sent after the transmission releasing I2C bus.
If FALSE a restart message is sent after the transmission.
5. Wire.write(Data)
This function is used for sending the data.
The data could be a value, string or an array.
The syntax for the same is as shown...
---Wire.write(value)...sends value as a byte
---Wire.write(string)...sends strings as a series of bytes
---Wire.write(array, length)...sends the specified no. of bits (=length) of the array
6. Wire.available()
This function returns the number of bytes available for reading
7. Wire.read()
This function is used for receiving the data.
8. Wire.SetClock(Clock_Frequency)
This function is used for setting the clock frequency for I2C communication.
The by default frequency is...100 kHz (i.e. Standard Mode)
The other supported frequencies are...
Fast Mode = 400 kHz
Fast Plus Mode = 1 MHz
High-Speed Mode = 3.4 MHz
Refer to the datasheet to know whether your board supports these modes or not.
9. Wire.onRecieve(handler)
This function basically directs the data when received by a slave to a function i.e called handler.
The handler should be of type...
----- void handler_name(int bytes_read)
i.e. return value = void and argument = int
10. Wire.onRequest(handler)
This function basically directs to a function (called handler)when the request is received.
The handler should be of type...
----- void handler_name()
i.e. return value = void and argument = void
Comments
Post a Comment