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

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

The output you posted did not come from the program that I posted

I re-checked that, and this is the output:

UKHeliBob:
The output you posted did not come from the program that I posted

I re-checked that, and this is the output:

max =
636
max =
663
max =
0
max =
669
max =
662
max =
0
max =
667
max =
661
max =
0
max =
666
max =
0

still getting different values. and also the output is not showing only 0 when button is switched off ..

also the output is not showing only 0 when button is switched off ..

You seem too think that reading an analog pin with nothing (effectively) connected to it should return 0. That is NOT the case. Reading an analog pin with nothing connected to it will give you values that are all over the place.

kaisbensalah:
I re-checked that, and this is the output:

I re-checked that, and this is the output:

still getting different values. and also the output is not showing only 0 when button is switched off ..

Sorry, my mistake, I copied the wrong program into my post

Try this

int max = 0;
const byte inPin = A0;

void setup()
{
  Serial.begin(115200);
  pinMode(inPin, INPUT);
}

void loop()
{
  int temp = analogRead(inPin);
  if (temp > max)
  {
    max = temp;
    Serial.print("max set to : ");
    Serial.println(max);
  }
}

Hi,
You need to set something like this up to test your code properly;

Use a 10K potentiometer.
Connect the blue wire to A5;

Tom... :slight_smile:

TomGeorge:
Hi,
You need to set something like this up to test your code properly;

Use a 10K potentiometer.
Connect the blue wire to A5;

Tom... :slight_smile:

Do you mean I need to connect A5 port to both potentiometer and switch button (parallely)?

kaisbensalah:
Do you mean I need to connect A5 port to both potentiometer and switch button (parallely)?

No use either the pot or the buttons.
Try this code with your buttons.

int value = 0;;
int maxval = 0;
int analogPin = A5;
void setup()
{
  Serial.begin(9600);
  pinMode(A5, INPUT);
}
void loop()
{
  value = analogRead(analogPin);
  if (value > maxval)
  {
    maxval = value;
  }
  Serial.print("Analog Input value = ");
  Serial.print(value);
  Serial.print ("   maxvalue = ");
  Serial.println(maxval);
  delay(500);
}

Tom.. :slight_smile:

No, you need to test with a potentiometer.

And I think that your have another flaw in your schematic; every button will result in 5V on the analogue pin when you press it.

Hi,
The Ops buttons are all grounded through the resistors and no 5V to apply potential difference?
Do you have a DMM, to measure your circuit?

Tom... :slight_smile:

UKHeliBob:
Sorry, my mistake, I copied the wrong program into my post

Try this

int max = 0;

const byte inPin = A0;

void setup()
{
 Serial.begin(115200);
 pinMode(inPin, INPUT);
}

void loop()
{
 int temp = analogRead(inPin);
 if (temp > max)
 {
   max = temp;
   Serial.print("max set to : ");
   Serial.println(max);
 }
}

The problem remains, here is a sample from the output:

max set to : 58
max set to : 127
max set to : 252
max set to : 483
max set to : 977
max set to : 1023

sterretje:
No, you need to test with a potentiometer.

And I think that your have another flaw in your schematic; every button will result in 5V on the analogue pin when you press it.

OK, but I have a 5-pin potentiometer (like this one http://hobbycomponents.com/1458-thickbox_default/ky-040-rotary-encoder-module.jpg) and I really don't know how to wire it.

Could you please explain why every button switch combination will result in 5V?

Thank you.

PaulS:
You seem too think that reading an analog pin with nothing (effectively) connected to it should return 0. That is NOT the case. Reading an analog pin with nothing connected to it will give you values that are all over the place.

Ah, ok thanks for this important note. I think that's my mistake. I have just updated my schematic.
What do you think of this: https://imgur.com/wCzCh5a?
Thank you.

OK, but I have a 5-pin potentiometer (like this one http://hobbycomponents.com/1458-thickbox_default/ky-040-rotary-encoder-module.jpg)

That thing in the picture is NOT a potentiometer. If that is what you really have, you can NOT read it like a potentiometer.

PaulS:
That thing in the picture is NOT a potentiometer. If that is what you really have, you can NOT read it like a potentiometer.

ok, what's then?

TomGeorge:
Hi,
The Ops buttons are all grounded through the resistors and no 5V to apply potential difference?
Do you have a DMM, to measure your circuit?

Tom... :slight_smile:

No I don't have a DMM.
Sorry I didn't understand. What do you mean by 'no 5V to apply potential difference'?

TomGeorge:
Hi,
You need to set something like this up to test your code properly;

Use a 10K potentiometer.
Connect the blue wire to A5;

Tom... :slight_smile:

Currently, I don't have a potentiometer. Is it required?

kaisbensalah:
ok, what's then?

The answer is right there in the picture's name.

PaulS:
The answer is right there in the picture's name.

Didn't notice that. understood now.

kaisbensalah:
Ah, ok thanks for this important note. I think that's my mistake. I have just updated my schematic.
What do you think of this: https://imgur.com/wCzCh5a?
Thank you.

can we just confirm one thing here coz this thread is suggesting you use a potentiometer to test your code but the schematic you posting is telling a different story!

  1. you want to connect 4 button switches to an anolog input for your project - YES/NO?

  2. if YES then am I correct to assume that u want to detect which button is pressed with your code - YES/NO?

if its YES again then your code and current circuit are both not fit for purpose! I would suggest a R-2R ladder. see here for some info. Resistor ladder - Wikipedia

Sorry I have come late to this discussion.

What the original poster is trying to do is quite straight forward, but he does have the wiring messed up (in my opinion) Your switch common connections need to be connected to the Arduino 0v, and the sense connection (analog pin) needs to be connected to the switch end if the first resistor, the other end if which needs to go to the 5v. [what you have currently is no source of voltage and variable resistance to ground.... that won’t work ]

When you do what I described, pressing each button should give you a fairly repeatable number on the serial display.

Using ohms law you should be able to predict what those numbers are.

When I do this, I calculate the resistor values to give me (approximately) even spacing, say 0, 200, 400, 600, 800, 1000. Then I take my analog input, divide by 200 and I then get a button number between 0 and 5.

If you did not have the foresight to calculate the resistor values like this, you will have to work a bit harder to separate the values, but I suggest a method that involves dividing by a constant as it reduces the impact of random variations due to the slightly different resistances of the button presses.