Hardware Adruino UNO
SainSmart 2 Channel relay board.
I am trying to read 5VDC on analog pin(s) 0 & 1 (not at the same time) in order to activate a Sainsmart relay board. Do I need the External Reference? I am using the LED Pin13 just for testing. I plan to use digital pins 8 & 9. I have to set the output pins high in order to not engage the relay when power is applied to the UNO. What am I doing wrong? Any help would be greatly appreciated.
int relayPin1 = 13; // Using LED Pin 13 for testing.IN1 connected to digital pin 7
int relayPin2 = 9; // IN2 connected to digital pin 8
int sensorPinA0 = analogRead(A0);
int sensorPinA1 = analogRead(A1);
int sensorValue = 0; // Sensor Value
void setup() {
pinMode(relayPin1, OUTPUT); // sets the digital pin as output
pinMode(relayPin2, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
analogReference(EXTERNAL);
}
void loop() {
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
sensorValue = analogRead(sensorPinA0); // read the value from the sensor:
digitalWrite(relayPin1, LOW);
sensorValue = analogRead(sensorPinA1); // read the value from the sensor
digitalWrite(relayPin1, LOW);
Here you are performing a analog read of A0/1 and storing the result in sensorPinA0/1
int sensorPinA0 = analogRead(A0);
int sensorPinA1 = analogRead(A1);
This is what I think you intended:
int sensorPinA0 = A0;
int sensorPinA1 = A1;
But as Rob mentioned your sketch must be incomplete because you never use sensorValue to do anything.
How about something like this:
sensorValue = analogRead(sensorPinA0); // read the value from the sensor:
if (sensorValue > 500){
digitalWrite(relayPin1, LOW)
}
else{
digitalWrite(relayPin1, HIGH)
}
analogRead returns a number between 0-1023 I chose 500 as the threshold to activate the relay
ramjdmcmm:
Hardware Adruino UNO
SainSmart 2 Channel relay board.
I am trying to read 5VDC on analog pin(s) 0 & 1 (not at the same time) in order to activate a Sainsmart relay board. Do I need the External Reference? I am using the LED Pin13 just for testing. I plan to use digital pins 8 & 9. I have to set the output pins high in order to not engage the relay when power is applied to the UNO. What am I doing wrong? Any help would be greatly appreciated.
int relayPin1 = 13; // Using LED Pin 13 for testing.IN1 connected to digital pin 7
int relayPin2 = 9; // IN2 connected to digital pin 8
int sensorPinA0 = analogRead(A0);
int sensorPinA1 = analogRead(A1);
int sensorValue = 0; // Sensor Value
void setup() {
pinMode(relayPin1, OUTPUT); // sets the digital pin as output
pinMode(relayPin2, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
analogReference(EXTERNAL);
}
void loop() {
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
sensorValue = analogRead(sensorPinA0); // read the value from the sensor:
digitalWrite(relayPin1, LOW);
sensorValue = analogRead(sensorPinA1); // read the value from the sensor
digitalWrite(relayPin1, LOW);
And it appears these two lines ...
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
Remove the external reference, unless you have an external reference connected. You do not need it for typical tasks.
Your problem I think is:
int sensorPinA0 = analogRead(A0);
int sensorPinA1 = analogRead(A1);
I don't even know what the compiler does with that, since it's outside of any function.... No matter what it's doing with it, there's no way it's what you want.
You could do (as suggested above)
int sensorPinA0 = A0;
int sensorPinA1 = A1;
But if you're using names like sensorPinA0 - why bother defining that name? When you want to get the value, just analogRead(A0).
The practice of giving names to pins is mainly so that if, say, you have a temperature sensor on pin A0, you say int temperaturePin=A0; - and then refer to temperaturePin when you want to read it - and then you decide that you need to move the temperature sensor to pin A1, you only need to change that one line at the top of the code, rather than digging through your code for every place you used that pin.
I entered the omissions and corrected the text. I got rid of the EXTERNAL reference. Now when 5 VDC is applied to A0 or A1 both pins 8 & 9 go LOW. How do I get them to work independently? I need to know this for my next project. May as well ask now.
int relayPin1 = 8; // Using LED Pin 13 for testing.IN1 connected to digital pin 7
int relayPin2 = 9; // IN2 connected to digital pin 8
int sensorPinA0 = A0; // 5VDC in
int sensorPinA1 = A1; // 5VDC in
int sensorValueA0 = 0; // Sensor Value
int sensorValueA1 = 0; // Sensor Value
void setup() {
pinMode(relayPin1, OUTPUT); // sets the digital pin as output
pinMode(relayPin2, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
}
void loop() {
sensorValueA0 = analogRead(A0); // read the value from the sensor:
if (sensorValueA0 > 500)
digitalWrite(8, LOW);
else
digitalWrite(8, HIGH);
sensorValueA1 = analogRead(A1); // read the value from the sensor:
if (sensorValueA1 > 500)
digitalWrite(9, LOW);
else
digitalWrite(9, HIGH);
}
If you're just applying +5v or 0v to the pins, just use digitalRead() - it's faster and compiles smaller. But if you need to switch in the middle at a ~2.44v, then analogRead() is the right tool for the job.
If a pin is "floating" (ie, is set as input and nothing is connected to it) it will read randomly due to noise picked up from the environment. You can make it change by waving your hands around the pin, in fact
If you analogRead a pin with something on it, then analogRead a floating pin, the floating pin will normally take on a value similar to the last analog value read.
To provide meaningful results you always need something connected to a pin that you're reading - either internally (by setting it to OUTPUT which connects the output drivers to it, or INPUT_PULLUP, which connects a ~20k pullup "resistor" to it), or externally. If it's a button, normally you want to have pressing the button connect it to ground, and set the pin INPUT_PULLUP - you can have the button connect to 5v when pressed, but then you need a pulldown resistor (10k is a good default value) from the pin to ground to keep it at 0 when not pressed.
The easy fix for that is to read each sensorPin twice.
Like this:
sensorValueA1 = analogRead(A1); // read the value from the sensor:
sensorValueA1 = analogRead(A1); // read the value from the sensor agian
if (sensorValueA1 > 500)
digitalWrite(9, LOW);
else
digitalWrite(9, HIGH);
Of course if your sensors provide only 0 or 5volts you should use digitalRead as pointed out above.
If it is an analog signal then you will have to adjust the 500 i used as an example to set the switching point as needed.
If you had used code tags (the </> symbol at the top left when you use reply NOT quick reply)
as suggested I could of copied your code to test it, but as is I would need to do to much editing
I tried the digitalRead function for A0 & A1 with no luck. The output pins 8 & 9 do not change state (from high to low).The only this thing that almost works in analogRead for A0 & A1. I have a one 10k resistor on A0 tied to ground and one 10k resistor on A1 tied to ground. When 5VDC is applied to A0 or A1 both the outputs 8 & 9 are set low (which is correct in this case). Both input pins are being read twice as suggested.
Any suggestions are welcome.
int relayPin1 = 8; // IN1 connected to digital pin 8
int relayPin2 = 9; // IN2 connected to digital pin 9
int sensorPinA0 = A0; // 5VDC in
int sensorPinA1 = A1; // 5VDC in
int sensorValueA0 = 0; // Sensor Value
int sensorValueA1 = 0; // Sensor Value
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(8, OUTPUT); // sets the digital pin as output
pinMode(9, OUTPUT); // sets the digital pin as output
digitalWrite(8, HIGH); // Prevents relays from starting up engaged
digitalWrite(9, HIGH); // Prevents relays from starting up engaged
}
void loop() {
sensorValueA0 = analogRead(A0); // read the value from the sensor:
sensorValueA0 = analogRead(A0); // read the value from the sensor:
if (sensorValueA0 > 500)
digitalWrite(8, LOW);
else
digitalWrite(8, HIGH);
sensorValueA1 = analogRead(A1); // read the value from the sensor:
sensorValueA1 = analogRead(A1); // read the value from the sensor:
if (sensorValueA1 > 500)
digitalWrite(9, LOW);
else
digitalWrite(9, HIGH);
}
int relayPin1 = 8; // IN1 connected to digital pin 8
int relayPin2 = 9; // IN2 connected to digital pin 9
int sensorPinA0 = A0; // 5VDC in
int sensorPinA1 = A1; // 5VDC in
int sensorValueA0 = 0; // Sensor Value
int sensorValueA1 = 0; // Sensor Value
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(8, OUTPUT); // sets the digital pin as output
pinMode(9, OUTPUT); // sets the digital pin as output
digitalWrite(8, HIGH); // Prevents relays from starting up engaged
digitalWrite(9, HIGH); // Prevents relays from starting up engaged
}
void loop() {
sensorValueA0 = analogRead(A0); // read the value from the sensor:
sensorValueA0 = analogRead(A0); // read the value from the sensor:
if (sensorValueA0 > 500) {
digitalWrite(8, LOW);
} else {
digitalWrite(8, HIGH);
}
sensorValueA1 = analogRead(A1); // read the value from the sensor:
sensorValueA1 = analogRead(A1); // read the value from the sensor:
if (sensorValueA1 > 500){
digitalWrite(9, LOW);
} else {
digitalWrite(9, HIGH);
}
}
I see nothing that would explain that... (above code adds brackets - but I don't think it changes the effect here - I just don't like the ambiguity created by if/else statements... I'd be inclined to suspect flaky connections to one of the pulldowns, or the inputs being connected to eachother.
Here is the circuit. I all so attached it. I have tried 9 VDC power, and just the USB power to the Adruino board. There is no difference in the results.