Sample rate analogRead at tone frequency.

Hello,

I have a led and a photodiode, to eliminate noise I’m trying to blink the Led at 3 kHz and then trying to read the analog value at the same frequency.

My first try was using blinkwithout delay, and every time I would say HIGH or LOW I also read the photodiode value. The problem is as I increase the code complexity (moving averages, etc) the frequency decreases and it’s not very stable.

My second try was using the tone function. I can blink the led at 3 kHz in a stable matter without being affected by the code, but how can I analogRead at the same frequency? Could I connect the tone pin to a digital pin to set as a clock someway? Or should I change the code behind the tone function?

Thanks in advance

Let's back up toward square one, what are you trying to do with the LED and Pdiode?

I'm trying to measure reflectance measurements of IR. So because of the normal room lights that are at 60Hz, I'm trying to blink at some kHz and then filter the room litghs.

I'm trying to blink the Led at 3 kHz and then trying to read the analog value at the same frequency.

That won't work at all.

The absolute rule is that you MUST sample at at least twice the frequency of the signal. Google "aliasing" and "Nyquist interval" to learn more.

I was sampling at the same rate, so arduino could act as kind of a digital chopper, or I'm thinking this the wrong way? I'm gonna test connect the tone pin to a attachInterrupt and then analogRead in the function.

rdFarinha:
I'm trying to measure reflectance measurements of IR. So because of the normal room lights that are at 60Hz, I'm trying to blink at some kHz and then filter the room litghs.

I don't think you need to blink the IR LED at kHz just to do reflectance measurements, you just need the difference when IR is ON vs. OFF

A simple example:

  1. IR OFF, delay 10uS measure photodiode = reading1
  2. IR ON, delay 10uS, measure photodiode = reading2
  3. IR OFF, delay 10uS, measure photodiode = reading3

averageOFF = (reading1+reading3)/2
deltaON= reading2 - averageOFF

Reading 'off' either side of the 'on' reading helps ignore the slope of any ambient 60Hz lighting
The example 10uS delay is to allow settling of your detector.

You could simply loop at any rate you like and average the results.

Yours,
TonyWilk

rdFarinha:
So because of the normal room lights that are at 60Hz.

"Normal" room lights come in many types.
Incandescents including halogen lights don't flicker, the filament will stay hot long enough to bridge the transitions.
Fluorescents flicker - at double the supply power frequency (so 100 or 120 Hz) as they produce a "flash" for both halves.
LEDs - what nowadays is the norm - well, I honestly don't know how they're driven exactly. They take DC and then it's easy enough to filter most of the ripple from the supply voltage to stop flickering.
The second and third type produce little heat so little IR in the first place, so it's not something to worry about much with your IR LED.

I was sampling at the same rate, so arduino could act as kind of a digital chopper, or I'm thinking this the wrong way?

Yes. Think about what happens if you sampled at exactly the same rate as the blink (which is impossible anyway), and your program accidentally samples only the "LED off" periods.