0

Connection

I want to use NodeMCU v3 with a MCP23017 that has a MAX6675 at the end. Everything is in the circuit; please see the image.

The MAX6675 (U3) doesn't want to work with the MCP23017.

  1. First MAX6675 (U4) works perfectly fine using MAX6675 library

      MAX6675 max(D5, D7, D6);
      max.readCelsius(); //gives proper temperature
    
  2. For MCP23017 I'm using the Adafruit_MCP23017 library

  3. I have tested the MCP23017 with simple code. The LED is blinking so I'm assuming that it is not broken.

     Adafruit_MCP23017 mcp;
     mcp.begin(0);
     mcp.pinMode(0, OUTPUT);
     mcp.digitalWrite(0, HIGH); //LED lights up
     delay(1000); 
     mcp.digitalWrite(0, LOW); //LED turns off
     delay(1000);
    

I slightly modified the MAX6675 lib to use adafruit_MCP23017 to digital Read/Write; here is my Github repo.

I'm trying to read the MAX6675 (U3) connected through the MCP23017 (U2) using this code:

  Adafruit_MCP23017 mcp;
  mcp.begin(0);
  MAX6675_MCP max_mcp(mcp, 1, 2, 3);  
  Serial.print(max_mcp.readCelsius());  //displays 0 or NAN

Assuming:

  1. U4 / U3 works connected straight to NodeMcu

  2. MCP23017 works because of flashing LED

My question is: Why doesn't the MAX6675 (U3) want to work connected through the MCP23017 (U2)?

Where did I make a mistake?

ocrdu
  • 8,705
  • 21
  • 30
  • 42
Sahee
  • 101
  • 2
  • This is probably not the cause if the problem you are seeing, but I don't see the pull-up resistors on the I2C lines. See also [this](https://electronics.stackexchange.com/q/102611/7036). – Nick Alexeev Aug 09 '18 at 17:25
  • Thanks, for your comment. I will add those resistors. – Sahee Aug 09 '18 at 17:40
  • You realize that you are doing SPI bit-banging through I2C port expander, meaning you have to send _4 complete I2C commands for every SPI bit?!_ And that is for SPI bus that has been designed to support multiple devices already, at speeds faster than I2C. Does not make any sense. – Maple Aug 09 '18 at 18:14
  • Yeah, I'm sure the problem lies with modifying the MAX6675 lib to work with the MCP23017 because one is SPI and the latter is I2C. Just do it right and get matching interfaces, and you won't have problems like this. – DigitalNinja Aug 09 '18 at 18:19
  • At the first glance the modification of MAX6675 lib looks OK. However you did change all _delay_ms() calls to delay(). What was the reason for that? For debugging I'd suggest checking if you have 16 clock pulses on GPA1 when you call readCelsius – Maple Aug 09 '18 at 18:46
  • I'd like to second @Maple questioning your approach. What's your rationale for tunneling SPI through I2C? – Nick Alexeev Aug 09 '18 at 19:02
  • Oh guys, I made very stupid mistake. Last time i was trying to connect two spi on one wire, instead of CS I used SO. That was my mistake... Thank you. Its working right now without extender ;) – Sahee Aug 09 '18 at 22:12

1 Answers1

0

Where I made mistake?

I could not find any mistakes in your port of MAX6675 library to MCP23017 channel, except for questionable replacement of _delay_ms() calls.

The missing pull-ups on the I2C lines could affect the operation of MCP23017 in unpredictable way. I'd expect it not be functional at all, but it might be possible that communication works one way only, i.e. NodeMCU is able to send commands to MCP (so the LED blinking test works) but cannot read slave responses properly (so the data from MAX gets corrupted).

Where you really made a mistake, is in trying to bit-bang SPI using I2C port extender. My original estimate was incorrect. After analyzing MCP23017 library I see that it takes 8(!) I2C commands to send 1 bit. i.e. your communication is 32 times slower than it could be.

If you need more than one SPI device you can put them all on same CLK/MISO pins and use MCP23017 to switch /CS pins only. Or, even better, use SPI multiplexor (like ADG731) on the same bus to switch /CS pins.

Maple
  • 11,755
  • 2
  • 20
  • 56
  • Thanks for yor comment, I connected this without extender like [here](https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino). And its working. My mistake – Sahee Aug 09 '18 at 22:15