Pages: [1]   Go Down
Author Topic: Processing 2 Analogue Inputs To Create Differential Value  (Read 2132 times)
0 Members and 1 Guest are viewing this topic.
Derbyshire
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33448
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Derbyshire
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Mike.

Much appreciated.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1837
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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).
Logged

[ I won't respond to messages, use the forum please ]

New Zealand
Offline Offline
Newbie
*
Karma: 1
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 ;
}
Logged

Arduino Internet / Web Control with Teleduino
https://www.teleduino.org

Derbyshire
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks all,

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



Kind regards,
Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Georgina Ontario
Offline Offline
Sr. Member
****
Karma: 5
Posts: 437
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Just another Hacker

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33448
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2898
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for your help.
Logged

Pages: [1]   Go Up
Jump to: