Processing 2 Analogue Inputs To Create Differential Value

First of all, thankyou for spending your time looking at this thread.

I have 2 analogue inputs (happen to be 10k potentiometers, for now)

I require to calculate the difference between the two.

I am aware I need a delay function to do this. (To allow comparative values, at a given point in time.)

So that overall I have differential values between the potentiometers.

Please could someone explain how to do this?

Apologies for being a programming noob.

From this differential value I will push the values into a LED bar for a visual difference.

Many thanks.

I am aware I need a delay function to do this.

No you don't.
Just read the two different channels, it is as easy as that.

Thanks Mike.

Much appreciated.

This is probably obvious, but just in case it isn't.

I could imagine if you are using a delay to do a debounce, that you would want to read both values first, then do the delay, and read them again and compare to the originals. Then just subtract. You probably don't want to do read first, delay, read first again, read second, delay, read second again.

Is the problem that the signals might be changing while you read them? Something like

int chan_diff ()
{
  int ch1 = analogRead (pin1) ;
  int ch2 = analogRead (pin2) ;
  ch1 += analogRead (pin1) ;
  ch1 >>= 1 ;
  return ch1 - ch2 ;
}

will compensate for the readings being taken at different instants (about 100us per sample).

I've read posts about allowing time for the ADC to switch and read the value of a different pin, does this still need to be put into consideration or is it handled internally? For example, if it were still relevant, you may find you get more stable readings from the following:

int chan_diff ()
{
  analogRead (pin1) ;
  delay (10) ;
  int ch1 = analogRead (pin1) ;
  analogRead (pin2) ;
  delay (10) ;
  int ch2 = analogRead (pin2) ;
  ch1 += analogRead (pin1) ;
  ch1 >>= 1 ;
  return ch1 - ch2 ;
}

Many thanks all,

I've implemented a lot of what has been discussed.

Kind regards,

NathanNZ:
I've read posts about allowing time for the ADC to switch and read the value of a different pin, does this still need to be put into consideration or is it handled internally?

That's only an issue if the source impedances are high - if 10k or so or less, not an issue at all.

Always, always always, model make and years(rev).

If you don't tell us what you have you will always get answers whoich might work -- but might not be the best or what you want. Maybe they are what you want. How would you know?

This is from the Mega 2560 Manual -- a comparator is built in. Surprise!

The Analog Comparator compares the input values on the positive pin AIN0 and negative pin
AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin
AIN1, the Analog Comparator output, ACO, is set. The comparator’s output can be set to trigger
the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separate
interrupt, exclusive to the Analog Comparator. The user can select Interrupt triggering on comparator
output rise, fall or toggle. A block diagram of the comparator and its surrounding logic is
shown in Figure 24-1.
The Power Reduction ADC bit, PRADC, in “PRR0 – Power Reduction Register 0” on page 56
must be disabled by writing a logical zero to be able to use the ADC input MUX.

The you can decide if you want to use the comparator or not.

Hello to all.

I am a beginner and I have a school project to develop the Arduino Mega2560.
It consists of 2 electrodes that are placed on the head. I have to measure the potential difference between them (because one of them is the reference), amplify the signal and make a chart.

My big problem is that I can not find and do not even know how to use differential inputs with gain (10x or 200x).
Somebody please (with some urgency), can give me a code example?

Thanks for your help.

My big problem is that I can not find and do not even know how to use differential inputs with gain (10x or 200x).

No problem you can't. There is no differential input on any arduino board.

can give me a code example

Why, this is a hardware problem not a code issue.

This is an almost impossible project for you with limited electronics knowledge. You will end up measuring muscle movement rather than anything else. It is also dangerous having a low impedance circuit across your head if you touch any other part of the circuit.
As a start read:-
http://www.researchgate.net/post/What_sensors_do_you_suggest_for_detecting_brain_waves

I have to measure the potential difference between them (because one of them is the reference),

I am concerned about this statement, which doesn't make sense.

You'll need an instrumentation amplifier to measure the difference in potential between two electrodes. The REF pin of the IA can be tied to 2.5V so that you can handle both positive and negative inputs.

50Hz or 60Hz mains interference may be a problem, unless you run everything from batteries and go to the middle of a field to take readings.. You will probably need to follow the IA with a notch filter to remove this, and then an op amp to provide further amplification before the signal is large enough for an Arduino to digitize.

However, you should consider the safety aspects. What would happen if the Arduino was being powered from USB, and the computer developed an earth fault? Medical EEG equipment has a high degree of isolation between the electrode amplifier module and the rest of the equipment, so as to be safe in the event of an equipment fault. One possibility is for you to power the Arduino from a battery while it is taking measurements, but then you won't be able to print the data directly to a chart. You could collect the data, save it to an SD card, and subsequently generate the chart from the data on the card.

Thank you all for your help.

@vieuxpalleto - that is a question for your own thread. This was dealing with differential ADC input on the mega (which is possible but not very easy to use.)

As for the 5+ 0 5- problem, you have two analog inputs, each tells a direction. The one that would go -5V needs to be inverted to positive voltage with a simple inverting voltage follower/buffer. See picture .. you would then attach a small diode with stripe connected to the op-amp, this will protect the analog input from getting -5V when the sensor output swings to positive. Basically the inverting buffer just changes the input from the sensor when it goes -5V. The resistor values need to be the same for a gain of 1.

D2 - Sensor Output 0..-4.4V when input swings 0 to -5V (D2 blocks when op-amp output swings neg)
D1 - Sensor Output 0..+4.4V when input swings 0 to +5V (follows only positive part of input)
D1/D2 need separate analog inputs to determine absolute position. Good luck.

Old post revived.

Arduino can measure +/-5volt with a normal 2-resistor voltage divider, e.g. 10k:10k.
The resistor that is normally connected to ground is now connected to 5volt.
That gives 2.5volt on the divider with 0volt input (value ~512).
Leo..

dc42:
You'll need an instrumentation amplifier to measure the difference in potential between two electrodes. The REF pin of the IA can be tied to 2.5V so that you can handle both positive and negative inputs.

True

50Hz or 60Hz mains interference may be a problem, unless you run everything from batteries and go to the middle of a field to take readings.. You will probably need to follow the IA with a notch filter to remove this, and then an op amp to provide further amplification before the signal is large enough for an Arduino to digitize.

Not so true and overkill, if proper grounding techniques are observed and implemented IE; no daisy chains or ground loops, also if you have "box" or faraday shielding, only one end of the shielded cable need be grounded to the case.

However, you should consider the safety aspects. What would happen if the Arduino was being powered from USB, and the computer developed an earth fault? Medical EEG equipment has a high degree of isolation between the electrode amplifier module and the rest of the equipment, so as to be safe in the event of an equipment fault.

A good idea and design practice. Running from a USB phone recharge pack will work well, just observe that they usually time-out if there isn't enough current draw (> ~ 200mA)