Kindly, im writing a code for my voltage divider control circuit .....i have :
analog input pin A0 (signal +1v or -1v or 0v) square wave
two digital outputs (+ 5v or 0v) for each one ... so i hv 4 states 0 0,0 1,1 0,1 1
Now :
if A0 = 0 v =====>output pins = 0 0
if A0 = +1 v =====>output pins = 1 1
if A0 = -1 v ======>output pins =1 0 or 0 1
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
AnalogRead will never return a negative number. If you try and feed -1V into an analog pin you will damage the Arduino.
Secondly, there is always a margin for error with ADC's. The value will not reliably be 1000, for example it may be 997, 999, 1003 - You should allow a range of numbers to be included.
Next, where did 1000 come from? The analogRead value returns a number between 0 and 1023 which represent equal steps between 0v and AREF. If you are using the default reference voltage then 1023=5v, 0=0v, and approximately 204=1v.
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
void setup () {
Serial.begin(9600);
pinMode(dig1_out, OUTPUT);
pinMode(dig2_out, OUTPUT);
delay (5000);
}
void loop () {
int value=analogRead(analog_in);
if (value >= 0) {
if (value <= 5) { // less than or equal 200 mv
digitalWrite(dig1_out,LOW);
digitalWrite(dig2_out,LOW);
} }
else if (value <= 209) { //about 1 v
if (value >= 200) {
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,HIGH);
} }
else if (value < 0){ // here is the problem right ?? i need -1 v what i do pls?
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,LOW);
}
delay(20);
}
You will find that the way you have built your if-elseif statements won't work quite as you expect:
if (value >= 0) {
//value is always greater than zero, so code always reaches here
if (value <= 5) {
...
}
} else if (value <= 209) { //about 1 v
//The first if statement is always true, therefore this bit of code never executes.
if (value >= 200) {
...
}
}
You are along the right lines though. You just need to consider what you are looking for. For the first if statement you want the value to be >=0 AND <= 5. So based on that you get this:
if ((value >= 0) && (value <= 5)) {
... //will execute if the value meets both conditions
} else if ((value >= 200) && (value <= 209)) { //about 1 v
...
}
The problem of negative values however still remains. What you need to do is to take your +/-1v signal and add a bias so that it is within the range of the ADC.
There is one such method which uses just two equally sized resistors. I have attached a schematic.
What the circuit does is att 2.5v onto the signal meaning that when you have -1v, the ADC sees 1.5V, when you have +1v, the ADC sees 3.5v.
How successful this method will be depends on what is driving the +/-1V signal.
Perhaps you could use a multimeter and a simple analog-to-serial sketch, so you can directly check voltage and analog reads.
There should be an example in the ide, anyway it's as simple as:
void setup() {
Serial.begin(9600); // or whatever speed you prefer
}
void loop() {
int value;
value = analogRead(0); // use the correct number for your hardware
Serial.println(value);
delay(1000);
}
Upload and open the serial monitor, then keep watching the multimeter readings and the serial values on the screen.