Using Arduinos is great and all, but what happens when you run out of pins? Sure, you could buy a Mega, but what if you already have a few Unos, or maybe some Micros? There is a nifty protocol in electronics nowadays, and it's called Serial. Oh, you've heard of it? Well, Serial communication can range from USB to these talking Arduinos. They talk on an I2C bus, or an Inter-Integrated Circuit bus. There can be up to 128 devices on a single I2C bus, because they all have their own names, or addresses, that allow them to be called by name. What else is really great about I2C? It only requires 4 wires! How amazing!
To show how simple I2C works with Arduino, I will be showing you how to connect and program 2 different Arduinos to split up a task into parts. In this case, I will take my existing Temperature display with momentary backlight and have the button read by the slave and the display/temperature controlled by the master.
You will notice quickly that these are not your normal Arduinos. I was recently selected to receive an Intel Edison and IoT Dev Kit by Instructables, which is where the Grove modules came from. Also, I bought a Teensy 3.1 a few months ago so I could tinker with the capacitance/many digital pins.
Before going any farther, I want to thank Intel and Instructables for being awesome by selecting me along with 249 others to receive a Dev Kit. That was very surprising to me, and I am very grateful for it. Thank you!
Step 1: Hooking It All Up
For the most part, I used my setup from my other Instructable, Intel Edison/Arduino Temperature Display with Momentary Backlight. The only difference is that I have another Arduino device which has the push button connected to. If you don't have another Grove shield for your second Arduino, you can stick the ends of breadboard jumpers into the end of the Grove cables. For the button, put power into the red hole, ground in the black, and the digital pin in the yellow. For the I2C, you will need to share power and ground between both devices, and bridge the SDA with the SDA and the SCL with the SCL. I2C is not like UART communication where the first output goes into the second input, instead the SDA (Serial DAta) is shared between both, and the SCL (Serial CLock) is shared.
You have an option with wiring the I2C. You can either take jumpers to both devices, or you can plug a grove cable into the Edison and do the same as you did with the button cable.
Step 2: Programming It
I put my code up on Github, like I did with my other temperature display: https://github.com/Frowney001/I2C_Temperature_Momentary-BL.
The way I2C works is it changes the clock to find the right device and tell it that a packet is coming through. The data line then communicates, byte by byte, the information. In this example, the master sends a "request" or a packet telling the slave that it wants it to send information. The slave sees this, and writes back the requested information, or the button press.
You can analyze the code and modify it to do whatever you want. I will tell you what you can change if you want to have it the same with your own customization.
In Master.ino (with the format of <Line#:what it does>):
- 11: The analog pin the temperature sensor is plugged into.
- 16: The serial address of the slave device.
- 21-23: The color of the backlight when the button is pressed.
- 40: The message displayed on the first line of the LCD.
- 61: The timeout period of the display backlight.
- 71: The refresh time for the temperature display.
- 75: The position of the temperature printout.
In Slave.ino (with the same format as above):
- 4: Pin the button is connected to.
- 6: Serial address of the device (must be the same as what the Master expects.)
That's pretty much it! Upload the slave code to the slave device and the master code to the master device, and watch it work! If you have any questions or tips, please let me know!