digitalRead not working or am i doing something wrong?

Hi, I am currently making an arduino based switch for my raspberrypi which is currently being used as a print server for an older printer that I have. I am making this switch so that when the printer powers on it gives power (5 Volts from the printer's USB port) to the Arduino's D15 pin which then sends a power impulse to relay (on D5), making it send power to the raspberrypi's gpio pin for a second, simulating a button. My problem here is that when I send the 5V to the arduino's D15 port nothing happens.

Here is my code:
(Excuse me if it's not very good, I am a bit of a newbie).
<
#define POWERPIN 15
#define RELAY 5

void setup() {
// put your setup code here, to run once:
pinMode(RELAY, OUTPUT);
pinMode(POWERPIN, INPUT);

if (digitalRead(POWERPIN == HIGH))
{
digitalWrite(RELAY, HIGH);
delay(1000);
digitalWrite(RELAY, LOW);
}
}

void loop() {
// put your main code here, to run repeatedly:

if (digitalRead(POWERPIN == LOW))
{
digitalWrite(RELAY, HIGH);
delay(1000);
digitalWrite(RELAY, LOW);
}
}

Did I make a mistake in the code or is feeding 5 Volts directly into the arduino wrong?

Thanks in advance!
Alex

Please post the schematics, use the autoformat in the IDE and code tags, </>, when pasting the code.

Do you have a common Ground between all components?

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Yes. You did not post your code in code tags.

if (digitalRead(POWERPIN == HIGH))

You probably meant

if (digitalRead(POWERPIN) == HIGH)
2 Likes

Sorry sorry, I'll correct it now

is it good now?

You can check the digital read with the Digital Read Serial Example

And you can check your output & relay with the Blink Example

(Of course, change the code to match the I/O pins you're actually connected to.)

BTW - There's no need to duplicate your "logical code" in setup(). It looks like the default/resting state is low, so you can set the output low during setup().

You can also take-out the comments that that say - // put your setup code here, to run once: and // put your main code here, to run repeatedly: Those are notes to you before you start programming. Any comments should reflect what your code is actually doing..

Thanks a lot!

By the way, sorry I haven’t posted the schematic yet, but I just realized I made a really stupid mistake in the hardware that might actually be the cause to why it isn’t working. Thank you @DVDdoug for the blink test recommendation and everyone else for your replies! If it still doesn’t work I’ll try the blink test and if after that it still doesn’t work I’ll let you know. FYI, I’m doing this tomorrow as it’s almost 23:00 and I need to sleep tonight as tomorrow I need to go to work. Thanks again everyone, Alex

The code is wrong too (reply #6)
Do you have a pull-down resistor on the switch input?

You’re right sorry.

Yes, thanks for the tip though.

No, please tell me more.

Your code implies that the switch is connected between the input pin and the supply.
If the switch is closed, then the pin will read HIGH.

This does not mean that if the switch is open it will definitely read LOW, unless you have a pull-down resistor wired between the pin and GND.

Alternatively, you can wire the switch between the input pin and GND, and use INPUT_PULLUP, and invert your logic, so a closed switch reads LOW.

1 Like

Thanks, I’ll look into it

The hardware issue that I mentioned yesterday was that I wired my transistor (the one controlling the relay) wrong. I managed to get the relay to click but my code still isn’t working. Thanks @anon73444976 for the pull-up tip, once again, I’ll look into it tomorrow. Thank you! Alex

@anon73444976
So basically when I define D15 as an input, I do this:

<pinMode(15, INPUT_PULLUP);>

Instead of how I did it now without the pull-up?

Thank you very much for your time and patience,
Alex

@UKHeliBob Actually, no, because if I try and compile it with 1 bracket it gives me an error. Still thanks.

Alex