Arduino pins always HIGH

So I'm trying to create a random dice generator circuit that generates a different random number based on which button is pressed. The problem is the digital pins always return high. I have used pull down resistors to keep them grounded and have confirmed that they are at 0V with a multimetre. However the serial monitor keeps displaying random numbers no matter what. Any help would be much appreciated :)

Here's my test code:

int d4 = 2; // Corresponding to a 4 sided die long roll = 0;

void setup() {

Serial.begin(9600); pinMode(d4, INPUT); digitalWrite(d4, LOW);


void loop() {

if(digitalRead(d4) == HIGH) //If the button is pressed { roll = random(1, 5); //Generate the random number Serial.println(roll); //Print it delay(1000); }



Captain_Aubrey: digitalWrite(d4, LOW);

if(digitalRead(d4) == HIGH) //If the button is pressed

So you are setting a pin to LOW (i.e. Output 0V), then reading it (i.e. input). Little wonder it doesn't work right. Pick ONE: input or output. Use PinMode() to set it so in setup().

Also, read this:,148850.0.html. Please use code tags next time.

void setup() {

  pinMode(d4, INPUT);
  digitalWrite(d4, LOW);


try changing to:

void setup() {

  pinMode(d4, OUTPUT);
  digitalWrite(d4, LOW);


You can read an output pin, but you can't write to an input pin.

Good catch Jack. I was sorta thinking there, then got distracted and forgot what I was trying to say :)

I have used pull down resistors to keep them grounded

External pulldown resistors? The atmega328 does not have internal pulldowns.

Your Setup() code is fine, and operates like this:

  pinMode(d4, INPUT);     // Set pin 2 as input
  digitalWrite(d4, LOW);  // make sure pullups are disabled

You don’t NEED the digitalWrite, since pullups are disabled by default, but it shouldn’t hurt anything. But if you were expecting the write to enable internal pull-downs, you were wrong…

Using a variable like “d4” makes me really nervous, since my immediate interpretation was “pin digital 4 of the arduino”, but I don’t actually see any conflicts…

I beg to differ westfw.

"since pullups are disabled by default," A digital input that is not attached to a source, and pullups are disabled, will not give any consistent readings . It will be floating.

yes, but I meant that “digitalWrite(pin, 0);” has no effect on the state of internal pullups on an otherwise uninitialized system. Pullups are disabled before you issue that command, and they are still disabled after you issue that command.
Having no pullups enabled, and no external pull resistors, does leave the pins floating as you said, and this is a bad thing.

(and yes, the fact that all arduino pins are by default left as inputs without pullups enabled, is less than ideal.)

digitalWrite(pin, LOW);
will have no effect on that pin, unless it was previously declared to be an output pin.
pinMode(pin, OUTPUT);

but, digitalRead(pin); will work fine, even if it was never declared as an input pin.
so, it is not necessary to declare a digital pin as input, unless it had already been declared otherwise, and you want to read the signal from outside the processor.

I think we are both on the same track now.