Potentiometer to scroll through values.

Hey guys,
So I was making a clock using the arduino uno and the DS 3231 RTC chip, I wanted to set the time using potentiometers to scroll through the hours(0-23) and minutes (0-59). But first I wanted to get this whole scrolling part done. But I have a problem constraining the values to show the full range of values. When I turn the knob dial to one end it doesn't show 0, but when i turn the knob closer to the middle it goes to 0, then back up again as i dial to the other side...weird huh?

I'm new to this so any help would be appreciated. This is a little snippet of the code I used, I printed to the lcd and serial monitor.

////// SETTING TIME ////

// Setting hour
const int hourpin = A1;
int hourvalue = 0;
int hourmin = 1023;
int hourmax = 0;

// Setting minutes
const int minutepin = A0;
int minutevalue = 0;
int minutemin = 1023;
int minutemax = 0;



void setup() { 
  lcd.begin(16, 2);
  Wire.begin();
  Serial.begin(9600);
  pinMode(9, OUTPUT); 
  analogWrite(9, 150);


  // calibrate during the first five seconds 
   
  while (millis() < 7000) {
    hourvalue = analogRead(hourpin);
    minutevalue = analogRead(minutepin);
    
    // record the maximum sensor value for hours
    if (hourvalue > hourmax) {
      hourmax = hourvalue;
    }
    // record the minimum sensor value for hours
    if (hourvalue < hourmin) {
      hourmin = hourvalue;
    }
   // record the maximum sensor value for minutes
   if (minutevalue > minutemax){
    minutemax = minutevalue;
   }
   // record the minimum sensor value for minutes
   if (minutevalue < minutemin) {
    minutemin = minutevalue;
   }
  }
  

}

void loop() {
 

 // read the sensor:
  hourvalue = analogRead(hourpin);
  minutevalue = analogRead(minutepin);
  
  // apply the calibration to the sensor reading
  hourvalue = map(hourvalue, hourmin, hourmax, 0, 23);
  minutevalue = map(minutevalue,minutemin,minutemax,0,59);
  
  // in case the sensor value is outside the range seen during calibration
  hourvalue = constrain(hourvalue, 0, 23);
  minutevalue = constrain(minutevalue, 0, 59);

  Serial.print("hour = ");
  Serial.print(hourvalue);
  lcd.setCursor(6,0);
  lcd.print(hourvalue);
  lcd.print("  ");
  delay(2000);
  Serial.print('\n');
  Serial.print("minute = ");
  Serial.print(minutevalue);
  lcd.setCursor(12,0);
  lcd.print(minutevalue);
  lcd.print("  ");
  Serial.print('\n');
  delay(1000);
}

You may have a "noisy pot". The track may not be smooth and regular. Try a different pot.

Weedpharma

weedpharma:
You may have a "noisy pot". The track may not be smooth and regular. Try a different pot.

Weedpharma

Is there anything I may add to my circuit to make it smooth?

How did you connect the pot.

Two outside pins to +5 and ground, and center pin to analogue in?

Pots are somewhat unreliable with DC.
A 100n capacitor from analogue in to ground could help.

Maybe you should look at rotary encoders.
Leo..

Wawa:
How did you connect the pot.

Two outside pins to +5 and ground, and center pin to analogue in?

Pots are somewhat unreliable with DC.
A 100n capacitor from analogue in to ground could help.

Maybe you should look at rotary encoders.
Leo..

Yup I connected it as you said, can you explain a little on how you picked that value for the capacitor and how it helps, I'd like to learn. Thank you!

A pot is basically a nude resistor with a metal wiper sliding over the body.
Over time, the wiper will oxidise and the track will get dirty.
When you turn the knob, the dirty contact bounces over the dirty track, and can temporarilly loose contact.
You might have heard that when you turn the volume knob of an amplifier (crackle).
Basically a floating analogue pin during contact bounce.
A cap could fix that by holding the voltage steady while the pot looses contact.
100n is a standard value. Bigger delays things, smaller won't work.
Leo..

If you do not have success with the capacitor, an old trick from my distant past is to give it a SHORT squirt of WD40 or similar. This is of course assuming it is an open frame pot.

Best idea is to try a different pot.

Weedpharma

I don't believe the issue described is caused by a noisy potentiometer.

It should not be necessary to go through the "calibration" procedure, whatever that is supposed to do.

Using the Arduino software, temporarily use 'File' > 'Examples' > 'Basics' > 'AnalogReadSerial' to check the 'minutes' potentiometer is wired into the circuit correctly. Edit the code so it reads input A1 to check the 'hours' potentiometer. You should find the readings go from 0 to 1023 as you turn the potentiometer.

What is the resistance of your pot?

10k or less probably works best.

...R

Robin2:
What is the resistance of your pot?

10k or less probably works best.

...R

I found two 500K pots I had so I started using them :confused: , I think I have one 10k laying around.

Lineair pots ofcourse.
A log pot (A) won't work with 60 levels.
Maybe a log pot was the initial problem.
Leo..

Wawa:
Lineair pots ofcourse.
A log pot (A) won't work with 60 levels.
Maybe a log pot was the initial problem.
Leo..

If you mean log pots as the rotary ones, yes that's what I use. I apologize for being a bit clueless I'm still learning as I go. Why can't a log pot work with 60 levels, don't we use them to tune from a range of 0-255 for other applications? Or am I thinking something completely?

A lineair pot has the resistance evenly distributed, so halfway is half the resistance.
With a log pot (mainly used for audio), halfway is ~10% of the resistance.
One side of the pot will have slow changing values, while the other side of the pot has fast changing values.
You might have trouble selecting values in the fast changing part of the pot.
1kA = log, 10kB = lin.
Leo..

Wawa:
A lineair pot has the resistance evenly distributed, so halfway is half the resistance.
With a log pot (mainly used for audio), halfway is ~10% of the resistance.
One side of the pot will have slow changing values, while the other side of the pot has fast changing values.
You might have trouble selecting values in the fast changing part of the pot.
1kA = log, 10kB = lin.
Leo..

Ahh I see, not sure what you mean by 1Ka = log and 10kb = lin though

"A" stands for "audio taper", log.
"B" for lineair.
"C" for reverse log
Look at the markings on the pot.

You need a lineair pot. 10kB.

But it might be better for your project to use a rotary encoder.
Maybe with push button to select hours/minutes.

Leo..

Wawa:
"A" stands for "audio taper", log.
"B" for lineair.
"C" for reverse log
Look at the markings on the pot.

You need a lineair pot. 10kB.

Potentiometer - Wikipedia

But it might be better for your project to use a rotary encoder.
Maybe with push button to select hours/minutes.
Rotary Encoder - COM-09117 - SparkFun Electronics
Leo..

Now I get it, thank you! My pot shows B500K, guess there may be something else wrong. I'm planning on using a push button two switch between alarm/clock mode, with two pots adjusting hours and time.

The R of the pot is very high.

Try something around 10k. (5k to 47k).

Weedpharma

In the original post: ". . . . when I turn the knob closer to the middle it goes to 0". A logarithmic potentiometer would not cause that.

Something else is wrong.

Wawa:
"A" stands for "audio taper", log.
"B" for lineair.
"C" for reverse log
Look at the markings on the pot.

You need a lineair pot. 10kB.

Potentiometer - Wikipedia

But it might be better for your project to use a rotary encoder.
Maybe with push button to select hours/minutes.
Rotary Encoder - COM-09117 - SparkFun Electronics
Leo..

Thank you, I seemed to have sort of fixed the problem but I will apply your advice if the problem returns!

Please let us know how you fixed the problem. It may help others.

Weedpharma