Difference between wire.write and write(analog/digital)

I have gathered from the references that wire.h and its commands are used with serial communication(I2C) devices. But I have come across examples of sensors that do not use wire.h commands but can even then be configured.
Now, if sensors (digital or analog) can be given instructions using digitalWrite or analogWrite, then what is the need of wire.h ?

I tried to understand it by examples and Arduino references but I wasn't able to understand it.
I have read that most of the sensors communicate serially and use I2C protocol.

I am including examples of potentiometer programmed with and without Wire.h and also an example of Grove Vibration Sensor SW-420 programmed digitally(a sensor using serial communication but not using Wire.h ?)

Anyhelp would be greatly appreciated.
Thanks in advance !

The items you mentioned are three different concepts entirely:

  • Wire is really Arduino’s poorly-chosen name for I2C (as you mentioned). It is a complete protocol standard for data transfer. That transfer it typically between ICs, hence the name - I(nter)-I(C)-C(ommunication) == IIC == I2C. Google will provide an abundance of references.

  • digitialWrite is the Arduino-ese command for setting a digital output HIGH or LOW.

  • analogWrite (on most Arduinos) simulates setting an output pin to an analog level using P(ulse) W(idith) M(odulation). Again, plenty of info from Google. On some boards, analogWrite is able to control actual hardware Digital to Analog Converts (DAC) associated with certain pins. Once again, check Google.

gfvalvo:
The items you mentioned are three different concepts entirely:

  • Wire is really Arduino’s poorly-chosen name for I2C (as you mentioned). It is a complete protocol standard for data transfer. That transfer it typically between ICs, hence the name - I(nter)-I(C)-C(ommunication) == IIC == I2C. Google will provide an abundance of references.

  • digitialWrite is the Arduino-ese command for setting a digital output HIGH or LOW.

  • analogWrite (on most Arduinos) simulates setting an output pin to an analog level using P(ulse) W(idith) M(odulation). Again, plenty of info from Google. On some boards, analogWrite is able to control actual hardware Digital to Analog Converts (DAC) associated with certain pins. Once again, check Google.

That doesn't answer my question.
As I have mentioned earlier, wire.h is used for I2C , which I know, but sensors and potentiometers which use serial transfer(I2C) can be programmed without it as well the examples of which I have included along with. So, then why and when is it actually used(I am missing something, I'm sure but don't know what) ? Please have a look at the examples (two of which are from Arduino references only) and help me. And as I said, I already tried references and Google before coming here as I wasn't able to understand it.
Thanks

holdingpattern:
Most of what sensors?

I guess the author here meant that in general I2C applies to most of all the sensors by this" While I2C is almost never used for PC-device communication, it is incredibly popular with modules and sensors" .

holdingpattern:
It might help to realise that I2C is a bus, that is, a huge number of devices can be accessed using the same two pins. (Subject to the device designers making the addresses modifiable in case of conflict, and subject to the absolute limit which I forget right now.)

If you wanted to read a load of potentiometers for example, via analogRead(), you're limited to the number of vacant analog inputs, unless you get fancy with external expanders and wotnot.

But I can easily connect say an I2C LCD, an I2C RTC, and an I2C temperature/pressure sensor all on the same two pins as a bus. Makes life a lot simpler.

Source

Thanks anyway ! I think I got some help through this

gfvalvo:

  • Wire is really Arduino’s poorly-chosen name for I2C (as you mentioned). [...]

The name is well chosen based on what the datasheet says:

wireData.png

wireData.png

The name is well chosen based on what the datasheet says:

rofl. Best joke of week. :slight_smile:
karma+

GolamMostafa:
The name is well chosen based on what the datasheet says:

wireData.png

So, names apart, let's stick to the original question.
Can you give a more detailed answer ?
What I have gathered till now is we should use wire.h and its commands to connect multiple modules to the same pin. Should those modules be analog or digital or can it program both of them ? And what about the address of the devices connected to the same pin ?

The modules can be analogue or digital, but they must be I2C, and they must have different addresses.

1. A set of agreed rules (the protocol) known as 'I2C Bus' developed by Phillips Semiconductor to exchange data in serial fashion among many devices within a circuit board (hence the name: Inter Inter Circuit). The same protocol is called 'TWI Bus' in Atmel Literature.

2. Characteristics of a TWI/I2C Device (the Master: ATmega328P MCU of UNO Board)
(1) It must have a digital interface called I2C Logic or I2C Interface.

(2) The I2C Logic has two signals:
(a) Unidirectional SCL (Serial Clock) line.
(b) Bidirectional SDA (Serial Data) line.
(c) A set of programmable registers to handle data transmission/reception.

(3) The PC4 and PC5 lines of ATmega328P are automatically converted to an I2C Bus/Logic (Fig-1) when the uses includes the following codes at the appropriate place of his sketch.

#include<Wire.h>
Wire.begin();

i2c-2.png
Figure-1:

3. Characteristics of a TWI/I2C Device (the Slave: BME280 PTH sensor)
(1) It must have a digital interface called I2C Logic or I2C Interface.

(2) The I2C Logic has two signals:
(a) Unidirectional SCL (Serial Clock) line.
(b) Bidirectional SDA (Serial Data) line.
(c) It must have 7-bit I2C Bus address; the range is: 0001000b - 1111111b (0000000 - 0000111 are reserved). The address value is usually factory set.
(d) There is no programmable registers in this sensor. (I am not sue.)

4. Characteristics of a TWI/I2C Device (the Slave: another ATmega328P MCU of UNO Board)
(1) It must have a digital interface called I2C Logic or I2C Interface.

(2) The I2C Logic has two signals:
(a) Unidirectional SCL (Serial Clock) line.
(b) Bidirectional SDA (Serial Data) line.
(c) It must have 7-bit I2C Bus address; the range is: 0001000b - 1111111b (0000000 - 0000111 are reserved). The address value is programmable (not factory set).
(d) A set of programmable registers to handle data transmission/reception.

i2c-2.png

GolamMostafa:
1. A set of ...

Thanks a lot ! That helped a lot

Just one doubt left. You are saying that the slave modules(and even the master modules for that matter) should have a digital interface but here

TheMemberFormerlyKnownAsAWOL:
The modules can be analogue or digital, but they must be I2C, and they must have different addresses.

AWOL mentions that interfacing analogue devices is also possible ? Or is it that even the analogue devices are communicating through ADCs and DACs ?
Pardon my ignorance ! :confused: :frowning:

Re analog sensors...

As an example, here is the data sheet for an i2c temperature sensor chip.

See section 5.1.3 (which notes an onboard delta-sigma ADC)

Look at the following circuit (Fig-1) where the ADC's (the analog devices) data are being supplied to NANO-2 (slim/breadboard version of UNO) via I2C Bus.
i2c-4.png
Figure-1.

Look at the following circuit(Fig-2) where data is being supplied to a DAC (a mixed device) via I2C Bus.
i2c-3.png
Fig-2:

i2c-4.png

i2c-3.png

Thanks a lot to all !