0

I have an MCP4151 digital potentiometer chip. I have managed to get it to work using and Arduino Uno using the code below:

#include <SPI.h> byte address = 0x00; int CS= 5; void setup() { pinMode (CS, OUTPUT); SPI.begin(); } void loop() { for (int i = 0; i <= 128; i++) { digitalPotWrite(i); delay(10); } delay(500); for (int i = 128; i >= 0; i--) { digitalPotWrite(i); delay(10); } } int digitalPotWrite(int value) { digitalWrite(CS, LOW); SPI.transfer(address); SPI.transfer(value); digitalWrite(CS, HIGH); } 

But when I try to get it to work using an ESP32 programmed using Arduino language it doesn't. I have connected like that

  • SCK -> 18
  • MOSI -> 23
  • SS -> 5

I also used the potentiometer inside like a voltage divider for test purposes. GND on one leg, 3.3v on the other and an LED on the wiper. Are there other settings that I have to implement in order the ESP32 SPI to work like an Arduino SPI? Did I miss something?

1
  • If I am using the SPI defaults (Serial.begin()) and print the variables on the serial monitor Serial.println(MOSI); Serial.println(SCK); Serial.println(SS); I got these defaults MOSI 23, SCLK 18 and SS 5 (VSPI). Commented Jan 21, 2019 at 15:56

3 Answers 3

2

I managed to get it to work in a strange way. I opened the example included in the ESP32 SPI Library and copied some points from there.

I first initialise a pointer to the SPI class, then I call begin() at that pointer and inside the digitalPotWrite function I call beginTransaction(10000,MSBFIRST,SPI_MODE0) and end the transaction before digitalPotWrite finishes. Full code here:

#include <SPI.h> static const int spiClk = 1000000; byte address = 0x00; SPIClass * hspi = NULL; void setup() { pinMode(15, OUTPUT); hspi = new SPIClass(HSPI); hspi->begin(); } void loop() { for (int i = 0; i <= 128; i++) { digitalPotWrite(i); delay(10); } delay(500); for (int i = 128; i >= 0; i--) { digitalPotWrite(i); delay(10); } } int digitalPotWrite(int value) { hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0)); digitalWrite(15, LOW); hspi->transfer(address); hspi->transfer(value); digitalWrite(15, HIGH); hspi->endTransaction(); } 
0

The ESP32 SPI library has defaults for the HSPI pins (14, 12, 13, 15). For VSPI pins, you must specify the pins in begin().

SPI.begin(18, 19, 23, 5); 
1
  • I both tried to use the HSPI pins with SPI.begin(); and those pins using SPI.begin(18, 19, 23, 5); but nothing worked. Maybe ESP32 used an incompatible frequency for SPI? Commented Sep 2, 2018 at 16:54
-1

Change the int for a void in the function... That's it. When you call that function is never going back, still waiting for a return.

int digitalPotWrite(int value) { digitalWrite(CS, LOW); SPI.transfer(address); SPI.transfer(value); digitalWrite(CS, HIGH); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.