I'm using three B10k pots on breadboard with the following function:
wired as:
wiper leg >> analog pin
one leg >> gnd
other leg >> +5V
void readPotentiometers() {
for (int i = 0; i < numButtons; i++) {
int potValue = 1023 - analogRead(potentiometerPins[i]); // Read the potentiometer value
//has analog value changed more than the filter amount ?
if (abs(lastValue[i] - potValue) > filter) {
if (lastValue[i] != potValue) {
//update to the new value
lastValue[i] = potValue;
Serial.print("Potentiometer");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(potValue);
}
}
}
}
I'm still having some noise ( unwanted fluctuating in numbers from the analog pins) . How can I get rid of it?
Does the fact I'm using breadboard contributing to the noise? will soldering the pots will eliminate the noise? will using larger value of pot will help?
To debug, display the Actual value read from the AD pin. Add different small value capacitors from the wiper to ground. See which value makes the greatest difference. A larger value pot will just make the noise have a greater value.
With an AD conversion, the LO bit will always be changing from 0 to 1 and back because of the uncertainty of the actual process. If more than the LO bit is changing, then you may need to smooth/average the readings.
That value is okey.
Is there any difference, in the trouble, between the different buttons?
Is there any pattern, noise coming at certain times?
Do You have an oscilloscope available to take a closer look att the analog inputs?
I use the same kind of analog controlling DC motors and never experienced and motor jumping.
between the difference pots? I don't see much difference.
I noticed the if my filter value is 2 the reading is just fine (noise is gone) but if I'm taking the filter value to 1 the fluctuating is back.
I have added 10nF cap between wiper to ground which I think helped to reduced the fluctuating
edit: so it seems to work very stable even when filter is equal to 1. I think the fluctuating I'm facing is because the pots not sitting stable on my breadboard
Breadboards are noisy. Hard wire (soldered or screw terminals) with SHIELDED wire for best results. Breadboards are not recommended for HF analog work because of parasitic coupling and intrinsic stray capacitance.
Rock solid, powered by Pc USB. Most of the time not even 1 digit change.
What filter? There's no filtering in that code. You made the mistake of posting a snippet. Don't do that again.
Please post a picture of the setup. Long cables, noisy equipment nearby, noisy environment in general?
const int numButtons = 3; // Number of buttons
const int numPots = 3; // Number of pots
const int buttonPins[numButtons] = {2, 3, 4}; // Button pin numbers
const int ledPins[numButtons] = {5, 6, 7}; // LED pin numbers
const int potentiometerPins[numPots] = {A0, A1, A2}; // Potentiometer analog input pins
const int heartbeatLED = 13;
bool ledStates[numButtons] = {false}; // States of LEDs
bool previousStates[numButtons] = {false}; // Previous states of buttons
unsigned long debounceDelay = 25; // Debounce delay time in milliseconds
unsigned long lastDebounceTime[numButtons] = {0}; // Last time the button was toggled
int lastValue[numPots];
const byte filter = 1; //adjust as needed
//timing variables
unsigned long previousMillis = 0;
unsigned long heartbeatMillis;
void setup() {
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Set button pins as inputs with internal pull-up resistors
pinMode(ledPins[i], OUTPUT); // Set LED pins as outputs
pinMode(heartbeatLED, OUTPUT);
}
Serial.begin(115200); // Initialize serial communication
}
void loop() {
checkButtons();
readPotentiometers();
checkHeartbeatTIMER();
}
void checkButtons() {
for (int i = 0; i < numButtons; i++) {
bool currentButtonState = (digitalRead(buttonPins[i]) == LOW); // Read the current state of the button
// Check if the button state has changed and enough time has passed for debounce
if (currentButtonState != previousStates[i] && (millis() - lastDebounceTime[i]) > debounceDelay) {
lastDebounceTime[i] = millis(); // Update the debounce time
ledStates[i] = !ledStates[i]; // Toggle the state of the LED
digitalWrite(ledPins[i], ledStates[i]); // Update the LED state
Serial.print("Button");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(ledStates[i]);
previousStates[i] = currentButtonState; // Update the previous button state
}
}
}
void readPotentiometers() {
for (int i = 0; i < numButtons; i++) {
int potValue = 1023 - analogRead(potentiometerPins[i]); // Read the potentiometer value
//has analog value changed more than the filter amount ?
if (abs(lastValue[i] - potValue) > filter) {
if (lastValue[i] != potValue) {
//update to the new value
lastValue[i] = potValue;
Serial.print("Potentiometer");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(potValue);
}
}
}
}
void checkHeartbeatTIMER() {
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500ul) {
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
} //END of checkHeartbeatTIMER()
No.
Breadboard and short cables usually works rather well. Know the digital uncertainty in the last bit, the half bit issue. A real value of 55.5 will be alternating between 55 and 56. Totally normal.
Yeah, you're going to have noise with that. Stable readings need stable connections. LSB is 5mV, 50mV is +-10 counts. Get a terminal shield and power distribution board and screw everything down.
void readPotentiometers() {
for (int i = 0; i < numPots; i++) { // numPots, not numButtons
potvalue = analogRead(potentiometerPins[i]; // dummy read
int potValue = analogRead(potentiometerPins[i]); // swap the outside pot wires to use this