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

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.

Hi,
You need to set your buttons up like this, add 10K and the red link.
MAKE SURE YOU HAVE THE PRESS BUTTONS CONFIGURED CORRECTLY as in the image.


Can you post a picture of your project so we can see your layout please?

Tom... :slight_smile:

TomGeorge:
Hi,
You need to set your buttons up like this, add 10K and the red link.
MAKE SURE YOU HAVE THE PRESS BUTTONS CONFIGURED CORRECTLY as in the image.


Can you post a picture of your project so we can see your layout please?

Tom... :slight_smile:

Hi,

I don't have any button shipped with my card, so instead I bought a light switch button and plugged it on my circuit as shown in the picture below:

I tried to apply the design you mentioned in your schematic, but just for one button, as can be seen here:

I also used the following code for dumping the maximum voltage, as suggested by @UKHeliBob:

int max = 0;
const byte inPin = A5;

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

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

after verifying the code and uploading the binary to the card, the serial monitor shows me the maximum voltage value without different values, however even if the button is switched off, the same voltage is shown instead of 0. What's wrong here? And also why 10k resistor is needed?

Thank you.

Skip the useless Fritzing diagram, since you have a completely different kind of switch. Get a pen and some paper and draw a schematic.

It really isn't clear what you are trying to accomplish. But, it appears as though one of two things is happening.

Either the current goes from the 5V pin to the switch, to the resistor, and to the analog pin, in which case the voltage at the analog pin will still be 5V, or the current goes through either the switch, when turned on or the resistor when the switch is turned off, in which case the voltage at the analog pin will be 5V.

So, switch on or switch off, I would expect 5V at the analog pin, which results in a reading of 1023.

The numbers that you have allocated to the connections on the switch mean nothing.

Please provide more details, a link to the details of the switch that you are using or a schematic of the circuit. Hand drawn and photographed is OK.