Weird analog reads

Hi, I am trying to make a simple circuit. The goal is to be able to make an interface with two buttons and detect which one is pressed with only one pin (two if counting 5v). I know it is a really simple task if I only made two individual circuits but I wonder why I can't do it.

The idea is to use two buttons on one pin. Parallel to each of them is a resistor. These resistors have to be different to be able to read which button is currently pressed. In my head, the circuit would go through both buttons when nothing is done, therfore reading about 5v or just 1023. If, say, the first button is pressed, the circuit would have to go through the first resistor and through the other button. Thereby reducing the analogRead. If the other button is pressed the opposite would happen.

When this is done, I get eighter 1006-1023 constantly, or the volts will just jump around between 0 and 1023. What is wrong?

This is just the code I used, nothing special at all, but it read volts so I guess nothing more is really needed at this point.

int sensorValue = 0;
int sensorPin = A1;
void setup()

{
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  
  Serial.begin(9600);
  
}


void loop()

{
  
  Serial.println(analogRead(A1));
  delay(500);
 /* sensorValue = (analogRead(sensorPin));
  float spenning = sensorValue * (5.0/1023); 
  Serial.println(spenning);
  delay(500);
  */
}

Henrik

PS. The fritzing file shows a pin to "5v" while the code says 13 - I tried them both with more or less the same results.

styresystem.fzz (3.38 KB)

Sounds like a floating input.
Post a schematic.

There was a fritzing file in my post, isn't that adequate? :cold_sweat:

HenrikAT:
There was a fritzing file in my post, isn't that adequate? :cold_sweat:

No it is not a schematic is it? It is a physical layout diagram and as such useless for seeing what your actual circuit is.
Most people will not have it on their machine because of how bad it is. So not everyone can read it.

There appears to be a rule here in that when ever anyone says something is weird it turns out to be perfectly normal.

In fritzing it says that the file can be seen as a schematic as well, do you mean another type of schematic? :~
if that is the case, how do I do that?

Screen dump, png, gif, something that people can read.

Liek this?

Close but no coconut.
A screen dump of the schematic that fritzing will give you is what is needed at the very least.

However I can say the circuit will not work at all as there is no ground, so you will either get a floating input or 5V depending on what buttons are pressed.

Ok, so AWOL's nick was correct once again...

Where is the ground supposed to be though?

I honestly think my fritzing-file is suicidal. It crashed my PC twice and was close to impossible to manipulate, but here you go, ugly as it might be =(

Make R2 10K.
Wire another 10K from A0 to ground.

Great, I didn't have more than one 10K but a 27K as R2 and to ground worked fine. Stable readings!!

R1 is 10k and now I can actually read in the Serial monitor which button I am pressing :wink: thank you very much! I promise to post screenshots of schematics on first post from now on 8)

I promise to post screenshots of schematics on first post from now on

Excellent

The original circuit just connected A0 to +5V (in an unnecesarily complex way)!

I'd recommend learning to read and hand-draw schematics, its the language of
electronics and its really useful.

A circuit diagram is there to denote the electronic function of each component,
not what its physical package looks like - that's a wiring diagram. Circuit diagrams
have rules - supply at the top, ground at the bottom, signal flow left->right, all
lines horizontal or vertical.

Ok, would you like to provide a recommendation as to which programs to use, or is it best to draw them by hand?

I would start off drawing them by hand. I up just use a general purpose 2D vector drawing package. It is the most flexible.

Thank you guys!!!

to Henrik. se dette bildet: (12 knapper på en inngang)
https://dl.dropboxusercontent.com/u/43081398/12knapper.png