Author Topic: Arduino question: unstable signal  (Read 1615 times)

0 Members and 1 Guest are viewing this topic.

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Arduino question: unstable signal
« on: March 17, 2019, 04:04:59 pm »
I have built the circuit shown in the attached diagram. An if statement tells the LED to come on when the ADC number drops below a certain number. It works. The LED will eventually be replaced with relays controlling a network of fans and Peltier cooling elements for a wine cooler. My problem is that the LED (or relay) flashes off and on when approaching the desired number, then it finally becomes completely on or completely off. I've read about some complicated methods for using 3.3v, but they involve inner workings that I simply don't understand yet. I am using the smoothing method, but seems of very little help if any. Is there a simpler way? I was hoping maybe a well placed cap or something like that might work. Any help will be appreciated.

PS: Using Uno V3
 

Offline WildMOSFET

  • Contributor
  • Posts: 21
  • Country: it
Re: Arduino question: unstable signal
« Reply #1 on: March 17, 2019, 04:14:48 pm »
Add a ten microfarad electrolytic capacitor in parallel with the resistance.
And take an average of the readings.
For example, take ten readings with a for cycle, add them all up and divide them by 10.
For each reading, you need to add a small delay, for example 10 milliseconds.


I have an example code, it is in Italian but I believe that the logic is understood the same.
Code: [Select]
float LetturaMediaTensione(int n_letture)
{
  unsigned int valore = 0;
  float tensione = 0;
 
  for(int i=0; i<n_letture; i++)
  {
    valore += analogRead(A0);
    delay(10);
  }
   tensione = (valore/n_letture)*(300.0/1024.0);
   
   return tensione;
}
« Last Edit: March 17, 2019, 04:55:03 pm by WildMOSFET »
 

Offline djacobow

  • Super Contributor
  • ***
  • Posts: 1062
  • Country: us
  • takin' it apart since the 70's
Re: Arduino question: unstable signal
« Reply #2 on: March 17, 2019, 04:40:35 pm »
Averaging, as described by another poster will help, but in theory even with averaging, the value will hover around your switch point. If you have a control loop, then it *will* hover around your switch point, and you will get a lot of annoying transitions.

The normal way to deal with this is to add some hysteresis to your control, so that there is a bit of a dead band. You do this by making the turn on value a bit lower than the turn off value.

Something like:

Code: [Select]

const uint16_t switch_point = 1234;
const uint16_t half_dead_band  = 5;

bool getOutputState(uint16_t input) {
    static bool curent_state = false;
    if (input < (switch_point - half_dead_band)) current_state = true;
    else if (input > (switch_point + half_dead_band)) current_state = false;
    return current_state;
}


I didn't test this, but you get the idea.
« Last Edit: March 17, 2019, 05:13:16 pm by djacobow »
 
The following users thanked this post: WildMOSFET

Offline WildMOSFET

  • Contributor
  • Posts: 21
  • Country: it
Re: Arduino question: unstable signal
« Reply #3 on: March 17, 2019, 04:48:56 pm »
Averaging, as described by another poster will help, but in theory even with averaging, the value will hover around your switch point. If you have a control loop, then it *will* hover around your switch point, and you will get a lot of annoying transitions.

The normal way to deal with this is to add some hysteresis to your control, so that there is a bit of a dead band. You do this by making the turn on value a bit lower than the turn off value.

I didn't test this, but you get the idea.

You made the idea right.
Hysteresis is also important, otherwise the wine cooler would almost always be in operation, with short breaks.
 
The following users thanked this post: djacobow

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #4 on: March 17, 2019, 09:19:42 pm »
Thanks guys! Over time, I will explore averaging and hysteresis as I want to learn everything. But-- lucky me: I found another solution that seems to be dead-nuts perfect. I abandoned the thermistor and voltage divider, and the need for some kind of stabilization for a TMP36 temp sensor. It will mean pulling the two-conductor wires out of the cooler and letting a three-conductor be pulled in behind them. But that's a small price to pay for excellent performance. Thanks again!
 

Offline Arznei

  • Contributor
  • Posts: 32
  • Country: de
Re: Arduino question: unstable signal
« Reply #5 on: March 17, 2019, 09:48:25 pm »
I'm afraid you will run into the same problems you are having right now even using the TMP36. The problem you are having is not (only) an unstable or inaccurate output of your current sensor, but also the inherent noise of your ADC. Even with a perfectly stable voltage input your ADC readings might have their last bit or more flipping from reading to reading.

Adding a small hysteresis is a pretty simple and working solution to that. Don't be afraid of it sounding somewhat complicated, it's actually easy to implement and will work like a treat, probably even with your original circuit.
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #6 on: March 18, 2019, 03:24:41 am »
"Adding a small hysteresis is a pretty simple and working solution to that. Don't be afraid of it sounding somewhat complicated, it's actually easy to implement and will work like a treat, probably even with your original circuit."

Alright then. I won't kick that can down the road. How can I introduce hysteresis into the original circuit? And thank you.
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1057
  • Country: nz
Re: Arduino question: unstable signal
« Reply #7 on: March 18, 2019, 03:30:55 am »
Alright then. I won't kick that can down the road. How can I introduce hysteresis into the original circuit? And thank you.

You don't introduce it to the circuit as such.
You introduce it to the code.
See the code @djacobow posted above.
Time is the overseer of all things.
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #8 on: March 18, 2019, 03:40:43 am »
I had edited the response about "introducing"...
I explained what confused me about djacobow's suggestion. But when I tried to send it, I got the red warning that I should read a new reply. By doing so, I lost my post. I hate it when that happens. Basically, I was just saying that I didn't understand how you can alter your IF statement on and off times beyond the desired temperatures.
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1057
  • Country: nz
Re: Arduino question: unstable signal
« Reply #9 on: March 18, 2019, 03:48:58 am »
Pseudo code:

Code: [Select]
Hysteresis = 5 degrees.
SetPoint = 30 degrees.
IF Temperature > (SetPoint + Hysteresis) THEN turn_cooler_on
IF Temperature < (SetPoint - Hysteresis) THEN turn_cooler_off

So, what happens is the cooler comes on at 35 degrees and stays on until the temperature drops to 25 degrees.
This way the on/off control is never dependent upon a single exact number. It will only switch the cooler at points that are miles apart from each other and therefore reasonably immune to ADC noise.
Note: The narrower your Hysteresis band, the more your cooler will be turning on and off.
Time is the overseer of all things.
 

Offline Kasper

  • Regular Contributor
  • *
  • Posts: 236
  • Country: ca
Re: Arduino question: unstable signal
« Reply #10 on: March 18, 2019, 04:17:23 am »
Hysteresis is standard solution to stop jitter for all sorts of things.

Specially when you are controlling machines, since jittering means machines starting and stopping much more often than needed and getting worn out faster because of it.

temperature = GetTemperature();
If (temperature > 20): TurnOnCooler();
Elseif (temperature < 10): TurnOffCooler();
 

Offline djacobow

  • Super Contributor
  • ***
  • Posts: 1062
  • Country: us
  • takin' it apart since the 70's
Re: Arduino question: unstable signal
« Reply #11 on: March 18, 2019, 08:29:12 am »
It sounds like the OP is a bit confused or wants to limit his complexity in this project. I understand that, but there are certain things that it's definitely worth understanding if you are going to do more projects that interface electronics with physical reality.

Physical reality is noisy, bumpy, and shaky. When you measure something you get noise. When you move something it often has "backlash", and it may even oscillate before settling. Because of all these phenomenon, you need to make your programs a bit more tolerant of slop. That's what hysteresis is about: it's creating logic so that there is some "space" between what causes your program to "do something" and to "undo that thing."

Without hysteresis, your program will spend a lot of time doing and undoing for no particular benefit.

Three people have provided you code or pseudocode now. What are you confused about?

PS -- regarding averaging. It's great to average. You should almost always do it, whether you're reading from an ADC or your sampling device like the TMP36 is doing that for you. If you're measuring temperature, an instantaneous reading problem doesn't matter much anyway.

There are lots of ways to average, but in microcontroller-land where time and memory are scarce, there is a cheap averaging you should always have in your toolkit, called an exponential moving average filter a.k.a. first-order low-pass IIR filter. Google and you should see examples of how to do it; it's very simple and cheap to calculate and requires only on variable for state.
 
The following users thanked this post: WildMOSFET

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #12 on: March 18, 2019, 11:59:31 am »
Okay. I'm sorry if I seen stubborn about the hysteresis. Actually, I'm not. There's no way I would decline a solution. I will definitely implement it into the code. If I can use existing thermistors rather than fish in a third wire and and new sensors-- I'm a happy man. The only thing I didn't understand was how hysteresis was anything more than the period between off and on. But I'm sure it is or you guys wouldn't have offered it up. Now I'm excited about giving it a whirl. I will report back for sure!
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1225
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: Arduino question: unstable signal
« Reply #13 on: March 18, 2019, 12:56:19 pm »
Alright then. I won't kick that can down the road. How can I introduce hysteresis into the original circuit? And thank you.

You don't introduce it to the circuit as such.
You introduce it to the code.
See the code @djacobow posted above.
You can introduce hysteresis control in your circuit or code, depending on your background knowledge (Hardware vs Software person)
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #14 on: March 18, 2019, 01:02:34 pm »
Peltier? Seriously?
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #15 on: March 18, 2019, 02:32:43 pm »
"Peltier? Seriously?"

I realize that no one would think of Peltier as the ideal cooling mechanism. But that's what I happen to have. We bought a wine chiller about five years ago. The two power supplies went belly up. I worked diligently to repair the boards but simply didn't have the knowledge required. So, now I'm replacing the two power supplies with one off-the-shelf unit with enough current to power the two Peltiers (one for the top of the cabin, one for the bottom). I'm sure I've got several hundred hours and some cash invested in repairing it. I knew from the get-go that it wasn't cost feasible. Instead-- I looked at it as a learning experience. But replacing the power supplies was only part of the fix. I had to find someway to marry it to a small break-out board. After failing at that I thought I might have enough Arduino experience to use an Uno to manage the system. I knew I could produce a high and low state to turn on an LED. So why not substitute the LED with Peltiers (going through relays). And shazam! I was actually able to pull it off with the exception of the bouncing back and forth which had the test relay buzzing around on my desk. So, now, I'll Google hysteresis as it applies to writing a proper sketch. If all goes well-- I'll have my wine cooler back, my wife will think I'm a genius, and only you and I will know that Peltiers are the most next-to-nothing cooling devices known to man.  ;D

PS: Volunteers to write the sketch will not be turned away. Top and bottom cabin chill to 60F. A fan for each Peltier comes on after enough heat is produced to warrant cooling them. They stay on for a short while after the Peltier is turned off. Another fan inside the cabin comes on with the Peltier (to draw in cool air). It stays on for a while after the Peltier goes low (to pull in air from the still-cold element).
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #16 on: March 18, 2019, 04:30:10 pm »
"Peltier? Seriously?"
I realize that no one would think of Peltier as the ideal cooling mechanism. But that's what I happen to have.
IMHO... bin it!
 

Offline djacobow

  • Super Contributor
  • ***
  • Posts: 1062
  • Country: us
  • takin' it apart since the 70's
Re: Arduino question: unstable signal
« Reply #17 on: March 18, 2019, 04:41:06 pm »
"Peltier? Seriously?"
I realize that no one would think of Peltier as the ideal cooling mechanism. But that's what I happen to have.
IMHO... bin it!

Oh come on now, he's doing it for fun and learning.

There is probably no more project more "Arduino" than your first thermostatically-controlled doohickey.

He's needs to write like 10 lines of code and he's in business.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #18 on: March 18, 2019, 05:00:59 pm »
Oh come on now, he's doing it for fun and learning.
I know... I feel guilty saying it but Peltier fridges are just ****.  Imagine if he nips out and buys a compressor based cooler... he will be able to cool a nice bottle of wine and then sip contentedly (knowing the performance and operating costs are going to be better) whilst working on a more useful project.
It's a bit like saying, building a solar roadway is a learning experience.  :-DD

... but...

If he insists... considering using an internal ARef (depends on which Arduino is being employed) and then the techniques listed above.  Also consider cooling for the peltier element and having the fan overrun to stop heat from the hot side migrating into the cooler when the element is off - Already mentioned but important.
« Last Edit: March 18, 2019, 05:19:41 pm by NivagSwerdna »
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1057
  • Country: nz
Re: Arduino question: unstable signal
« Reply #19 on: March 18, 2019, 06:40:20 pm »
Alright then. I won't kick that can down the road. How can I introduce hysteresis into the original circuit? And thank you.

You don't introduce it to the circuit as such.
You introduce it to the code.
See the code @djacobow posted above.
You can introduce hysteresis control in your circuit or code, depending on your background knowledge (Hardware vs Software person)

Yes, you are correct.
However, in this specific case I would suggest the OP will find it considerably easier to introduce it in code.
Time is the overseer of all things.
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Reporting Back
« Reply #20 on: March 21, 2019, 04:56:19 pm »
As suggested by several posts, averaging didn't work out. It dramatically reduced the amount of bounce so that readings would vary by three or four digits rather than ten or so. But, the result was that the LED I wanted to control would always be half-on (dim) even as the numbers were no where near the turn-on range. And when the wine cooler temp got low enough so that all numbers clearly were in range, the LED remained dim as if it were controlled by a 50% duty cycle in PWM.

The purpose of that particular test was to see how effective "smoothing" would be in cleaning up the numbers. I knew that it might not have anything to do with the use of hysteresis, but still something I wanted to see.

If the group is wondering why I don't just go ahead and convert djacobow's hysteresis example, it's because I haven't yet figured out how to write it. And since my last post I've discovered that the whole thing may not work anyhow as my power supply's noise throws any data (sent to the Arduino) into total chaos. I might have never known this without adding a laptop to the project so that I could actually see the ADC (Analog to Digital Converter) numbers with and without the power supply.

But I still won't abandon the project until I'm able to control the cooler with the Arduino even if I have to connect it to a car battery! As stated before, the goal is to learn enough electronics/Arduino to (at least) control the chiller according to fluctuating temps. To do that, I will try again to convert djacobow's example to my application.

I know that all this seems pointless to many viewers. On the other hand, maybe someone as slow on the uptake as I am, and will be able to benefit from the thread.

(The ADC number that correlates to the temp for turning off the cooler would be 650. The numbers correlating to the temp for turning the cooler back on would be 675. So that's the range.)

Suggestion to learn about first-order low-pass filter duly noted.
 

Offline Kasper

  • Regular Contributor
  • *
  • Posts: 236
  • Country: ca
Re: Arduino question: unstable signal
« Reply #21 on: March 22, 2019, 03:21:44 pm »
If you post your code, it will be easier for people to recommend ways to add hysteresis.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #22 on: March 22, 2019, 03:59:19 pm »
Can you tell us a bit about the Thermister?  What is is it's resistance at two temperatures?

Also, post your code.

How does your code compare against say... http://www.circuitbasics.com/arduino-thermistor-temperature-sensor-tutorial/ ?

... oh... and although it is possibly a guess too far... your voltage divider is currently referenced to the supply, If you get a sag/ripple on the supply it will affect your measurements.   

I reached over to my box of sensors and grabbed a thermister....  which is NTC 10k Ohms at 25C.   It currently measures around 8k6 Ohms... in an arrangement with a fixed 10k resistor that should give a range between 2.5V (at 25C) and 3.7V (at 0C)... what sort of numbers are you getting?  Are you using an UNO?

I took a 10k resistor and put it between 5V and A0, Put the NTC across the A0 and GND....

Code: [Select]
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600); 
  pinMode(ledPin, OUTPUT);
}

#define SIZE 512

int j=0;
int i = 0;
int n = 0;
int readings[SIZE];

void loop() {
  digitalWrite(ledPin, HIGH);
 
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

  readings[i] = sensorValue;
  i++;
  i %= SIZE;
  if (n<SIZE) n++;

  j++;
  if (j==10000) {
    int min=readings[0];
    int max=readings[0];
    for (int k=0; k<SIZE; k++) {
      if (readings[k]<min) min=readings[k];
      if (readings[k]>max) max=readings[k];
    }
    Serial.print("Reading: Min ");
    Serial.print(min);
    Serial.print(", Max ");
    Serial.print(max);
    Serial.println();
    j=0;   
  }
   
 
  // turn the ledPin on
  digitalWrite(ledPin, LOW);
}


The example is unnecessarily complex but just takes 512 samples and then every now and again looks at the set and calculates max and min to give a range... under steady state max and min should be similar (and they are)...

It gives a very stable set of readings....

Code: [Select]
Reading: Min 531, Max 533
Reading: Min 532, Max 533
Reading: Min 531, Max 533
Reading: Min 531, Max 533
Reading: Min 531, Max 533
Reading: Min 532, Max 533
Reading: Min 532, Max 533
Reading: Min 532, Max 533
Reading: Min 531, Max 533
Reading: Min 532, Max 533
Reading: Min 531, Max 533
Reading: Min 531, Max 533

Started warming NTC....

Reading: Min 531, Max 534
Reading: Min 530, Max 533
Reading: Min 528, Max 532
Reading: Min 514, Max 518
Reading: Min 503, Max 506
Reading: Min 492, Max 497
Reading: Min 486, Max 489
Reading: Min 480, Max 483
Reading: Min 474, Max 478
Reading: Min 470, Max 474
Reading: Min 467, Max 471
Reading: Min 463, Max 467
Reading: Min 460, Max 465
Reading: Min 458, Max 463
Reading: Min 455, Max 459
Reading: Min 453, Max 457
Reading: Min 451, Max 455
Reading: Min 449, Max 453
Reading: Min 448, Max 452
Reading: Min 446, Max 450
Reading: Min 444, Max 449
Reading: Min 443, Max 448
Reading: Min 442, Max 445
Reading: Min 443, Max 445
Reading: Min 445, Max 447
Reading: Min 447, Max 448
Reading: Min 447, Max 450
Reading: Min 448, Max 452

Let go of NTC...

Reading: Min 451, Max 452
Reading: Min 453, Max 454
Reading: Min 454, Max 456
Reading: Min 455, Max 456
Reading: Min 455, Max 458
Reading: Min 456, Max 458
Reading: Min 457, Max 459
Reading: Min 458, Max 460
Reading: Min 459, Max 461
Reading: Min 460, Max 461
Reading: Min 460, Max 462
Reading: Min 461, Max 462
Reading: Min 461, Max 464
Reading: Min 462, Max 464
Reading: Min 463, Max 464
Reading: Min 463, Max 465
Reading: Min 464, Max 465
Reading: Min 464, Max 466
Reading: Min 464, Max 466
Reading: Min 464, Max 467
Reading: Min 466, Max 467
Reading: Min 466, Max 468
Reading: Min 466, Max 468
Reading: Min 467, Max 469
Reading: Min 467, Max 469
Reading: Min 468, Max 469
Reading: Min 468, Max 470
Reading: Min 469, Max 470
Reading: Min 469, Max 471
Reading: Min 469, Max 471
Reading: Min 469, Max 471
Reading: Min 469, Max 472
Reading: Min 471, Max 472
Reading: Min 471, Max 472
Reading: Min 471, Max 472
Reading: Min 471, Max 473
Reading: Min 472, Max 474
Reading: Min 471, Max 474
Reading: Min 473, Max 474
Reading: Min 473, Max 474
Reading: Min 473, Max 475
Reading: Min 473, Max 475
Reading: Min 473, Max 476
Reading: Min 474, Max 476
Reading: Min 475, Max 476
Reading: Min 475, Max 477
Reading: Min 475, Max 477
Reading: Min 476, Max 477
Reading: Min 475, Max 478
Reading: Min 476, Max 478
Reading: Min 477, Max 478
Reading: Min 477, Max 478
Reading: Min 477, Max 479
Reading: Min 477, Max 479
Reading: Min 477, Max 479
Reading: Min 478, Max 480
Reading: Min 478, Max 480
Reading: Min 478, Max 480
Reading: Min 479, Max 480
Reading: Min 479, Max 481
Reading: Min 479, Max 481
Reading: Min 480, Max 481
Reading: Min 480, Max 482
Reading: Min 481, Max 482
Reading: Min 480, Max 482
Reading: Min 481, Max 483
Reading: Min 480, Max 483
Reading: Min 481, Max 483
Reading: Min 482, Max 484
Reading: Min 482, Max 484
Reading: Min 483, Max 484
Reading: Min 483, Max 485
Reading: Min 484, Max 485
Reading: Min 484, Max 486
Reading: Min 483, Max 486
Reading: Min 485, Max 486
Reading: Min 485, Max 487
Reading: Min 485, Max 487
Reading: Min 485, Max 487
Reading: Min 486, Max 487
Reading: Min 486, Max 487
Reading: Min 486, Max 488
Reading: Min 486, Max 488
Reading: Min 487, Max 488
Reading: Min 486, Max 489
Reading: Min 486, Max 490
Reading: Min 487, Max 489
Reading: Min 487, Max 489
Reading: Min 487, Max 490
Reading: Min 488, Max 490
Reading: Min 488, Max 490
Reading: Min 489, Max 490
Reading: Min 488, Max 490
Reading: Min 489, Max 491
Reading: Min 489, Max 491
Reading: Min 489, Max 491
Reading: Min 490, Max 491
Reading: Min 490, Max 492
Reading: Min 489, Max 492
Reading: Min 490, Max 492
Reading: Min 490, Max 492
Reading: Min 490, Max 492
Reading: Min 491, Max 493
Reading: Min 491, Max 493
Reading: Min 491, Max 493
Reading: Min 492, Max 493
Reading: Min 492, Max 494
Reading: Min 492, Max 494
Reading: Min 491, Max 494
Reading: Min 493, Max 494
Reading: Min 492, Max 494
Reading: Min 493, Max 495
Reading: Min 493, Max 495
Reading: Min 493, Max 495
Reading: Min 493, Max 496
Reading: Min 494, Max 496
Reading: Min 494, Max 495
Reading: Min 493, Max 496
Reading: Min 495, Max 496
Reading: Min 495, Max 496
Reading: Min 494, Max 497
Reading: Min 494, Max 497
Reading: Min 495, Max 497
Reading: Min 495, Max 497
Reading: Min 494, Max 497
Reading: Min 496, Max 498
Reading: Min 496, Max 498
Reading: Min 496, Max 498
Reading: Min 496, Max 498
Reading: Min 496, Max 498
Reading: Min 496, Max 499
Reading: Min 496, Max 499
Reading: Min 496, Max 499
Reading: Min 497, Max 499
Reading: Min 496, Max 499
Reading: Min 496, Max 499
Reading: Min 497, Max 500


So seems pretty stable.... now what do you see...? 

(My suspicion is still on power rail droop confusing you)
« Last Edit: March 22, 2019, 05:35:33 pm by NivagSwerdna »
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #23 on: March 22, 2019, 06:22:02 pm »
Here's my example code... given that the measurements are pretty stable... within a couple...  A small state machine and different thresholds does it.

Note that for my NTC sensor the numbers increase as it gets colder and decrease as it gets hotter... A PTC sensor would do the opposite and the comparisons and thresholds would have to be the other way around.  (If you get the thresholds the wrong way round it oscillates at the switch point... maybe that is your only problem?)

Code: [Select]
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED

void setup() {
  Serial.begin(9600); 
  pinMode(ledPin, OUTPUT);
}

int getTemp() {
  return analogRead(sensorPin);
}

#define STATE_HOT 1
#define STATE_COLD 2

int state = STATE_COLD;

#define THRESHOLD_COLD_TO_HOT  500
#define THRESHOLD_HOT_TO_COLD  520

void loop() {
    int current = getTemp();

    switch(state) {
      case STATE_COLD:
        if (current<THRESHOLD_COLD_TO_HOT) {
          state = STATE_HOT;
        };
        break;
      case STATE_HOT:
        if (current>THRESHOLD_HOT_TO_COLD) {
          state = STATE_COLD;
        };
        break;
    }
//    Serial.print("Reading: ");
//    Serial.print(current);
//    Serial.print(" ");
//    Serial.print(state);
//    Serial.println();

    digitalWrite(ledPin, (state == STATE_HOT) ? HIGH : LOW);
}

Share & Enjoy
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Arduino question: unstable signal
« Reply #24 on: March 24, 2019, 03:05:56 pm »
I should probably throw in the towel. I have no business posting on a forum while lacking the most fundamental knowledge. It's not fair to you guys. Bryan Reagan, a stand up comedian, does a bit where he's taking very simple instructions from UPS on how to get some packages ready. After half of the first sentence, he says "Slow down!" That's me! So maybe I'll end the thread after today.

The problem in trying to help someone like me is that suggestions (do this), leads to "I don't know how to do that or what you're even talking about". While another beginner may see hysteresis as an obvious fix, I'm trying to figure out how to execute it.

Anyhow, I'm going to respond to the latest crop of generous suggestions. Any of you who see this as a lost cause can bail.

Answer to thermistor question:
75.8F = 10.72K ohms
60.1F = 14.76K ohms

Thermal control is currently off the table though as Arduino ADC output numbers are chaotic when power supply running.

Arduino is Uno Rev 3

(In addition to scrapping the original power supplies, I ditched the break out board the controlled them. That's where the Arduino comes in.)

As to my code: It's constantly changing with trial and error. The thinking was-- hit on something that works, and then figure why. I've had this wine cooler in the middle of our home office for (probably) four months. Remember, it's about the challenge, not whether the cooler is worth having in the first place.

The ideal completed project would have two Peltier elements on slightly different schedules: one for the lower half for white wine, the other for the top half for red wine (or the other way around). Additionally, there are two heat sink fans that would come on shortly after the Peltiers come on and need cooling. They might be programmed to stay on for a bit shortly after the Peltiers go off. Two more fans, one for the top half, one for the bottom half, are located in the cabinet to pull air over the cold sinks and into the cooler. Ideally, they too, would be on slightly different schedules.

As if that weren't complicated enough, There are controls on the door display to change the settings. That would be another project altogether.

What I'm shooting for now is an initial cool-down from room temp, maybe 30 minutes, and then an concessional restart of the cooler as the temp begins to rise. So, maybe, run both Peltiers and all fans (as if they were one) for 30 minutes. That seems adequate to get around 58F or so. Then, shut down for maybe 20 minutes. It seems insulated well enough to maintain a temp no greater than 62F or so. After the 20 minutes shut down, everything comes back on for ten minutes.

So, on for 30minutes; off for 20 minutes; then repeated cycles of off for 20 minutes; on for 10. Hopefully, that crude arrangement would have the cooler functional. I could go back to simple projects no bigger than the top of my work bench. In time I would master skills needed to improve the wine cooler, even the door, with proper code. Having said all that, I'm unable to go beyond mostly psuedo code as follows:

<code>

unsigned long top_fans_and_pelt = 8;//pin 8 turns on relay for top peltier and all fans
unsigned long bot_pelt = 9;//pin 9 turns on relay for 2nd peltier
unsigned long previous = 0;
int interval30 = 1800000;//30 minutes
int interval20 = 1200000;//20 minutes
int interval10 = 600000;//10 minutes

void setup(){
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}

void loop(){

current = millis();//start the clock

if(current > 0 || < 5){
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}

after 30", shut down (one time)

then off 20"
then back on 10"
repeat over and over
}//end loop

</code>

 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #25 on: March 24, 2019, 09:54:05 pm »
75.8F = 10.72K ohms
60.1F = 14.76K ohms

Apart from those quaint F units looks very like my thermister above... Resistance increases as it gets colder... NTC... and around 10k at 25C

It must be a power supply issue.  Try powering the Arduino from a seperate 9V wall wart.  Use my sketch above with your Arduino away from the rest with just 10k fixed resistor and the thermister as a voltage divider between 5V and GND... then report back.  I think I used A0 on an UNO clone.

PS
I see you have start to think about timing...

current = millis();

Just be aware that millis() being 1000th of a second... is not continuous... https://www.arduino.cc/reference/en/language/functions/time/millis/ it wraps... and your code should handle that.

PPS
Don't feel guilty about asking simple questions; it's that type we have a chance of answering... the tricky ones we leave to the experts.  :-DD
 
« Last Edit: March 24, 2019, 10:01:58 pm by NivagSwerdna »
 

Offline billbyrd1945

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Wow!
« Reply #26 on: March 25, 2019, 01:15:17 am »
EUREKA!

It worked! Thank you a million times!

There are a couple more tweaks needed: The code provides two states. I would need a third one for a long transition from off to the first cool down. The cooler is so well insulated that I may not need it though. It may be able to function well with only short ons, long offs, gradually getting cool enough and able to hold it when off.

The other thing is that there is some on/off behavior between full off and full on. It's so regular, it looks like a blink program. Too many number changes take place on the way to getting inside a window of solidly on or solidly off. I plugged in a three second delay in order to get a good look at the numbers. And I had to change the code settings from 500/520 to 768/762.

The behavior is as follows:

>768: solidly off
<768 and > 762: blink program
<=762: solidly on

I didn't concern myself with actual cooler temps as I'm confident that moving the numbers up and down will take care of that. The fantastic thing is that the program actually works AND, I get to use the thermistors that I had completely given up on! The only problem with them is noise from the power supply I ordered. The testing tonight was with my bench supply that produces no noise. But that's another issue all together.

I'm a happy man if I can get the blinking to go away. Then, all I'll need is some means of killing the noise from my power supply.

Is there some way to put averaging in the program? I'm almost afraid to touch it. But I will though. Gotta try. I'll just keep a copy of this one. Or you can send an updated code if you would be so kind.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 1872
  • Country: gb
Re: Arduino question: unstable signal
« Reply #27 on: March 25, 2019, 08:46:34 am »
The blinking will be because your thresholds and or conditions are the wrong way around. (or your thresholds are too close relative to noise)
Now welcome to the interesting part....
In compressor fridges there are a number of trade offs so for example many start stops is a bad thing so parameters like minimum off time or max continuous on time can be considered. Also ice builds up on the cooling elements so at some points they stop cooling and let the ice melt perhaps by using a heating element (and typically lie to the user about their temperature).

Have a Google for the instruction manual for an elliwell controller to get an idea for the sort of parameters.

Cheers!

PS
Averaging isn't your problem. Averaging might allow you to eliminate some Gaussian wobble but it won't help if you have droopy signals because the ADC reference is moving.  You could consider using a better reference, possibly an internal AVR one or fix your power supply to the controller.  I have seen this with a design of a circuit that drove multiple servos and the designer was stumped that the circuit behaved badly when the motors ran leading to brownouts, confusion and general mayhem.
« Last Edit: March 25, 2019, 10:27:41 am by NivagSwerdna »
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2408
  • Country: dk
  • Country: Broken Britain
Re: Arduino question: unstable signal
« Reply #28 on: March 25, 2019, 10:19:26 am »
Is there some way to put averaging in the program?

It's very easy to test some averaging, just replace the the single analogRead() with 4 summed and then divided by 4, you can put it into a loop later. I think averaging will help even if it's supply noise. If your ADC value's noise is only +/- 3 counts that's not terribly bad.

value = analogRead(sensorPin);
delay(4);
value += analogRead(sensorPin);
delay(4);
value += analogRead(sensorPin);
delay(4);
value += analogRead(sensorPin);
value = value/4;

« Last Edit: March 25, 2019, 10:28:24 am by StillTrying »
CML+  That took much longer than I thought it would.
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1225
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: Arduino question: unstable signal
« Reply #29 on: March 25, 2019, 12:33:50 pm »
When dealing with thresholds, it is better to go with min/max rather than average.  Measure the min and max for N measurements, then set your on/off thresholds based on those values
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf