nwb99 / MCP4151

MIT License
4 stars 1 forks source link

Software pull-up and hard-ware pull down? #3

Open lvdh-github opened 7 months ago

lvdh-github commented 7 months ago

Hello, I love the simplicity of your include. Looking at your README.MD and your code, I find the following a bit confusing with regard to the MISO pin.

On the one hand, you advise: 'On the MCP4151, the MISO and MOSI are on the same pin. Use a 1 kOhm resistor to pull down the MISO pin on the chip to GND, with the MOSI line (on the MOSI pin of the Arduino) in series with the resistor."

On the other hand, in your code you tell the Arduino to pull-up that same pin via pinMode(MISOpin, INPUT_PULLUP)

So at the same time, we seem to pull-up AND pull-down the MISO pin. I can imagine you use a software INPUT_PULLDOWN on some pins.

I am still a layman, but maybe you can provide some clarification?

Best regards, Lucien

nwb99 commented 7 months ago

Sorry for the late reply-- I honestly don't remember why I typed it like that. I likely just made a typo. Page 41 in the documentation for this chip (and similar) shows connecting them through the same pin on the chip. It's been a while since I worked on the project I was using the MCP4151 for (it's on my to do list but way at the bottom: day-job, real life issues, etc.).

I believe

Here's the docs: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22060b.pdf

Also this link may provide a better explanation than what I can: https://electronics.stackexchange.com/questions/240852/mcp4131-how-to-know-what-value-resistors-to-use-on-the-multiplexed-spi-io

lvdh-github commented 7 months ago

Hey Nathan, thanks so much for your reply. Very helpful. I have got things working now with an ESP8266.   Had to mod your lib a tiny bit though to be able to set pot values.Thanks again Met vriendelijke groeten,Kind regards,Mit freundlichen Grüßen,Lucien van der Hoeven0031 6 5066 8010Op 29 feb. 2024 om 19:11 heeft Nathan Barnett @.***> het volgende geschreven: Sorry for the late reply-- I honestly don't remember why I typed it like that. I likely just made a typo. Page 41 in the documentation for this chip (and similar) shows connecting them through the same pin on the chip. It's been a while since I worked on the project I was using the MCP4151 for (it's on my to do list but way at the bottom: day-job, real life issues, etc.). I believe Here's the docs: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22060b.pdf Also this link may provide a better explanation than what I can: https://electronics.stackexchange.com/questions/240852/mcp4131-how-to-know-what-value-resistors-to-use-on-the-multiplexed-spi-io

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

MetalMan1 commented 4 months ago

@lvdh-github any chance you can share what you did to get things working which are different from what's in @nwb99 's code and readme? Unfortunately I'm not having any luck with it.

lvdh-github commented 4 months ago

Hey, no problem. I have a small app running to simulate a resistor that registers the temperature for my heat pump. EPS8266 micro board

Below are my pin definitions and the pot layout as used in the sketch.


/*
    Pin setup D1 mini ESP8266 NodeMcu - use logical function names vs pinnames via below definition, connecting GPIO numbers to names
    For the D1 mini board, we have built-in pullup or pulldown if needed, to avoid electromagnetic interference
      All pins 0 to 15 can be set to INPUT_PULLUP if needed
      D0 pin 16 can be set to INPUT_PULLDOWN if needed, but it has no SPI MISO functionality
      A 1KΩ resistor can be used as an external pull-up (to 5V) or pull-down (to ground) means
*/
#define SCK 14   // D1 mini D5 pin 14 (GPIO14) Clock  --> MCP4151 SCK     = pin 2
#define MISO 12  // D1 mini D6 pin 12 (GPIO12) Input  --> MCP4151 SDI/SDO = pin 3 - pulled down with 1kΩ to GROUND
#define MOSI 13  // D1 mini D7 pin 13 (GPIO13) Output --> MCP4151 SDI/SDO = pin 3
#define CS 15    // D1 mini D8 pin 15 (GPIO15) Select --> MCP4151 CS      = pin 1

/*
    MCP4151 pot layout:
            ------------
         CS | 1 o    8 | VDD
        SCK | 2      7 | P0B
    SDI/SDO | 3      6 | P0W
         CS | 4      5 | P0A
            ------------
*/

Include the SPI lib in your list of includes
#include <SPI.h>                // to communicate with the potmeter via SPI protocol

The code for controlling the pot is pretty straight forward.  I use pin 15 for chip select

// for moving the pot meter wiper position
const int csPin = 15;  // Chip Select pin for MCP4151

In my INIT section:
  pinMode(LED_BUILTIN, OUTPUT);  // initialize pin LED_BUILTIN as an output.
  pinMode(csPin, OUTPUT);        // Chip Select pin to output
  digitalWrite(csPin, HIGH);     // Ensure MCP4151 is deactivated at start

/* =========================================================================================================
  Set to pot wiper to the desired position
*/
void setPot(int value) {
  value = constrain(value, 0, potMaxPos);  // Ensure the value is within 0-255
  SPI.begin();
  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0));  // Begin SPI transaction at 5 MHz, SPI mode 0
  digitalWrite(csPin, LOW);                                         // Activate MCP4151
  SPI.transfer(0x00);                                               // Send command byte for wiper 0 (adjust if necessary)
  SPI.transfer(value);                                              // Send data byte
  digitalWrite(csPin, HIGH);                                        // Deactivate MCP4151
  SPI.endTransaction();
  SPI.end();
}

So in practice, I only WRITE to the pot, any value between 0 and 255

As for the connections:
on the ESP8266 side:
I added a 1kOhm to my ESP8266 D6 pin and ground to pull down the pin.
I also added 2 capacitators between ground and 5V of the ESP8266 to eliminate electrical noise when not on battery power
0,68 nF and 10mF

on the MCP side:
Use either pin 5 and 6 to get an R or use pin 6 and 7 to get a reversed R

Let me know if this helps and just ask if you have any questions
lvdh-github commented 4 months ago

P.s. make sure you provide power to the pot as well. Not in my code, but without power, the MCP will do nothing ;-) Ground connects to pin 4 VSS (and 5 in my case) of the MCP 5V connects to pin 8 VDD of the MCP