Monday, 20 January 2014

40. A foray with I²C

It's been a while since I built something that didn't come in a kit.

I've had a RTC (Real Time Clock) chip (PCF8563) in my collection for a while as I wanted to get to grips with I²C.

I²C is a protocol for allowing ICs to communicate via a 2 wire interface.  One wire is the clock (SCL) and the other is the data (SDA).  This saves on the number of connections and also simplifies circuit design.

My early experiments writing bit bashing code on a PIC to speak I²C ended up fruitless and I thought I had missed some vital information.  Needless to say, it put me off for a bit.  I got other people's code to work on the PIC but I wanted to understand it because I thought it might be cool to get the Z80 to talk to a RTC.

However, the Arduino boards have a dedicated I²C interface and library code that makes it a piece of cake to get started.  So I dug out the PCF8563 and started coding.  Fortunately there was even code examples for Arduino using the PCF8563. Once you've seen how the code should be written its a lot easier to roll your own.

So I got the RTC clock wired up, and added my 16x2 LCD and in no time had a clock working.  It seemed a bit too easy, so I wondered what else I could connect to the I²C bus.  I found the DS1631 temperature/thermostat IC.

Each I²C device has a different address.  I don't know how unique they are as there are only 128 addresses available.  Bit 0 is used to signify read or write.  Luckily the RTC and Temp IC are different.

Needless to say it wasn't to hard to talk to the DS1631 as well using the Wire library and display the temperature.

To spice things up a bit more I wanted to divorce the ATMega328P from my Arduino Uno and put it directly on the breadboard.  This needed an additional 16MHz crystal and 2x 22pF capacitors.

Lastly a battery backup for the RTC clock which is quite easy to connect by using diodes to make sure the current goes in the right direction.

The software allows you (via the 2 buttons) to set the date and time and set a thermostat temperature, which when reached lights the red LED via a 1K resistor at the bottom.  In fact the DS1631 will operate on its own once configured to control a heating system.

Update: somehow the 1K resistor was a 47 ohm.  I noticed that when the thermostat temperature was reached and the LED lit up the temperature would go back up again.  It seems without much resistance on the output it causes the IC to warm up due to the current flow.  What I have done now it put a 10k resistor and fed that into a BC548B transistor to light the LED.

No comments:

Post a Comment