Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32-S3-Mini and MCP3462R not working in Mux Mode #13

Open
LeKrakenTheCode opened this issue Jan 24, 2024 · 24 comments
Open

ESP32-S3-Mini and MCP3462R not working in Mux Mode #13

LeKrakenTheCode opened this issue Jan 24, 2024 · 24 comments
Assignees

Comments

@LeKrakenTheCode
Copy link

Keep receiving a value of zero from analogRead. Moving this into its own issue from: #5 (comment)

@LeKrakenTheCode
Copy link
Author

LeKrakenTheCode commented Jan 24, 2024

What's the speed of your SPI bus? I remember having some issues if the speed was over 500k. I'm currently running w/ 200k\

I was running at 10MHz. I lowered to 200KHz and still no change.

@LeKrakenTheCode LeKrakenTheCode changed the title ESP32 and MCP3462 not working in Mux Mode ESP32-S3-Mini and MCP3462 not working in Mux Mode Jan 24, 2024
@LeKrakenTheCode LeKrakenTheCode changed the title ESP32-S3-Mini and MCP3462 not working in Mux Mode ESP32-S3-Mini and MCP3462R not working in Mux Mode Jan 24, 2024
@Mirageofmage
Copy link

If you have access to a scope, can you check the activity on MISO?

@LeKrakenTheCode
Copy link
Author

Sorry for the delay. Was feeling rather unwell this weekend. Working on it now.

@LeKrakenTheCode
Copy link
Author

LeKrakenTheCode commented Jan 31, 2024

If you have access to a scope, can you check the activity on MISO?

image
image

I have this tool that can view SPI. Does this help? Please ignore the CS line, it is not connected as i didn't break out a header for that pin, however the analyzer is set to ignore the CS line for now.

@nerdyscout
Copy link
Owner

nerdyscout commented Feb 3, 2024

MISO and MOSI seem to show reasonable signals. Seems like the second picture is showing a 1Byte command followed by another 2Byte command... I did not try to figure out which are these. But it seems save to say MCP3x6x::read(Adcdata *data) is never executed as this would result in a 3-5Byte command. At least this is not shown in any of these pictures.

I still suspect the issue in this little snippet:

    settings.mux = ch;
    _status      = write(settings.mux);
    _status      = conversion();
    while (!_status.dr) {
      _status = read(&adcdata);
    }

please put an Serial.print(_status.dr) inside the loop and right in front of it, it looks like this is currently not executed. This bit should be set when conversion started and reset when valid data available. As this bit is part of the status byte endianness is important, but also timing of spi speed vs conversion speed might cause it.

@nerdyscout nerdyscout self-assigned this Feb 3, 2024
@nerdyscout nerdyscout reopened this Feb 3, 2024
@LeKrakenTheCode
Copy link
Author

image
Here's the change and here is the output:
image
only one appears to be running.

@Mirageofmage
Copy link

image Here's the change and here is the output: image only one appears to be running.

I've had issues with the while loop, in my implementation I just directly read instead of waiting for the data ready bit. I have to be careful with my sample rate or it returns the same value for each of the channels.

@LeKrakenTheCode
Copy link
Author

image

No change to the output.

@Mirageofmage
Copy link

image

@LeKrakenTheCode
Copy link
Author

LeKrakenTheCode commented Feb 5, 2024

still no change to the output:

image

Trying to read MCP_TEMP.

@Mirageofmage
Copy link

@nerdyscout
image

Question on why the int32 is truncated to 25 bits in adcdata? The ADC can return either left aligned or right aligned 32 bit values. The assignment in the result struct retains all 32 bits

@nerdyscout
Copy link
Owner

Question on why the int32 is truncated to 25 bits in adcdata? The ADC can return either left aligned or right aligned 32 bit values. The assignment in the result struct retains all 32 bits

yes, the dataformat is choosen within the begin() funtion

@Mirageofmage
Copy link

Question on why the int32 is truncated to 25 bits in adcdata? The ADC can return either left aligned or right aligned 32 bit values. The assignment in the result struct retains all 32 bits

yes, the dataformat is choosen within the begin() funtion

I see that, but my point is that if somebody chooses a left aligned output, then the 7 leftmost bits get masked out? Or am I understanding it wrong?

@nerdyscout
Copy link
Owner

nerdyscout commented Feb 5, 2024

I assume you are right.
My intention was to make this library for getting easy started as the component is really complex. This is why the begin functions sets a few default parameters... they might be not good for every case, but if someone chooses to set up their own config than I would suspect it is on him self to handle this. This is also why I refactored settings to be a class by its own so in future it could be a parameter of begin().

@LeKrakenTheCode
Copy link
Author

Any other idea's I can try?

@Mirageofmage
Copy link

Any other idea's I can try?

Maybe try returning adcdata.value directly at the end of analogRead?

So
return adcdata.value;
instead of
return result.raw[(...

Otherwise I don't really have a clue/it has to be in hardware? I've been testing the same code revisions on my hardware and it's been working (though I'm not using the R revision).

I remember that you put the pins in the constructor of whichever MCP3x6x class you used, I instead opted to redefine the consts in the default constructor.

So my object declaration is just
MCP3561 mcp;

with #defines for MOSI, MISO, SS, SCK, and IRQ (if required). The default SPI interface through Arduino is selected.

@Mirageofmage
Copy link

I actually think you HAVE to use #define to select pins instead of using the constructor, it doesn't look like parameters are properly passed through the constructor to the SPI object

The parameters required are

#define MOSI 
#define MISO
#define SS
#define SCK

@nerdyscout
Copy link
Owner

I actually think you HAVE to use #define to select pins instead of using the constructor, it doesn't look like parameters are properly passed through the constructor to the SPI object

The parameters required are

#define MOSI 
#define MISO
#define SS
#define SCK

as his logic analyzer pictures have proven some SPI communication I don't see why this should be the case.

@Mirageofmage
Copy link

Mirageofmage commented Feb 12, 2024 via email

@nerdyscout
Copy link
Owner

@LeKrakenTheCode please publish your code. either setup a new repository as minimal example which reproduces your issue or the original code.

@LeKrakenTheCode
Copy link
Author

LeKrakenTheCode commented Feb 12, 2024 via email

@LeKrakenTheCode
Copy link
Author

LeKrakenTheCode commented Feb 12, 2024 via email

@nerdyscout
Copy link
Owner

seems like it could be fixed by something like this, will work on a better fix later.

MCP3x6x::status_t MCP3x6x::_transfer(uint8_t *data, uint8_t addr, size_t size) {
  _spi->beginTransaction(SPISettings(MCP3x6x_SPI_SPEED, MCP3x6x_SPI_ORDER, MCP3x6x_SPI_MODE));
  noInterrupts();
  digitalWrite(_pinCS, LOW);
  _status.raw = _spi->transfer(addr);
#ifdef ARDUINO_ARCH_ESP32
  _spi->writeBytes(data, size);
#else
  _spi->transfer(data, size);
#endif
  digitalWrite(_pinCS, HIGH);
  interrupts();
  _spi->endTransaction();

  return _status;
}

@McMillanFlowProducts
Copy link

McMillanFlowProducts commented Feb 21, 2024

Just changed the _transfer function, still getting a reply of zero. I am LeKrakenTheCode, I just ended up creating the issue under the wrong account and never changed it.

*** I have removed a println from the analogread function, i am now getting a constant value of 130 (raw adc value) regardless of what i am reading in the mux. i have updated code on github.
image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants