Hi all, This is my first post here since I discovered Arduino, I really like it but I am not really familiar with elettronics
What I am tryng to do is to use one of the analog input of arduino to recognize if a switch has been turned on or of, this switchs are used to check if a sliding part has reached the top or the bottom,
Oversimplyfing the circuit, it is something like linking the +5V output of Arduino to one of the Analog inputs using a switch, when the switch is turned on I read rightly the 1023 value, but if the swtich is on the "off" position I can read values that are very far from the 0, sometimes I can read even if the swtich is down a value that is very near to 1023, I understand that it has sense and I am reading the potential of the "Word" around me, but Someone can suggest me a way to decrease this noise? Adding maybe something between the switch and the Analog input port? Maybe a capacitor?
actually the easiest way to solve this is to use a digital pin, rather than analog. the digital pins are high and low anyway, so with just a delay function, it will correctly read only on or off!
Thank you so much for your reply, but I started initially using the digital input approach you suggested me, but I had the same problem: wrong signals from arduino that told me that the switch was closed even if it was not closed! that this is the initial version of the function I had for the digital input:
float Verifica_Touch(int pin_verifica)
{
long i;
float valore=0;
for (i=0; i < 20; i++)
{
int valore_appoggio= digitalRead(pin_verifica);
delay(1);
valore+= valore_appoggio;
}
i=0;
return (valore / 20.00);
valore=0;
}
I inserted several reading operations to smooth the value but it didn't help, So I am tryng with the analog pins.
The thing that screw me up is that if I use a multimeter to measure the potential at the ends of the switch I have exactly zero OR 5 Volt, nothing in the middle, and I also noticed that if I measure the potential between the ground pin and the analog pin of arduino I have more or less 0.6 volts, but as I already told I am a very noob in electronics
This is the version of the code using the analog Pin
float Verifica_Touch(int pin_verifica)
{
int i;
long valore_sommato=0;
int volte=35;
long prodotto=0L;
long voltaggio=1022L;
prodotto=voltaggio*volte;
// Serial.println(prodotto);
float valore=0;
for (i=0; i < volte; i++)
{
int valore_letto= analogRead(pin_verifica);
//Serial.print("valore letto " );
//Serial.println(valore_letto);
delay(10); // DA VERIFICARREEEEEE!!!!
//Serial.println(i);
valore_sommato+= valore_letto;
}
i=0;
//Serial.print("valore_sommato ");
//Serial.println(valore_sommato);
return (float(valore_sommato) / float(prodotto) );
valore=0;
valore_sommato=0;
}
bishop_house:
Hi all, This is my first post here since I discovered Arduino, I really like it but I am not really familiar with elettronics
What I am tryng to do is to use one of the analog input of arduino to recognize if a switch has been turned on or of, this switchs are used to check if a sliding part has reached the top or the bottom,
Oversimplyfing the circuit, it is something like linking the +5V output of Arduino to one of the Analog inputs using a switch, when the switch is turned on I read rightly the 1023 value, but if the swtich is on the "off" position I can read values that are very far from the 0, sometimes I can read even if the swtich is down a value that is very near to 1023, I understand that it has sense and I am reading the potential of the "Word" around me, but Someone can suggest me a way to decrease this noise? Adding maybe something between the switch and the Analog input port? Maybe a capacitor?
Thank you for your help!
Ciao ciao!!
Giampiero
The problem with your method is that when the switch is not help down there is not a really electrical 0 volts being applied to the pin, but rather a 'floating input' condition. You need to wire a say 5k ohm resistor from the analog input pin and ground, that way when the switch is released the pin will see a ground voltage and give a 0 count return. Try it and see if that does not improve your situation.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
this is the basic switch reading code. notice how it defines inputs and outputs, and only uses highs and lows. a float will not be necessary. only an integer. try this!
As suggested you need to set up a resistor as a 'pull down' resistor to stop the floating input when the switch is off. In that mode it is actually not off (0) but the Arduino pin is actually not connected at all. The problem is not a software one but related to your hardware configuration.
retrolefty:
The problem with your method is that when the switch is not help down there is not a really electrical 0 volts being applied to the pin, but rather a 'floating input' condition. You need to wire a say 5k ohm resistor from the analog input pin and ground, that way when the switch is released the pin will see a ground voltage and give a 0 count return. Try it and see if that does not improve your situation.
Lefty
Thank you so much, this was more or less the kind of answer I was expecting such as " Put this there and see "
I tried and things are going much better, even if when I turn on the entire system, ( arduino is used for driving two of these Stepper motor drive - NDC 94 - R.T.A. srl - DC ) there is still some noise, but there are two capacitors big as cow....
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
this is the basic switch reading code. notice how it defines inputs and outputs, and only uses highs and lows. a float will not be necessary. only an integer. try this!
Thank you I used the float value because I sum several reading of the pin to obtain an average value of what I am reading, just for this.