This function loads the slave address you want to talk to, intAddress, into a behind-the-scenes variable and specifies that the communication will be a WRITE action, meaning the Arduino is going to follow up the address and R/W flag with some quantity of data for the slave component. You imported the library in your includes, and you initialized the I2C system by using the Wire.begin() function, and now you want to actually do something. This is how you start talking with an I2C slave component. Oscilloscope Trace of Wire Function Begin As expected, nothing is happening because this is only a preparatory function. Here is an oscilloscope trace showing what appears on the SCL and SDA signal buses immediately after the Wire.begin() function was executed. It can be any 7-bit address, provided it doesn’t conflict with any other on the bus. The intAddress variable is the 7-bit I2C address you assign to your Arduino. Nearly exactly the same as the command above, this function joins your Arduino to the I2C bus, however it connects it as a slave component, rather than as a bus master.
This command has to exist in your code somewhere in order for I2C to work, and generally it’s placed in the setup portion of your code.
twi_init() sets a series of very specific configurations in the ATMEGA328P including the speed at which the I2C bus will communicate and prepares the interrupts necessary for it. This command prepares a few buffers for storing streams of data, and runs a function from twi.c called twi_init(), which is the Two Wire Interface Initialization routine. Each of the functions below were executed followed with a five second delay that allowed me to capture what the scope was displaying at each stage.Īrduino Wire Library Functions – Acting as Master The oscilloscope traces below were generated by setting the scope to trigger on a single negative going edge on the SDA signal bus, which would indicate the beginning of the Start Condition. I’ve listed them below in the order that you’re likely to use them. These are made available in your code by using the command #include “Wire.h” at the top of your sketch. There are nine functions created by the Wire library, that you use to make I2C magic happen with your Arduino. If you ever wondered exactly what makes the Arduino system so special, this is it there is a lot of sausage being made behind the scenes that you never have to deal with. That in turn, calls twi.h/twi.c which imports a WHOLE bunch of the more hardcore libraries from various sources, such as Atmel, the chip manufacturer. When you’re writing Wire.begin(), that command is defined in Wire.h / Wire.cpp. Now it’s not a perfect analogy, but you can think of these files as creating a machine, where Wire.h/Wire.cpp are the parts that let you write the easy-to-understand Arduino code, and twi.h/twi.c are the parts that take your easy-to-understand code and reconfigure it to something the C compiler wants to chew on. There are several files in that path, including the example code that is available to you in the IDE, but the heart of I2C communications with an Arduino resides in four files… The library and files all contain the word “wire” because Atmel (the chip at the heart of the Arduino) devised a system called “Two Wire Interface” or “TWI” which is their flavor of I2C. Arduino/libraries/Wire directory on the machine you write your code on. The Wire Library is a series of files in plain text that exist in the. Additionally, if you wish to replicate the oscilloscope traces, use any two channel oscilloscope capable of reading a 100kHz signal connected to the SDA and SCL lines between an Arduino and any I2C chip, and then send each function listed below as necessary with sufficient delay time in between to allow you to view the resulting scope trace. Be very careful when opening those files though, that you don’t modify them or you’ll need to reinstall everything from scratch. No setup is required, however you can access the files that comprise the wire library in the program folders installed with your Arduino IDE. No schematic is associated with this module. Understand which functions are used with an Arduino Master and which are used with an Arduino Slave.Know which functions actually cause data to transmit and which are only preparatory.Learn the nine Wire Library functions and their purpose.Understand why the code is called the Wire Library and not the I2C Library.Now that we have a firm understanding of how we need to structure the signals to communicate with an I2C component, let’s look at the Arduino Wire Library that will actually manage all of that signaling for us.