Blink 2 leds with MCP23016 I/O expander

Status
Not open for further replies.

ZTiK.nl

Well-known member
Hi again :)

I am trying to turn on 2 leds using an MCP23016, but I haven't been able to get them working yet.
I have connected everything like this example states.
MCP23016.jpg
I connected the bottom blue wire to SCL/pin A5 and the top blue wire to SDA/pin A4

When I remove the lines from pin 23 & 24 on the MCP23016 and connect a jumper between the 3.3v and the two resistors, the leds turn on, so the leds must work.

To make them light up, I am using the following code:
Code:
#include <Wire.h>;
#include <inttypes.h>;

void setup()
{
  Wire.begin(); //Initialisatie van de wire klasse
  sendData(0x06, 0x00); //binair 00000110, 00000000 //Zet alle 8 poorten op GP0 op output
  sendData(0x07, 0x00); //binair 00000111, 00000000 //Zet alle 8 poorten op GP1 op output
}

void loop()
{
  sendData(0x00, 0x04); //binair 00000000, 00000100 //zet poort 2 van GP0 hoog, de rest laag, pin 23 van de MCP23016 (GP0.2)
  Serial.println("sendData(0x00, 0x04);");
  delay(500); //wacht 500 ms
  sendData(0x00, 0x08); //binair 00000000, 00001000 //zet poort 3 van GP0 hoog, de rest laag, pin 24 van de MCP23016 (GP0.3)
  Serial.println("sendData(0x00, 0x08);");
  delay(500); //wacht 500 ms
}

void sendData(uint8_t byte1, uint8_t byte2)
{
  Wire.beginTransmission(0x20);
  Wire.send(byte1);
  Wire.send(byte2);
  Wire.endTransmission();
}
This example was supplied by iPrototype (where I bought the expander) and can be found here. (contains some more dutch comments though)


If I understand the process right, both lights should light up one after the other and each lights for a period of 500ms
It should be pretty straight-forward so I don't see where I am going wrong...


I have been using the supplied resistor/condensator, but I do notice that the condensator looks a bit different and is not blue as the product description suggests.

Am I wrong in using the supplied parts, are these meant for 5v perhaps?
Resistor 3K9 Ohm
Condensator 33 pF
 
What kind of test equipment do you have? I'm not sure about the code aspect, but verifying the clock is present on the port expander will at minimum require a logic probe so you can verify there are pulses present on the TP pin. Though you'd need a scope or a logic analyzer to see exactly what those pulses are.
 
What kind of test equipment do you have? I'm not sure about the code aspect, but verifying the clock is present on the port expander will at minimum require a logic probe so you can verify there are pulses present on the TP pin. Though you'd need a scope or a logic analyzer to see exactly what those pulses are.


Again I thank you for taking the time to offer me a push in the right direction!

When I started this project, I had been making a list of things I'd need at some point, but these items seemed like very professional equipment.
Basically I didn't expect I'd ever need them, or need to learn how they work.

After reading your suggestion I have spent the night reading up on all kinds of analyzers etc., and I will follow your advice and buy some proper testing equipment.
I have been looking at Open Workbench Logic Sniffer and the Scanalogic-2 Logic Analyzer, reasonable choice or would you advise something else?




I also want to mention something else, because I have done another test after reading this post.

When I use Teensyduino beta 10 instead of the latest version I do get blinking lights on 23/24 (yay!), but the example seems to stop after 5 loops.
I added 2K pull-ups and things went from flakey to functional.
Can I (safely) try to reproduce these findings, or is there a chance of damaging my Teensy?
 
Well, I figured I should at least try and find out what happens, worst case scenario I'll just have to order more Teensy's, which I am planning on anyway.

I tried, and it works also on the latest beta!
Well, I tried with the 2 leds on 23/24, still have to test more to find out if it works as I need it to.

I will test more in the coming hours and report back here on my (amateur) findings.

edit: I am still planning on getting the test-equipment as soon as possible, suggestions are 'very' welcome ;)
 
Last edited:
Congrats on the progress, but even a $25 logic probe can be invaluable. Useful test equipment doesn't always have to be expensive. If you were here in the states i'd suggest just getting a cheap surplus tektronix scope from a local hamfest.. But i'm not really sure that'd be an option for you heh.

Also, why are you still running the beta when tensyduino 1.12 has been released?
 
Congrats on the progress, but even a $25 logic probe can be invaluable. Useful test equipment doesn't always have to be expensive. If you were here in the states i'd suggest just getting a cheap surplus tektronix scope from a local hamfest.. But i'm not really sure that'd be an option for you heh.

Also, why are you still running the beta when tensyduino 1.12 has been released?

Thats good to hear, while browsing online the ones I mentioned were already 'cheap' :)
I'm quite positive we have similar concepts around here in the Netherlands too, probably not as big events as in the states though (I wish...).

About the beta, that is my mistake, sry posted that at 1AM :)
I tried to use beta 10 to see if it would work, which it did, after that I went back to 1.12 with the added resistors.

Thanks for the support... it's because of posts like yours that I haven't been beat down by setbacks and misunderstandings (yet...) :)
 
After getting the lights to blink and reading up some more on how to send commands, I have started using the IO expander library to communicate with the expander.

I have been able to get my 2 LEDs to light up on all ports/pins, which was pretty basic and quite simple.
I can definately recommend the library for easy of use!
 
Hmmmm, after running my compass example for about 15 mins, the program hangs and needs to be rebooted.

After reading up some more, I noticed a post by Paul that mentions 1k pullups instead of 2k.
I made the change, and my compass example hasn't stopped working yet (2hrs runtime now).

I'll probably stop the test soon and try to stresstest it during the night and report back here.
Now to see if I can get my MCP23016 working with the compass module without crashing within +/- 1hr...
 
I think I have to withdraw my conclusions from the previous post and just wait until I can properly test instead of guesstimate.

The reason my Teensy hung after a certain amount of time is probably due to code.
It happens only when I use the io expander library to blink the two leds, if I use my own code it runs perfectly (it ran the entire night without a hitch).

The same appears to be the case with the compass module itself, though I haven't stresstested it like the 2 led example above.
I'll be away for an hour or two and I'll leave the compass example running in the meantime to see if it is still running when I get back.
 
Status
Not open for further replies.
Back
Top