Voltage sensor - AnalogIN - Threshold

Hello,

I’ve a quick question conserning a rather simple circuit. It’s just that this is my first Arduino project and I cannot seem to find my mistake. My main problem is the measurement of a voltage. This voltage triggers an event, if it falls below a defined threshold.

I’ve built the following circuit:

http://www.fotos-hochladen.net/uploads/nanosteckplati76l38abne2.jpg

Don’t get confused, there isn’t really an LED, but a piece of metal wire in lye. The current causes the wires’ diameter to decline due to a chemical reaction.

What it should do:
The applied 7,5 V DC-Voltage causes a current in the wire that decreases its diameter. Therefore the resistance changes. If the voltage drops below a defined threshold, the Arduino turns on the relay which interrupts the circuit.

What’s the problem:
I seem to have wired the measuring resistance wrong. The board fails to compare the measured voltage with the threshold, i.e. the relay is never turned on.

Here is my code, although the relay works if I turn it on by default and skip the comparison, so I tend to think it’s a problem with the wires.

const int analogOutPin = 9;         // relay-pin
const int analogInPin = A0;         // V(in)
float threshold = 15/256;           // threshold
int led = 13;                              // board lamp

void setup () {
  Serial.begin(9600);                             // bits per sec
  pinMode(analogOutPin, OUTPUT);       // def pin as output
  pinMode (analogInPin, INPUT);          // def pin as input
  pinMode(led, OUTPUT);                  // lamp
}


void loop () {
  
  int sensorValue = analogRead(analogInPin);  // Analog Read incoming voltage signal
  float voltage = sensorValue*(5.0/1023.0); // calculate a voltage value from signal
  
  if(voltage < threshold) {  
    delay(5000);  // wait 5secs
    digitalWrite(analogOutPin, HIGH); // tunr on relay
  }
 else {
  digitalWrite(led, HIGH);   // turn on lamp
  delay(100);                // wait
  digitalWrite(led, LOW);    // the blinking is just a sign the voltage is not below the threshold
 
  }
}

I appreciate your help :slight_smile:
Thank you

Marcus

Hello JohnJohn Marcus,

I think that the wiring is ok. Have you tested with a voltmeter? When you connect the voltmeter in parallel to your 82 Ohm resistor you see the actual voltage that the arduino also sees. Then simply read from analog in and output to serial. Now you see if your hardware and analog read does what you suppose it to do.

Hope this helps

Elektrix

Thanks you were right, the wiring is ok. I measured the voltage with a multimeter and the value corresponds with my powersupply. I tried the idea with serial.println with all variables (voltage, threshold, sensorValue), yet I get 0.0 for all three. This is strange. I'm going to test different values and variable types, maybe my calculation for the threshold isn't right, either. This value comes from a priviously used microcontroller board, that only had 256bits of resolution.

Thanks, I will keep you posted and ask more, when I'm done with all options ;)

When your hardware is ok then read Analog In and send the value directly to serial. This way you will see if the input is correct (and the signal really is connected to Analog In 0).

Hope this helps

Elektrix

  1. As that circuit stands, you are likely to damage the Arduino microcontroller by putting more than +5V into the analog input pin. You may have already damaged it, if you are lucky just that one pin. Connect a 10K resistor in series with the analog input (i.e. between the input pin and the junction of the 82 ohm resistor and your "led") to avoid that.

  2. What relay are you using? Most relays take too much current to be driven directly from an Arduino output pin. Again, you may have damaged the pin.

I took your advice and installed the high resistance, just in case. I am measuring the voltages and there only remains about 1V although 7.5V are applied.

I used an extra low power relay, that was suggested in another forum. I inserted a transistor (2N2222) between the arduino and the relay, so there are no peaks that could damage the board.

My mistake was to declare the voltages as double and just give them integer values, so everything just went zero. I corrected that mistake and now everything works fine. The serial monitor helps keeping track of the measurements (again, not above 1.0V) and when the threshold is reached, the relay clicks and shuts everything down. The metal wires have the expected shape. So thanks a lot to everyone!

I see it now:

float threshold = 15/256;

will mean that threshold is zero.

dc42: 1. As that circuit stands, you are likely to damage the Arduino microcontroller by putting more than +5V into the analog input pin. You may have already damaged it, if you are lucky just that one pin. Connect a 10K resistor in series with the analog input (i.e. between the input pin and the junction of the 82 ohm resistor and your "led") to avoid that.

  1. What relay are you using? Most relays take too much current to be driven directly from an Arduino output pin. Again, you may have damaged the pin.

Dc42, won't adding a 10K resister change the voltage value significantly? Please excuse me if I am incorrect, ohms law and electronics are not my specialty, I'm more of a coder.

B1un7me: Dc42, won't adding a 10K resister change the voltage value significantly? Please excuse me if I am incorrect, ohms law and electronics are not my specialty, I'm more of a coder.

Okay dc42, I've just tried this on my breadboard... And it does NOT effect the voltage! So maybe a better question for me is why?

FYI I'm creating a low voltage detector fir an arduino to test a 1S lipo falling below 3.2v to alert user it needs to be charged. This circuit seems to do just that (assuming I set a pin high (I.E. Pin 6) to an LED when the measured pin drops to below 3.2)