digitalRead() not working with 12V PSU

Hi, I am working on a small home project that turns on an AC load for a specified amount of time, once a button is pressed.
Everything works as long as I power it with 5V through USB, however, when I power it through 12V VCC, the digitalRead(10) always pulled to HIGH.

I have no idea what could be causing this, but I did make a small discovery, when powered through USB (or VCC while USB plugged in), it works, and the voltage from Digital Pin 10 to GND (with nothing connected to the pin), is about 30mV.
But when testing the same thing while connected through 12V VCC, the pin to GND voltage is approx 1.5V, which seems to be triggering the digitalRead() to output HIGH at all times.

It's probably an issue with my wiring as I tried a few different pins and another Arduino, which produced the same results. I have attached the beadboard layout & schematic (the PSU shown is not the same one that I am using, however, it is also 12VDC).

Any help would be greatly appreciated!
Thanks,
Kaii

---------------------------CODE-------------------------------
int ButtonPress = 0;

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(10, INPUT);
}

void loop() {
digitalWrite(13, LOW);
ButtonPress = digitalRead(10);
Serial.println(ButtonPress);
if(ButtonPress == HIGH){
digitalWrite(13, HIGH);
delay(10000);
}
}

Capture.PNG

Capture.PNG

According to your diagrams there is nothing connected to pin 10. So why are you reading it?

Steve

If you connect 12V to Vcc you will destroy the Arduino instantly.

You diagram shows a supply correctly connected to Vin, not Vcc. Is that what you meant?

If an input is unconnected the result of digitalRead() on it is completely meaningless and
undefined - this is a floating pin.

Thanks for including the schematic and code!

When the button is not pushed the input is not connected to anything and it's "floating" to an unknown/unpredictable state.

The digital read serial example shows how to correctly wire your switch and pull-down resistor.

Or, the easiest solution is to enable the internal pull-up resistor which will pull the input high with no connection. Then wire the switch so it grounds the input when the button is pushed. (You'll have to reverse your software logic.)

I have no idea what could be causing this, but I did make a small discovery, when powered through USB (or VCC while USB plugged in), it works, and the voltage from Digital Pin 10 to GND (with nothing connected to the pin), is about 30mV.
But when testing the same thing while connected through 12V VCC, the pin to GND voltage is approx 1.5V, which seems to be triggering the digitalRead() to output HIGH at all times.

It's explained in here https://forum.arduino.cc/index.php?topic=719995.0. The pin is behaving exactly as it should.

Hi, I didn't realise the diagram was a bit outdated, as I was testing it with a few pins.
The button is indeed connected to pin 10, not 12.
Yes, when I wrote VCC I meant VIN.
Sorry for the mistakes, posted it very late last night.

DVDdoug, when I connect it in that way, I sometimes (approx every couple 100 reads) get a HIGH reading mixed in with the LOW, while not changing anything, this is when powered through USB.

PerryBebbington, will give it a try!

Thanks a lot,
Kaii

Thanks, PerryBebbington, using the internal pullup resistor works perfectly.

Normally the pullup resistor would be between 5V and the switch and the switch would short the pin to GND.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.