Controling a DS1804 Digital Potentiometer

Hi to all. Can anyone provide me with a simple routine to start controlling a DS1804 to slowly turn an LED on and off?
I get the error: "[DS1804] Wiper is at position 0. Additional clock pulses are ignored"

I use the tom-dudman library. Thank you.
Thank you.

Please post your sketch, using code tags when you do

My code is very ugly, sorry ...

#include <DS1804.h>

const byte wiperPin = A5;	
// DS1804( byte CSpin, byte INCpin, byte UDpin, unsigned long maxResistance );
DS1804 digipot = DS1804( 7, 5, 6, DS1804_FIFTY); //DS1804 digipot = DS1804( 6, 5, 4, DS1804_TEN );
	
byte reading = 0;
unsigned long new_resistance = 1;
	
void setup() {
  Serial.begin(9600);
  Serial.println("Start ...");
  pinMode (7, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  digipot.setToZero();
  digitalWrite(7, HIGH); //CS1 disable
  delay(100);
  digitalWrite(5, HIGH); // INC increment HIGH
  delay(100);
  digitalWrite(6, HIGH); // U/D increment
  delay(100);
  
  digipot.setResistance( new_resistance );
}
	
void loop() {

for(int i = 0; i<50; i++){
    digitalWrite(7, LOW); // CS1 enable
    delay(500);
	digitalWrite(6, HIGH); // U/D increment UP
	delay(500);
	digitalWrite(5, HIGH); // INC increment HIGH
	delay(100);
	digitalWrite(5, LOW); // INC increment LOW
	delay(100);
    digitalWrite(7, LOW); // CS1 disable
    delay(5000);  
  }

What do you mean by that? You're not thinking of using it as a variable series resistor for the LED, are you?

Digital pots are not (generally) designed for power control - the DS1804 has a maximum current of only 1mA.

The usual approach is to use PWM...

So it thinks you're trying to turn it down further when it's already at minimum.

How does your code ensure that you don't try to keep turning down below minimum, or up above maximum?

No, the idea is not to use the DS1840 to control an LED. I am using a simulator that can do it, it is just a scheme to know if it works or not. A simple UP and DOWN control sketch. It would be enough for me to get out of the quagmire.

Some clarification is needed..!
1840 & 1804. more haste, less speed.

Maybe start again?

Sorry, is a DS1804 a Maxim Dig Pot.

Did you look at any of the examples that came with the library?

There is no need for you to be manipulating the pins the digital potentiometer is connected to, that's the hole point of using a library.

You didn't use any of the functions that take care of this.

Read this example that the author provides. Maybe even just try to get it working for yourself:

#include <DS1804.h>
	
const byte wiperPin = A5;
DS1804 digipot = DS1804( 6, 5, 4, DS1804_TEN );
	
byte reading = 0;
unsigned long new_resistance = 0;
	
void setup() {
  Serial.begin(9600);
  digipot.setToZero();
}
	
void loop() {
  if ( Serial.available() > 0 ) {
    new_resistance = Serial.parseFloat();
    digipot.setResistance( new_resistance );
    delay(1);	// give DS1804 time to change, and for analogue reading to settle
    reading = analogRead( wiperPin );
    String wiperPositionText = "DS1804 Wiper at ";
    Serial.println( wiperPositionText + digipot.getWiperPosition() );
    String resistanceText = "DS1804 Resistance set to ";
    Serial.println( resistanceText + digipot.getResistance() + " ohms (requested " + new_resistance + " ohms)" );
    String wiperPinVoltageText = "Wiper pin voltage: ";
    Serial.println( wiperPinVoltageText + 4.9*reading + " mV" );
  }
}

See calls to methods (functions) like setResistance() and getResistance().

You don't even need to set the pin modes.

The library is your friend; learn how to exploit it.

HTH

a7

are no more - now Analog Devices:

Addendum

Since Aug 2021

Sorry, I'm a very bad programmer. Although at the end of the design I will have to use the serial, what I really want is to see the DS1804 increase/decrement. I had already tried this sketch, I tried it again but I can't see it working in the simulator.
At the risk of sounding like an abuser, could you correct the loop so that it simply raises or lowers the wiper value?

I got it!, thanks alto777 and other kind people.

Post a schematic.

DID YOU UNDERSTAND THIS STATEMENT FROM @awneil ???????

An LED may require 5-10mA. Do you see the problem?

@gfvalvo - did you see OP's reply in post #6 ?

Nice. Please post the sketch where you realized that you got it.

a7

Yes, but I didn't understand anything. I have corrected the alto777 example (from the library) and I have already gotten the DS1804 to work in the simulator. Again, thank you for your time and attention.

Well now I must ask again, if you aren't just now actually posting your current working Aha! sketch, to do so.

I am curious about the nature of the correction(s).

TIA

a7

It took me a little while to understand that I had to replace i++ with i = i+100 to see that everything was going well. Houses of age, I'm going to be 75 years old...

#include <DS1804.h>
	
unsigned long wiperPin = A5; //const byte wiperPin = A5;
//DS1804( byte CSpin, byte INCpin, byte UDpin, unsigned long maxResistance );
DS1804 digipot = DS1804( 7, 5, 6, DS1804_FIFTY );
	
byte reading = 0;
unsigned long new_resistance = 0;
	
void setup() {
  Serial.begin(9600);
  Serial.println("Start ...");
  digipot.setToZero();
}
	
void loop() {
  //if ( Serial.available() > 0 ) {
  for(unsigned long i = 0; i < 50000; i= i+100) {
    new_resistance = i; //new_resistance = Serial.parseFloat();
    digipot.setResistance(new_resistance);
    delay(100);	// give DS1804 time to change, and for analogue reading to settle
    reading = analogRead(wiperPin);
	delay(100);	// give DS1804 time to change, and for analogue reading to settle
    String wiperPositionText = "DS1804 Wiper at ";
    Serial.println( wiperPositionText + digipot.getWiperPosition() );
    String resistanceText = "DS1804 Resistance set to ";
    Serial.println( resistanceText + digipot.getResistance() + " ohms (requested " + new_resistance + " ohms)" );
    String wiperPinVoltageText = "Wiper pin voltage: ";
    Serial.println( wiperPinVoltageText + 4.9*reading + " mV" );
	}
  //}
}

Many thanks for everything

Just reread it. I missed the "not" the first time through:

Still, pretty odd given @Breixo's post #1:

Seems like the design objective keeps shifting.

Maybe. @Breixo was using a simulator, where some things can work that don't IRL, and she is (now if not always) aware of that.

So we got shifted once, or more fully informed, soon enough.

@Breixo THX for the code.


Me too, if my luck holds out. Not for awhile, though. :wink:

BTW

    i = i + 100;

does the trick, but the cool kids typically use a language feature, an assignment operator viz:

    i += 100;

Seen above as statements, but you can do the same thing in the expressions in the for loop.

Not just for brevity.

At a certain point, no writer of code does it the first way, so you'll have to know about it anyway if you are reading code.

a7