Turn on an LED by grounding an analog input pin.

I am working on a project for my car. I am trying to simplify some of the wiring. My plan is to use my Arduino to turn on a relay, using a relay board (pic of the board below). For the purposes of figuring out my code I am substituting the relay for an LED, I have the + side of the LED on on my bread board with a 10k resistor and I have the - side connected to Digital pin 2 of the Arduino. The relay board is ground activated, meaning the relay is turned on when the pin is grounded, and off when it is not. To minimize the amount of wiring in the car I will have a simple on/off switch. When the switch is on the wire is grounded and when the switch is off there is no ground or voltage. My idea is to have the switch ground pin A0 and have the Arduino ground digital pin 2.

I dont know how to tell the Arduino to do this, or if it is even possible to use a switched ground to A0 as an input.

Below is the code I have come up with so far, I have only gotten as far a defining the different inputs/outputs. I am not even sure if this is correct.

This is the first time I have ever attempted to write my own code. I have watched several YouTube videos and searched several different websites trying to find the answer. But its seems everyone uses 0-5v as an input and not ground.

I am starting with just the 1 circuit, the Fuel Pump. I plan on adding several more circuits once I get the code figured out.

Sorry for the crappy hand drawn wiring schematic.

Thank you for any help you can give me!

Here is the code I have come up with so far:

int fuelpumpPin=2;               //Pin that controls the FUEL PUMP relay
int ButtonfuelpumpPin=A0;   //Pin that turns on the fuel pump

void setup() {
pinMode(fuelpumpPin,OUTPUT);
pinMode(ButtonfuelpumpPin, INPUT);
}

void loop() {

//Switch is turned ON and grounds A0
if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}   


//Switch is turned OFF and the is no ground or voltage to pin A0
else {digitalWrite(ButtonfuelpumpPin,HIGH);}


}

9665_2_5523.jpg

But its seems everyone uses 0-5v as an input and not ground.

0V is ground.

Please remember to use code tags when posting code.

if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}  Have you ever seen any code like that in the plentiful examples in the IDE?

So does that code compile? Does it work?

You clearly know how to post images, so go back to that first post and mark up your code as code using the "</>" widget.

Now one particular concern - how are you going to supply 5 V to the Arduino and relay board (and which one are you using? A Nano would be best for this project, UNOs are not useful for "real world" applications).

AWOL:
0V is ground.

Please remember to use code tags when posting code.

if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}

Have you ever seen any code like that in the plentiful examples in the IDE?

Sorry about that. I fixed the code tags.

I have seen several examples similar to that.

If I put something like

 (ButtonfuelpumpPin, 0){digitalWrite(fuelpumpPin,0);

That is telling the Arduino when it sees 0 on A0 to ground Pin 2?

Paul__B:
So does that code compile? Does it work?

You clearly know how to post images, so go back to that first post and mark up your code as code using the "</>" widget.

Now one particular concern - how are you going to supply 5 V to the Arduino and relay board (and which one are you using? A Nano would be best for this project, UNOs are not useful for "real world" applications).

Sorry about that. I fixed the code tags.

The code compiles, but it doesn't do what I want it to do. The LED does not turn on when I ground A0. So I know I have something wrong in the code. If I connect the - side of the LED to ground (without the Arduino) it turns on. So I know it the code and not the wiring or the LED.

I planned on powering the Arduino and the Relay board from a fused 12V source on the car with a DC-DC step down voltage converter like this one:

I am prototyping my idea with an UNO. Once I have the bugs worked out I'll probably us an Arduino Micro without headers and solder in the connections.

I have seen several examples similar to that.

Really?

Show me one that has something that looks anything like this line?

if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}

Delta_G:
Really?

Show me one that has something that looks anything like this line?

if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}

Are you asking so you can try and help? As I stated in my post I have never attempted to write code. The learning curve is pretty steep and overwhelming. I have looked at a few dozen examples and tried to modify those examples to suite my needs. Obviously I got it wrong.

I think I figured it out.

Instead of:
if (ButtonfuelpumpPin, LOW) digitalWrite(fuelpumpPin,LOW);

should it be?:
analogRead(ButtonfuelpumpPin); //Tells the Arduino to read Analog pin A0
if (ButtonfuelpumpPin, LOW) //If A0 is grounded
digitalWrite(fuelpumpPin, LOW); //Digital Pin2 is grounded
if (ButtonfuelpumpPin, HIGH) //If A0 is not grounded
digitalWrite(fuelpumpPin, HIGH); //Digital Pin2 is grounded

int fuelpumpPin=2;               //Pin that controls the FUEL PUMP relay
int ButtonfuelpumpPin=A0;   //Pin that turns on the fuel pump

void setup() {
pinMode(fuelpumpPin,OUTPUT);
pinMode(ButtonfuelpumpPin, INPUT);
}

void loop() {

//Switch is turned ON and grounds A0
if (ButtonfuelpumpPin, LOW){digitalWrite(fuelpumpPin,LOW);}   


//Switch is turned OFF and the is no ground or voltage to pin A0
else {digitalWrite(ButtonfuelpumpPin,HIGH);}


}

OK no. You are still missing it. What I was trying to get you to do was to go look at the examples.

Your setup is OK maybe, depending on how you wired your button. If you don't want to have to use a resistor you can use INPUT_PULLUP but then you have to make sure you wire the button to go to ground when you close it.

An if statement has to have a valid expression in it. Not two things with a comma jammed in there. This is where you need to look at some examples.

See if that helps. Notice how they read the pin and save that state into a variable. You don't want to compare the pin number to LOW, you want to compare what you got from digitalRead.

You can use A0 as a digital pin too by the way. The analog function is something extra that it does. But it is also just a plain old digital pin. Just make sure to call it 14 or A0 and never just 0 cause that will go to digital pin 0.

mustangman570:
As I stated in my post I have never attempted to write code. The learning curve is pretty steep and overwhelming. I have looked at a few dozen examples and tried to modify those examples to suite my needs. Obviously I got it wrong.

Clearly. :grinning:

OK, look, I cannot stand the suspense. I haven't really used "C" much if at all before I started playing with the Arduino around 2013. I generally have preferred assembler - it is mostly easier; I know what I want it to do and the steps to do it.

However let's look at "C". Your "C".

int fuelpumpPin=2;               //Pin that controls the FUEL PUMP relay
int ButtonfuelpumpPin=A0;        //Pin that turns on the fuel pump

OK, these numbers are the address of a pin.

void setup() {
pinMode(fuelpumpPin,OUTPUT);
pinMode(ButtonfuelpumpPin, INPUT);
}

You set the pins (Data Direction Register) as outputs and inputs. No problem

Inside a loop. OK, "ButtonfuelpumpPin" is the pin we want to read. To read it,

digitalRead(ButtonfuelpumpPin)

Don't get confused. We do not want to read an analog value, we want to know whether it is HIGH or LOW, so we do a digitalRead.

OK, you have read it. It is either HIGH or LOW. In this case, you want to know whether it is LOW. So the question is, is

(digitalRead(ButtonfuelpumpPin) == LOW)

We compare one thing with another. That comparison is a value - a Boolean value which is either true or false.

So we want to make a decision on that value - we use an "if".

if (digitalRead(ButtonfuelpumpPin) == LOW)

And if it is, we want to do something to the fuelpumpPin, we want to write it LOW.

digitalWrite(fuelpumpPin, LOW)

So

if (digitalRead(ButtonfuelpumpPin) == LOW) digitalWrite(fuelpumpPin, LOW);

That would be a complete statement, ended with a ";". But you do not want to end it there because you have an alternative, an "else" which is

digitalWrite(ButtonfuelpumpPin, HIGH)

so you add that before the ";".

if (digitalRead(ButtonfuelpumpPin) == LOW) digitalWrite(fuelpumpPin, LOW)
    else digitalWrite(ButtonfuelpumpPin, HIGH);

So there you go!

OK, these numbers are the address of a pin.

Nope.
They are variables which identify pins.

Whatever ...

Paul__B:
Whatever ...

So, following your description, I should be able to write byte x = *A0;

Oops.

if (digitalRead(ButtonfuelpumpPin) == LOW) digitalWrite(fuelpumpPin, LOW)
    else digitalWrite(ButtonfuelpumpPin, HIGH);

Similarly, oops.

I GOT IT! I GOT IT! I GOT IT!

I stumbled across this example and modified it to suit. It didn't require much modification.

void setup() {
Serial.begin(115200);
pinMode(A0, INPUT_PULLUP);    //Fuel Pump Input
pinMode(2, OUTPUT);                //Fuel Pump Output
}

void loop() {

int sensorVal1 = digitalRead(A0);   //Read Fuel Pump Input

Serial.println(sensorVal1);
if (sensorVal1 == HIGH){digitalWrite(2, HIGH);}
else{digitalWrite(2, LOW);}

}