Arduino and I2C

Your source for all things Atmel.

Moderator: phalanx

Post Reply
pclyne
Posts: 35
Joined: Sun Mar 30, 2008 8:19 pm

Arduino and I2C

Post by pclyne » Thu Jan 01, 2009 8:16 pm

Hiya,

I recently purchased some Mini PCF8574 boards from a company called Futurlec.

I have successfully got the board talking I2C and would like to offer this very simple Arduino Sketch in the hope that it helps someone get their system working quicker.

For testing you need to connect from the [V] pin on the output header to a resistor {I used 560 ohm} then to your LED anode, tie LED cathode to pin [0] {or whatever pin you want to use to test}.

Hope it helps someone..

Enjoy


Code: Select all

/*
  Test program for PCF8574A I2C I/O expander from Futurlec
  - Blinks all pins low then high - 1 second cycle.
  by pclyne
*/

#include <Wire.h>

#define CARD_ADR B0111000  //PCF8574A chips are of address format 0111 [a1] [a2] [a3]
                           // Address with three address pins grounded. = 0111000
			   // Note that the R/W bit is not part of this addressas the wire library takes care of the LSB

void setup() {
  
  Wire.begin();
  Serial.begin(57600);
  
}


void loop() {
  
  //Turn all pins off
  Serial.println("Write B00000000.");
  CardWrite(B00000000);
  delay(1000);
  
  //Turn them back on
  Serial.println("Write B11111111.");
  CardWrite(B11111111);
  delay(1000);
  
}





void CardWrite(byte txdata) {
  
  Wire.beginTransmission(CARD_ADR);
  Wire.send(txdata);
  Wire.endTransmission();
  
}

HMC_UAV
Posts: 5
Joined: Wed Jan 02, 2008 9:06 pm
Location: Claremont, CA

Post by HMC_UAV » Sat Jan 17, 2009 10:41 pm

Hey, this is a nice concise example, thanks!

Now I wonder if I might pose a question about reading bytes from an i2c device with the Arduino. You see, I'm having a problem interfacing with an SCP1000 pressure sensor with my Arduino Mini Pro. I am pretty sure I am writing bytes to the sensor over i2c just fine, but I cannot read registers, and I think that it is because of the way that the Wire library is set up in the Arduino project. Has anyone else noticed that the Wire library does not allow for a repeated i2c "start" condition? Is there a way to get around this or would I have to delve into Wire.h and mess around a bit?

HMC_UAV
Posts: 5
Joined: Wed Jan 02, 2008 9:06 pm
Location: Claremont, CA

Post by HMC_UAV » Sat Jan 17, 2009 10:58 pm

By the way, for those not familiar with what START and STOP conditions are on i2c and how the waveforms actually travel down the line, I would recommend reading the manual for the SCP1000 sensor. The tricky part is that the datasheet and manual are different. The manual's nice since it describes how to interface to it (with SPI or I2C since it comes in both flavors). The manual can be found here:

http://www.vti.fi/en/products/pressure- ... e_sensors/

On the right side of the page look for "SCP1000 Product Family Specification rev 0.08.pdf"

Also, I thought it might help to explain the problem I think I'm having with an example: Let me abbreviate the arduino as uC and the scp1000 as SCP. So in order to read a register (lets say the status register), you need to send the following sequence:

START - uC sends 7 bit address of scp1000 - uC sends write bit (0) - SCP sends acknowledge - uC sends 8 bit address of status register - SCP sends ack - Another START - uC sends 7 bit address of scp1000 - uC sends read bit (1) - SCP sends ack - SCP sends 8 bits of status register - uC sends non-acknowledge to tell it that's it - STOP

I believe that the way the Wire library is set up, anytime you do a send, like in the pclyne's example with Wire.beginTransmission(), Wire.send() and Wire.endTransmission(), then it sends a STOP condition and prevents you from jumping into a read. I have tried getting rid of the Wire.endTransmission(), but that doesn't work, since I believe you can queue up several Wire.send()'s and the Wire.endTransmission() line of code is what actually sends them off.

Argh, I'm sorry for such long posts, but hopefully I'm making the problem clear, or at least clear enough for someone to see the errors I am making.. Thanks!

doctek
Posts: 7
Joined: Tue Sep 09, 2008 1:16 pm

Post by doctek » Sat Jan 31, 2009 4:26 pm

You've explained the problem perfectly. What you describe is the protocol for reading multiple items on the I2C bus and is most commonly used for reading SRAM or EEPROMs.

For more discussion about the I2C bus and the what drivers need to do, you may be interested in this Instructable.
http://www.instructables.com/id/I2C_Bus ... nd_ATmega/

Unfortunately, the drivers are in C, not Wiring. If someone is real familiar with Wiring and would like to work on a conversion, I'd be glad to help.

HMC_UAV
Posts: 5
Joined: Wed Jan 02, 2008 9:06 pm
Location: Claremont, CA

Post by HMC_UAV » Mon Feb 02, 2009 2:10 pm

Thanks doctek, very nice Instructable! Perhaps sometime I will delve into the Wire library and see how I can make it do what I want..

tz
Posts: 148
Joined: Sun Aug 23, 2009 10:40 pm

Post by tz » Mon Jan 18, 2010 9:25 pm


Post Reply