This is a VERY basic newbie question so I appreciate your patience and understanding in that context. I'm trying to figure out the code to monitor voltage on A0 and if 5v is detected on A0, output 5V on A4.
So far, I have the following I scraped from the Arduino community:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
What is confusing is that with the above, the serial monitor shows A0 at 1.52 with no input - nothing physically pinned into A0. What am I doing wrong and how do I get the if/then properly added to the code above to output 5v on A4?
You have a floating input q.v. Try connecting A0 to ground or 5V and run your code.
Why A4 particularly? You can treat it as a digital pin if you want, but right now you aren't using the regular digital pins for anything so if you want a 5V output, one of them will do the same thing.
Thanks for the replies - even the sarcastic humor. So what I'm trying to do, and I suppose I should have opened this thread by better describing is:
Presently I have a setup that involves a control device that sends a 5v signal to a pump when the pump needs to turn on. What I'd like to do is intercept the 5v signal from the control device and send it to multiple pumps so that I don't need a 1:1 controller to pump ratio.
Then you may not need the analog pins at all. digitalRead and digitalWrite will likely be sufficient. Do remember though that while the Arduino can read a 5V and 0V signal, it won't be able to drive a pump directly. You will need a power supply for the pump and suitable electronics (or relays) to let the Arduino control it.
Thanks wildbill for the help and insight. I'm just trying to use the Arduino to send 5v signals to multiple devices - not power them. I have revised my code but I'm still not seeing the expected results. When I apply 5v to digital pin 2 I see a 1 on the serial output and I'd expect to see digital pin 4 return a 1 but it doesn't. digital pin 4 stays 0.
int pump = 2; // pump connected to digital pin 0
int manf = 4; // output 5v to manifold
int val = 0; // variable to store the read value
int manval = 0; // pin 4 value
void setup() {
pinMode(manf, OUTPUT); // sets the analog pin 4 as output
pinMode(pump, INPUT); // sets the analog pin 0 as input
Serial.begin(9600); // open the serial port at 9600 bps:
}