I'm uploading this code to Arduino, interestingly I'm getting random values from analog and digital inputs. There is no problem with Arduino. I tried 3 different Arduinos and they all have the same problem. why is this happening? I am using Arduino 1.8.18
int buton=5;
int buton2=6;
int buton3=A0;
void setup() {
pinMode(buton, INPUT);
pinMode(buton2, INPUT);
pinMode(buton3, INPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(digitalRead(buton));
Serial.println(digitalRead(buton2));
Serial.println(analogRead(buton3));
delay(300);
}
Probably your inputs are floating.
Digital inputs needs to have pull-up or pull-down resistors.
Analog inputs needs to be attached to a voltage: 0 - reference voltage at all times.
Show your circuit.
b707
October 20, 2022, 8:39am
3
If nothing connects to your inputs - this behaviour is absolutely normal.
Do not use pinMode() on pins that you are only using for analogRead(). Use pinMode() with digitalRead() or digitalWrite().
Note that you can use
pinMode(buton3, INPUT_PULLUP);
This gives the pin a default high level without the need for an external resistor.
system
Closed
April 18, 2023, 1:13pm
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.