Reading from a Slave on The I2C Bus:
Reading from a slave is very similar to writing, but with some extra steps. In order to read from a slave, the master must first instruct the slave which register it wishes to read from. This is done by the master starting off the transmission in a similar fashion as the write, by sending the address with the R/W bit equal to 0 (signifying a write), followed by the register address it wishes to read from. Once the slave acknowledges this register address, the master will send a START condition again, followed by the slave address with the R/W bit set to 1 (signifying a read). This time, the slave will acknowledge the read request, and the master releases the SDA bus, but will continue supplying the clock to the slave. During this part of the transaction, the master will become the master-receiver, and the slave will become the slave-transmitter.
The master will continue sending out the clock pulses, but will release the SDA line, so that the slave can transmit data. At the end of every byte of data, the master will send an ACK to the slave, letting the slave know that it is ready for more data. Once the master has received the number of bytes it is expecting, it will send a NACK, signaling to the slave to halt communications and release the bus. The master will follow this up with a STOP condition.
Read from one register in a device
Real Time Clock Interfacing with Arduino using I2C bus:
Even though Arduino and almost all microcontrollers have built-in timers and timekeepers (millis () in case of Arduino), they are power dependent i.e. they run as long as there is power supply. Once the power is turned off (manually or due to power outage), all the timers are reset to 0. Hence we need an alternative in projects like data loggers, clocks, alarms and like where the timer runs independently irrespective of the external power. Here comes the use of Real Time Clock ICs. Almost all RTC ICs are low-current devices that run for years on a single lithium cell. One of the popular and most commonly used RTC ICs is the DS1307 Real Time Clock. This RTC can maintain full clock and calendar i.e. hours, minutes, seconds as well as year, month and day.
DS1307 Pin diagram
Pin description of DS1307 RTC:
- X1 and X2: These are pins for connecting the crystal of frequency 32.768 KHz to enable the internal oscillator. If an external oscillator is connected to X1, then X2 can be left floating.
- VBAT: Battery Power Supply Pin. Must be connected to a 3V Lithium cell for backup supply.
- GND: Ground Pin.
- SDA: Serial Data Pin. It is the Data Input/Output pin of the I2C Interface. An external pullup of 5V is required, usually through a 10KΩ Resistor.
- SCL: Serial Clock Input Pin. It is the clock input pin of the I2C Interface. It must also be pulled up to 5V through a 10KΩ resistor.
- SQW/OUT: Square wave output pin. If not used, it can be left floating.
- VCC: The main supply pin.
For Intefacing DS1307 with Arduino UNO Click here
For Interfacing External Memory with Arduino UNO Click here