Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Answer:
The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the Arduino Controller board pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices. As the clock line changes from low to high (known as the rising edge of the clock pulse), a single bit of information - that will form in sequence the address of a specific device and a a command or data - is transferred from the board to the I2C device over the SDA line. When this information is sent - bit after bit -, the called upon device executes the request and transmits it's data back - if required - to the board over the same line using the clock signal still generated by the Controller on SCL as timing.
Because the I2C protocol allows for each enabled device to have it's own unique address, and as both controller and peripheral devices to take turns communicating over a single line, it is possible for your Arduino board to communicate (in turn) with many devices, or other boards, while using just two pins of your microcontroller.
In order to build the circuit for interfacing LCD display with two Arduion boards in master slave configuration, I have used 2 Arduino Uno boards and one I2C LCD display.
First I connected the A5 pin of the first board with the A5 pin of the second board and the A4 pin of the first board with the A4 pin of the second board using two wires. I connected the ground of the first board with the ground of the second board.
I selected an I2C LCD to be connected to the slave Arduino board. The LCD has 4 pins, 1 pin for input voltage, Vcc (5 volts), 1 pin for ground, 1 SCL pin for clock synchronization and 1 SDA pin for data transfer. I connected the Vcc to 5 volts pin in the slave Arduino board, the ground to the ground pin in the slave Arduino board and the SDA and SCL pins to the SDA and SCL pins on the slave Arduino board.
With this, all the hardware connection were complete and the circuit was ready to be programmed.
In the program I included the header files, 'Wire.h' for I2C communication and 'Adafruit_LiquidCrystal.h' for the LCD. I the master Arduino board, I initialized the I2C communication using 'Wire.begin()' in the setup function. In the loop function, I started the I2C transmission by using 'Wire.beginTransmission(1)' using the address, '1'. I wrote the message to be transmitted using, 'Wire.write()' API. I transmitted one text message, 'Hello' followed by one integer variable, '250'. Then I ended the transmission using 'Wire.endTransmission()'. After this, I introduced a delay of 500 msec to ensure that there is some time for processing the received information at the slave Arduino board.
In the slave Arduino board, I initialized the I2C reception by using the 'Wire.begin(1)' API tuned to the address '1'. I also initialized the LCD display by using 'lcd_1.begin' in the setup function. In the loop function, I printed the received message (both text and integer) after setting the cursor to (0,1) which is the second line. After this I introduced a delay of 100 msec to ensure synchronization with the master Arduino board. I declared the transmission of a message from the master board as an event using 'Wire.onReceive()'. The event was named, 'receiveEvent'. After writing the code in the loop function, I declared the new function named 'receiveEvent' in which I read the text message using a for loop to read it character by charater. I used 'Wire.read()' to read the transmitted message. Then I used the same API to read the integer value into a variable named, 'y'. Once this data was read into the respective variables, it was printed in the loop function.
I will attach a screeshot of the output for reference. The circuit can be tested on Tinkercad. The link to the circuit is given below:
================================END=================================