[SOLVED] How to read the maximum voltage value from analog input?

Hi folks,

I am trying to read the maximum voltage value from analog input using the following code:

void setup() {
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop() {
  Serial.println("value = ");
  Serial.println(analogRead(A5));
}

the analog input is connected to the breadboard via a 220 ohm resistor.

And the serial output result is displaying normally as the following:

value =
0
value =
1023
value =
1023
value =
497
value =
0
value =
642
value =
1023
value =
777
value =
21
value =
161
value =
985
value =
1023
value =
409
value =
0
value =
737
value =
1023
value =
685
value =
0
value =
434
value =
1023
value =
968
value =
150
value =
0
value =
993
value =
1023
value =
395
value =
0
value =
752
value =
1023
value =
669
value =
0
value =
448
value =
1023
value =
955
value =
141
value =
0
value =
1003
value =
1023
value =
582
value =
0
value =
549
value =
1023
value =
864
value =
74
value =
86
value =
1023
value =
1023
value =
494
value =
0
value =
649
value =
1023
value =
771
value =
17

and that's an expected behavior. But I don't know how can I read just the max value instead of reading all the different value.

My application is about reading max value per each button switch combination from a charlieplexing circuit.

Any help is greatly appreciated.

Thank you.

kaisbensalah:
Hi folks,

I am trying to read the maximum frequency from analog input using the following code:

void setup() {

Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop() {
  Serial.println("value = ");
  Serial.println(analogRead(A5));
}

AnalogRead returns a value based on the voltage on that pin NOT frequency. it is it voltage or frequency you after?

See above reply. Please provide some more details.

Further

If you only want to display the highest value, use an additional variable to remember the highest value that you have measured and only print if the current reading is higher than the highest reading at present.

Analog read reads DC voltage levels from 0 to Vcc or AREF, not frequency. What is connected to the other end of the resistor?

sherzaad:
AnalogRead returns a value based on the voltage on that pin NOT frequency. it is it voltage or frequency you after?

Hi, thanks for your reply.

I mean the voltage (analog value), sorry if I used a confusing term.

outsider:
Analog read reads DC voltage levels from 0 to Vcc or AREF, not frequency. What is connected to the other end of the resistor?

Hi, yep. I mean voltage. only a wire connecting directly to the ground. the only component that exists on the circuit is a resistor.

So the method you mentioned doesn't fit my application.

But it finds the highest value read from an analogue pin, which appears to be what you want, so why can't you use it ?

The only code you have shown us just reads a value from one pin.

If you have a much more complex requirement and the code to match then perhaps you should give details of that instead of just saying "doesn't fit my application" without any hint of what application is.

We're mostly not bad at programming but we're rubbish at mind-reading.

Steve

UKHeliBob:
But it finds the highest value read from an analogue pin, which appears to be what you want, so why can't you use it ?

Do you mean that it dumps the highest value as related to each cycle? because I thought it's meant to measure the absolute highest value.

Do you mean that it dumps the highest value as related to each cycle? because I thought it's meant to measure the absolute highest value.

The technique will provide the highest value over a number of readings. How many readings you take and the source of the readings is up to you. If you need to read the highest value returned from any combination of 4 inputs and save the 16 values separately then it could do that as well.

kaisbensalah:
I'm willing to implement charlieplexing using 4 buttons and I need to find a unique value for the voltage from each button switch combination. So how this can be accomplished?

for starters a button is a digital input ie its either open(released) or closed(pressed). so what are you using analog input!

secondly if your IOs are not limited you better off having an individual input for each button since to charlieplex 4 button you would need 3 IOs!

if you insist on using analog input of you buttons, you need a threshold of when the button is ON and OFF. for example if you are using a pullup resistor at you arduino then when the button is not pressed the input see a HIGH (5V) and when its pressed the button pull the input to GND (0V) which is a LOW. you can choose a threshold of 2.5V (512 in your code) to detect a Press or release

sherzaad:
for starters a button is a digital input ie its either open(released) or closed(pressed). so what are you using analog input!

secondly if your IOs are not limited you better off having an individual input for each button since to charlieplex 4 button you would need 3 IOs!

if you insist on using analog input of you buttons, you need a threshold of when the button is ON and OFF. for example if you are using a pullup resistor at you arduino then when the button is not pressed the input see a HIGH (5V) and when its pressed the button pull the input to GND (0V) which is a LOW. you can choose a threshold of 2.5V (512 in your code) to detect a Press or release

I need to use one analog input per 4 buttons, because I need to connect many buttons using my card - that's why I'm going to use charlieplexing technique within my circuit.

UKHeliBob:
The technique will provide the highest value over a number of readings. How many readings you take and the source of the readings is up to you. If you need to read the highest value returned from any combination of 4 inputs and save the 16 values separately then it could do that as well.

Ok, that's what I am looking for. But as I'm new to Arduino, I don't know how this can be achieved. How this can be done using code?

How this can be done using code?

The easiest way to do this is to press a combination of buttons and manually note the highest value returned over a period.

This calibration process is presumably not going to be done many times so it may not be worth the effort of trying to automate it. To do so you would need a method of determining whether a button was pressed or not and I assume that they are single pole switches leaving no spare contacts to read their state.

How are the buttons wired and what is connected when a button is pressed ?

UKHeliBob:
The easiest way to do this is to press a combination of buttons and manually note the highest value returned over a period.

This calibration process is presumably not going to be done many times so it may not be worth the effort of trying to automate it. To do so you would need a method of determining whether a button was pressed or not and I assume that they are single pole switches leaving no spare contacts to read their state.

How are the buttons wired and what is connected when a button is pressed ?

You might have a look at my schematic here...

Thank you.

I tried to follow up what @sterretje have suggested, using the following code

int max = 0;
bool cycle_ended = true;

void setup() {
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop() {
  int temp = analogRead(A5);
  if (cycle_ended && temp == 0) {
    cycle_ended = false;
  }
  else {
    if (!cycle_ended && temp == 0) {
      Serial.println("max = ");
      Serial.println(max);
      max = 0;
      cycle_ended = true;
    }
  }
  if (temp > max) {
    max = temp;
  }
}

However the output doesn't show correct results, as can seen below:

max =
0
max =
927
max =
1023
max =
0
max =
992
max =
1023
max =
0

But instead different values from the only expected value which is 1023.

What's wrong with my code?

I suggest that you start with a simpler program. Something like this

int max = 0;
bool cycle_ended = true;

void setup() {
  Serial.begin(9600);
  pinMode(A5, INPUT);
}

void loop() {
  int temp = analogRead(A5);
  if (cycle_ended && temp == 0) {
    cycle_ended = false;
  }
  else {
    if (!cycle_ended && temp == 0) {
      Serial.println("max = ");
      Serial.println(max);
      max = 0;
      cycle_ended = true;
    }
  }
  if (temp > max) {
    max = temp;
  }
}

Hi, thanks for your reply.

I have just compiled the sample and uploaded it to the board, but the values are still different:

max =
299
max =
0
max =
299
max =
296
max =
0
max =
301
max =
294
max =
0

What's supposed to cause such behavior?

Also the same values get displayed even when having the button switched off (i.e no current flowing in the circuit) - Which seems to me very weird.