I2C tutorial in 5 minutes + Arduino & Raspberry Pi implementation. Electronics Crash course # 13
Channel: Saral Tayal
Date: 2019-03-17

Enroll in the full version of 'Electronics Crash Course' here: https://www.udemy.com/electronics-in-a-nutshell/?couponCode=TINKERSPARK

My FREE introduction to robotics course- https://tinkerspark.teachable.com
Full Video article: http://tinkerspark.org/i2c_tutorial
Facebook- https://www.facebook.com/tinkerspark1/
Twitter- https://twitter.com/saraltayal
Website: https://tinkerspark.org
___
Video Article-

I2C in 3 mins

I2C is a communication protocol that is used to transmit information between electronics. It is used in professional consumer goods, and hobby projects as well. 
We can connect 1008 devices within a communication loop and only use 2 wires
It was developed by philips in 1982 to standardized communication between the cpu and peripherals inside its audio and video appliances. Since then the protocol has been periodically updated since then and now can support up to 3.2 megabit speeds
Now lets understand how it works.
Uses 2 wires to communicate information. These are Serial data- SDA, & Serial clock-SCL
It supports multiple devices on one loop and multiple ‘masters’ 
I2C messages are sent as messages. Now each message is broken into individual frames or segments which are responsible for different things. 
First we have the start condition,
Followed by our address frame where i2C sender(master) communicates the recipient of the message (Slave) 
Followed by a read/write bit which states if the sender is sending information or receiving information
Followed by the acknowledge bit by the reciver to ensure that the information so far has been successfully received by the intended recipient(slave). Think of roger or copy in military context
Lastly we sent our actual data followed by our stop condition.  

what is it up against
• serial- another 2 wire communication protocol. You have probably seen this in your arduino 'serial monitor'. Serial is asyncronnous which means clock information and this has to be established beforehand. Furthermore Serial doesn’t support more than 2 devices like I2C can do.

SPI is closer to I2C in the sense that you can control multiple devices. However SPI uses many more pins than I2c. Furthermore while SPI is established protocol but not standardized so its implementation might differ from device to device. 

What are some downsides
The actual communication and software behind is  quite complex however, most of this work is done for us by device manufacturers and open source libraries. 
Slower than SPI
Requires dedicated hardware which means you can’t bitbang. Bitbanging is using software to decode signals instead of harware. 


What are some positives
I2c doesn’t need dedicated lines to enable each receiver(slave) unlike SPI. This means you can have up to 1008 devices all running with just 2 wires.
Ack bit confirms that data has been successfully received
Standardized protocol makes it plug and play with multiple different electronics. 
I2C can have multiple master or transmitting devices within a loop unlike SPI. Look at the pic for more info


Raspberry pi Implementation 
The raspberry pi has 2 I2C pins pains- pins (3,5 or 27,28). 
To activate I2c, you need to go into raspi-config, then into interfacing options and enable i2c. 
Make sure to have the rpi.GPIO python package installed on your RPI
Arduino Implementation 
Each arduino board has different I2C pins. The uno uses A4 and A5. 
Lastly import the wire library to implement I2C in your code, 






Links-
http://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/
https://learn.sparkfun.com/tutorials/i2c/all
https://www.best-microcontroller-projects.com/i2c-tutorial.html
https://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/
https://en.wikipedia.org/wiki/I%C2%B2C