Go Down

### Topic: Processing 2 Analogue Inputs To Create Differential Value (Read 5717 times)previous topic - next topic

#### Wi11turner

##### Jul 25, 2012, 11:32 pm
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.

#### Grumpy_Mike

#1
##### Jul 25, 2012, 11:47 pm
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.

#### Wi11turner

#2
##### Jul 25, 2012, 11:54 pm
Thanks Mike.

Much appreciated.

#### MichaelMeissner

#3
##### Jul 25, 2012, 11:57 pm
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.

#### MarkT

#4
##### Jul 26, 2012, 02:04 am
Is the problem that the signals might be changing while you read them?  Something like

Code: [Select]
`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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### NathanNZ

#5
##### Jul 26, 2012, 01:11 pm
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: [Select]
`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 ;}`
Arduino Internet / Web Control with Teleduino
https://www.teleduino.org

#### Wi11turner

#6
##### Aug 05, 2012, 01:35 am
Many thanks all,

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

Kind regards,

#### MarkT

#7
##### Aug 05, 2012, 04:10 am

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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### WillR

#8
##### Aug 06, 2012, 05:16 pm
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.
Just another Hacker

#### ManSilva

#9
##### Oct 23, 2013, 10:53 am

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?

#### Grumpy_Mike

#10
##### Oct 23, 2013, 03:17 pm
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.
http://www.researchgate.net/post/What_sensors_do_you_suggest_for_detecting_brain_waves

#### michinyon

#11
##### Oct 23, 2013, 03:31 pm
Quote
I have to measure the potential difference between them (because one of them is the reference),

#### dc42

#12
##### Oct 23, 2013, 11:21 pm
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.
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.

#### ManSilva

#13
##### Feb 12, 2014, 10:59 am
Thank you all for your help.

#### rinkrides

#14
##### Oct 12, 2015, 12:30 amLast Edit: Oct 12, 2015, 12:41 am by rinkrides
@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)
Electronics Engineering, "EE=MC^(OMG x WTF?!)"
Professional Auto Collision/Custom tech

Go Up

Please enter a valid email to subscribe