Im just testing everything on my robot to see if it works but Im a newbie to I2C. I have a MCP23016 I/O expander http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=MCP23016-I/SP-ND and I needa start learning how to use it. Its wired to pins A4 and A5 of the Arduino pro mini and its hooked up to 6 QRD1114 line sensors and 9 LEDs. I just need a bit of code to get me started, just like turning on a few LEDs and reading the line sensors. Any help appreciated.
You have not stated if you have other i2c devices you have programmed for in the past, so I will assume not.
The most common mistake is address which for is chip is :
0x20 + ( a2 * 4 ) + ( a1 * 2 ) + a0 , and not 0x40 + As
First Set BOTH DDRs to output
write_io(6,0),write_io(7,0); // ( 6&7 , 0xff ) for all inputs
to control the pins use "write_io(A,B)"
A = 0 or 1 , the first or second 8 bit port
B = The byte to control the port pins
void write_io(int cmd_reg, int push_this)
{
Wire.beginTransmission(io_address);
Wire.send(cmd_reg),Wire.send(push_this); // reset register pointer then set GP0 to push_this // use (0,??)
Wire.endTransmission();
} // end of write_io
Don`t forget to setup the i2c with “Wire.begin();”
I have never used I2C before. Do the adress pins on the I/O expander need to be connected to anything? I thought that would be so but the schematics i saw only showed the SDA and SCL pins connected to A4 and A5.
Yes , just ground them all for 0x20, don`t just let them float
To start with i2c I would have done with the MCP23008 which is sampler to connect because you haven't got any other components to insert.
E.g. the capacitor and resistor of the MCP23016
Can i get a complete code for turning on an LED using I2C? The bit you already posted is great but if i could get a little assistance on where to put the "turn on LED" that would be even better ;D
Not sure if this helps but I have some I2C code examples for a DS1337 RTC.
See the NB1A application hints at Loading...
(* jcl *)
This is my fully working and tested code.
io_int is a work around for setting the DDR and making sure that the DDR is set to 0 on both ports.
I have left you with 2 methods to controls the ports.
Wire.beginTransmission(io_address);
Wire.send(0); // set to reg - gpio
Wire.send(255); // <--- change this - 255 = all on - port 0
Wire.send(255); // <--- change this - 255 = all on - port 1
Wire.endTransmission();
or with
write_io(0,change this)
write_io(1,change this)
It is fully tested , so if it don`t work change the #define io_address 0x20 and swap over pins SDA and SCL wires.
#include <Wire.h>
#define io_address 0x20
int x = 0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
io_int();
}
void loop()
{
all_off();
delay(1000);
all_on();
delay(1000);
}
void all_off()
{
Serial.println("all_off");
Wire.beginTransmission(io_address);
Wire.send(0); // set to reg - gpio
Wire.send(0); // 0 = all off
Wire.send(0); // 0 = all off
Wire.endTransmission();
// or
// write_io(0,0) // port 0
// write_io(1,0) // port 1
}
void all_on()
{
Serial.println("all_on");
Wire.beginTransmission(io_address);
Wire.send(0); // set to reg - gpio
Wire.send(255); // 255 = all on - port 0
Wire.send(255); // 255 = all on - port 1
Wire.endTransmission();
// or
// write_io(0,255)
// write_io(1,255)
}
void io_int()
{
do {
x = read_io(6),write_io(6,255);
} while ( x != 255 );
do {
x = read_io(6);
write_io(6,0),write_io(7,0);
} while ( x != 0 );
} // end of int_io
int read_io(int cmd_reg)
{
int tmp;
Wire.beginTransmission(io_address);
Wire.send(cmd_reg); // 0 or 1 -- GP0 or GP1
Wire.endTransmission();
Wire.requestFrom(io_address, 1);
tmp=Wire.receive();
return tmp;
} // end of read_io
void write_io(int cmd_reg, int push_this)
{
Wire.beginTransmission(io_address);
Wire.send(cmd_reg),Wire.send(push_this); // reset register pointer then set GP0 to push_this // use (0,??)
Wire.endTransmission();
} // end of write_io
Hey I tried the program with both control methods and it didn't work. I tried switching the SDA and SCL pins also. same result... soooo is there any other things i can try? it will be a pain to switch out the chip (i regretfully didnt get a DIP socket). I hooked up the LEDs anodes to Vdd and thecathodes to the chip, so to turn it on you would have to connect the pins to ground. From looking at the program it should have been turning on the LEDs after the first second, but I just wanna see if theres anything else i can try?
give me a pin to pin on how it`s connected .
It`s got to be a wiring faut now , if you have the address right
Heres the datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20090C.pdf Ill type the pins in order from 1-28:
Vss: Gnd
GP1.0: QRD1114 Far Left Sensor
GP1.1: QRD Left
GP1.2: QRD Center Left
GP1.3: QRD Center Right
INT: Nothing
GP1.4: QRD Right
Vss: Gnd
CLK: Nothing
TP: Nothing
GP1.5: QRD Far Right
GP1.6: Nothing
GP1.7: LED green
SCL: Arduino SCL
SDA: Arduino SDA
A0:
A1: (A0-2 soldered together to Vss pin)
A2:
Vss: Gnd
Vdd: 5V
GP0.0: LED green
GP0.1: LED green
GP0.2: LED red
GP0.3: LED red
GP0.4: LED red
GP0.5: LED red
GP0.6: LED red
GP0.7: LED red
( LEDs connected like; GPx.x <-- resistor <-- LED <-- 5V )
Thanks for all your help, I'm like scrambling to get my robot done for the robothon next weekend :o :-/
what about the resistor / cap ?
I have a cap between the Vss and Vdd pins, but wheres the resistor supposed to go?
READ 1.4 Clock Generator
resistor CLK to 5+
cap CLK to Gnd
HARDWARE FAULT !!!!!!
3.9k resistor and 33pf cap? i hope to god that radioshack has those...
Read my second post and you now know what I`m on about !!!
To start with i2c I would have done with the MCP23008 which is sampler to connect because you haven't got any other components to insert.
E.g. the capacitor and resistor of the MCP23016
If you only need 8 port the mcp23008 is sampler .
yea I didnt know about that chip if i did i totally would have gotten it... so if i manage to get the resistor and cap in time could you give me a few tips on making a line following code? Nothing fancy, just enough to make it scoot around and follow a line...
No problems , you will have a better chance of it working with the cap / resistor .
yea im gonna call them tomorrow and pray that they have it. then I have less than a week to fix the motor drivers, program and test it. oh boy... :o
is there like any equivalent circuit? Im thinking about if i cant find a 33pf cap, is there any other way?
I don`t know I just use those values.