Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Teensy 4.0 + mcp23017 blink

  1. #1

    Teensy 4.0 + mcp23017 blink

    Hi all,

    I'm trying to get my MCP23017 working with a teensy.
    I've hooked it up on a breadboard, the wire scanner finds it on 0x20, so I'm assuming all my wiring is correct.
    I see posts here and there where people put 2 4.7kO resistors between scl > teensy 19 and sda > teensy 18 but that didn't do anything for me.
    Also tried 3.3v and 5v.

    I wrote a very simple blink sketch to test it out. I haven't connected an LED, I'm just measuring voltage at the ports.
    It's always in a low range. First I tried just one port but I just put it all in a loop so I could test all the ports.

    Code:
    #include <Wire.h>
    #include "Adafruit_MCP23017.h"
    Adafruit_MCP23017 mcp;
    
    int led = 13;
    
    void setup() {
      while(!Serial);
    
      Serial.begin (9600);
      Serial.println("Start:");
    
      pinMode(led, OUTPUT);
      mcp.begin(0x20); 
      for(int i = 0; i < 16; i++) {
        mcp.pinMode(i, OUTPUT);
      }
    }
    
    void loop() {
      for(int i = 0; i < 16 i++) {
        mcp.digitalWrite(i, HIGH);
      }
      digitalWrite(led, HIGH);
      delay(1000);
    
      for(int i = 0; i < 16; i++) {
        mcp.digitalWrite(i, LOW);
      }
    
      digitalWrite(led, LOW);
      delay(1000);
    }
    I had it working before, including a button and rotary encoder but now I can't even get this to work.
    I tried another chip, same result (scanner finds it, ports do nothing)
    Did I fry both chips?

    Any ideas?

    Thanks a lot!
    Last edited by defragster; 12-05-2020 at 08:35 AM. Reason: pin# not GND p#2

  2. #2
    I can't find the edit button for this post
    But where I wrote scl > gnd and sda > gnd I meant scl > teensy 19 and sda > teensy 18.

    Also, here is a picture of my setup.

    Click image for larger version. 

Name:	mcp1.jpg 
Views:	21 
Size:	57.8 KB 
ID:	22737

    I also tried attaching an LED ofcourse and strangely enough I noticed it lighting up reaaaally lightly when I only connected the ground and nothing on the other leg.
    Is static electricity interfering with my mcp?
    The teensy is hooked to my macbook with one of those usb-c hubs. I unhooked my laptop from power and it did seem to change something in measured values on each port.

    The mcp reset pin gets a constant voltage it seems. Is that how it should work? Is something that looks constant on my multimeter constant enough or could it be resetting?

    Thanks again !

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,488
    Post #1 edited to : put 2 4.7kO resistors between scl > teensy 19 and sda > teensy 18

    Just checking - the added 4.7K resistors are from Pin to 3.3V as Pull Ups correct?

  4. #4
    I don't have them added at the moment, the same breadboard worked before. Now I don't have extra resistors anywhere.
    I don't know how they should work either, but when I tried I just placed them between sci and 19 and sda and 18.

    I don't fully understand pullup resistors but I guess I have to make a connection between sci => 19 and 19 => teensy 3.3v? Or 5v when I put everything on 5v?

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,488
    With a Teensy 4.0 / 4.1 not touching 5V at anytime is the best bet.

    But yes a [ Pullup resistor goes PIN# to 3.3V while that same PIN# also goes to the Device ], for both SCL and SDA. Only one set of pullups are needed on each i2c set of devices, some devices have them built in, but if not then adding the resistor is needed on Teensy. And for the 3.3V AFAIK using 2.2K is generally good for most speeds at 3.3V.

  6. #6
    Alas, no luck with added resistors and putting it on 3.3v.

    Click image for larger version. 

Name:	mcp2.jpg 
Views:	13 
Size:	158.2 KB 
ID:	22738
    Click image for larger version. 

Name:	mcp3.jpg 
Views:	13 
Size:	102.2 KB 
ID:	22739

    Sorry for the wire mess, I didn't have proper colored breadboard wires anymore.

    pin 18/19 go through 2.2k resistors to the same V as the rest. (Picture is still 4.7k but I just tried 2.2)
    Mcp is still found on 0x20 with the scanner example.

    I added an LED w resistor to MCP port GPB0, and am calling it with mcp.digitalWrite(8, HIGH); Tested the led on 3.3v, it works.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,488
    Finding it with the Scanner is a promising start ...

    No specific idea of the behavior of that MCP ... but one general thing to try is to delay before the device begin - and with the speed of the Teensy is might not hurt to try the following :

    Code:
    void setup() {
      Serial.begin (9600);  // This line of code actually does nothing - but if it did after the while() would not make sense
      while(!Serial);
    
      Serial.println("Start:");
    
      pinMode(led, OUTPUT);
      delay( 1100); // random delay - the Teensy may power up faster than the i2c device can handle
      mcp.begin(0x20); 
      for(int i = 0; i < 16; i++) {
        delay( 50 );  // Fast Teensy may be swamping the i2c device?
        mcp.pinMode(i, OUTPUT);
      }
    }

  8. #8
    Click image for larger version. 

Name:	mcp4.jpg 
Views:	17 
Size:	87.9 KB 
ID:	22741

    I simplified the setup a bit.
    The delay didn't really do anything I'm afraid..

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,488
    The picture does look easier to read - but wires aren't my specialty (shorter wouldn't hurt) to see what else my be present - others may see something.

    The delay before .begin was noted as a common 'guess' some devices that makes the difference.

    Knowing the device responds to Scanner suggests it is powered wired mostly correct - but AdaFruit ref or others may help if not.

    I'd confirm a touch of 3.3V on the expected (yellow ?) LED side makes it light up to prove breadboard is connecting and the polarity is right.

  10. #10
    Quote Originally Posted by defragster View Post
    The picture does look easier to read - but wires aren't my specialty (shorter wouldn't hurt) to see what else my be present - others may see something.

    The delay before .begin was noted as a common 'guess' some devices that makes the difference.

    Knowing the device responds to Scanner suggests it is powered wired mostly correct - but AdaFruit ref or others may help if not.

    I'd confirm a touch of 3.3V on the expected (yellow ?) LED side makes it light up to prove breadboard is connecting and the polarity is right.
    the led works, I think I'll try to use shorter lines.
    Thanks!

  11. #11
    Senior Member
    Join Date
    Nov 2015
    Location
    Cold hollow VT
    Posts
    183
    Is there a common ground connecting those seperate proto boards?

  12. #12
    Quote Originally Posted by grease_lighting View Post
    Is there a common ground connecting those seperate proto boards?
    All the wiring was correct, but thanks for checking it out!

    I have it working! I found a sketch using purely the wire library. I ran this and it works like a charm. Even without pullup resistors and on 5v.
    It appears I have problems with my teensyduino install, maybe because I also have platformio running and there are conflicting libraries or something..
    I still prefer to use the adafruit mcp23017 library but at least I know that it's a software problem.

    Code:
    #include "Wire.h"
    
    void setup(){
     
     //Send initial configuration to MCP23017
     Wire.begin();
     
     Wire.beginTransmission(0x20); //opcode/address of chip
     Wire.send(0x12); //iocon
     Wire.send(0x20); //disable sequential addresses
     Wire.endTransmission();
    
     Wire.beginTransmission(0x20);
     Wire.send(0x00); //iodira
     Wire.send(0x00); //all 0's, all outputs
     Wire.send(0x00);
     Wire.endTransmission();
    
    }
    
    void turnOnLED()
    {
    
     Wire.beginTransmission(0x20);
     Wire.send(0x12); // gpioa
     Wire.send(0xff);  // all high
     Wire.send(0xff);
     Wire.endTransmission();
     
    }
    
    void turnOffLED()
    {
    
     Wire.beginTransmission(0x20);
     Wire.send(0x12); // gpioa
     Wire.send(0x00); // all low
     Wire.send(0x00);
     Wire.endTransmission();
     
    }
    
    void loop(){
    
     turnOnLED();
     digitalWrite(13, HIGH);
     delay(100);
     turnOffLED();
     digitalWrite(13, LOW);
     delay(100);
     
    }

  13. #13
    Ok wow. I've got it working with the adafruit library.

    it works when I do mcp.begin() instead of mcp.begin(0x20). The address should be an int instead of a hexadecial number!
    Anything > 7 is set to 7. So, instead of using the i2c address you have to use the 3 bit mcp address, 0 - 7.

    Only took me about 3 evenings pffff.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •