how fast is the digitalread function?

how fast is the digitalread function? I am trying to find out if a arduino is suitable for my project.

I want to measure the voltage over a precicion resistor every 2 microseconds to calculate the used current as accurate as possible.

Did you mean how fast is the analogRead?
Neither digitalRead nor analogRead is fast enough for what you want.

Need a really fast external ADC for that.
That's a read every 32 clocks at 16 MHz, not much time to do anything with the data after you read it.

CrossRoads:
Need a really fast external ADC for that.
That's a read every 32 clocks at 16 MHz, not much time to do anything with the data after you read it.

And at those speeds you probably need a different microcontroller... probably a DSP to have much chance of doing anything useful...

read every 2 uS is sampling at 500,000 samples per second... Maybe the upcoming Due might be good (with an external ADC) but I still think this is better the realm of a DSP...

You're going to need a modified project or a different processor.

The first issue is getting an ADC that will do 500ksps, they're around but not internal to any small processors such as used on the Arduino. But then you have to read the data from the external ADC and do power calcs as well, all in 2uS.

Short answer, not on any current Arduino. The Due has an ADC that will do 1msps, there's just a chance you can do it with that if your code is very well written.

How long do you need to sample for? If it's just a short period you may be able to save the data and post process it.


Rob

You don't want to use an opamp as an integrator? This allows much less frequent sampling (but does introduce the issue of resetting)

AWOL:
Did you mean how fast is the analogRead?

Oh ....' I do mean analogRead.

Graynomad:
You're going to need a modified project or a different processor.

The first issue is getting an ADC that will do 500ksps, they're around but not internal to any small processors such as used on the Arduino. But then you have to read the data from the external ADC and do power calcs as well, all in 2uS.

Short answer, not on any current Arduino. The Due has an ADC that will do 1msps, there's just a chance you can do it with that if your code is very well written.

How long do you need to sample for? If it's just a short period you may be able to save the data and post process it.


Rob

I was also thinking about a chipkit controller. Maybe these are fast enough for what I need.

I want to calculate how much power my amps are using so I can switch them off automaticly when there is no music playing for 20 minutes or so. I don`t think it should be necessary to measure all the time. Maybe even less measurements per second will also do.

I want to measure it in the ac line (230 volt @ 50Hz). So one sinewave is 1/50 = 20 ms. I had in mind to make 10 easurements per sinewave so I can calculate really accurate the used current. When the used current is stable for some time, it must mean that there is no music being amplified.

I know that one can easily buy power killers and plug the between the apparatus and the wall socket, but I want to build this myself inside the amps.

Wouldn't it be easier to monitor the line-in signal then, if no input was coming in than for sure nothing was being amplified.
Add a simple envelope follower circuit and a comparator. When the comparator was not tripped for a long time, use that as the turn-off signal.

analogRead does about 10.000samples/sec by default... 16MHz/128/13 (u can tweak the 128 down to 32 or so if u want)...
0.1msec=100usec between the samples...

CrossRoads:
Wouldn't it be easier to monitor the line-in signal then, if no input was coming in than for sure nothing was being amplified.
Add a simple envelope follower circuit and a comparator. When the comparator was not tripped for a long time, use that as the turn-off signal.

I`m not that familiar with electronics, but it sounds like it has to be build into the signal path. I have two reasons for not wanting to use this envelope follower.

  1. Lack of space between the input and the amps.
  2. I don`t like the idea of more components in the signal path. Less=more XD

Anyways, I must have been real sleepy last night (and this morning) :roll_eyes: I wrote digitalRead instead of analogRead and also 2 us instead of 2 ms

Reading every 2 ms means 500 samples per second. Reading the replies I understand that an arduino is suitable for the job.

I wrote the ShowInfo sketch: Arduino Playground - ShowInfo
There is a speedtest in it.
On a Arduino Uno, the AnalogRead uses 112 us.
But if you write your own code to control the ADC, it can be faster.

Krodal:
I wrote the ShowInfo sketch: Arduino Playground - HomePage
There is a speedtest in it.
On a Arduino Uno, the AnalogRead uses 112 us.
But if you write your own code to control the ADC, it can be faster.

Nice! This makes it clear that the arduino is fast enough to sample every 2 ms and also do some calculations with the data.

will u use a hall sensor?
AC lines r quite dangerous... for arduinos and humans... :slight_smile:

No, not a hall sensor. It came to my mind, but using a precision resistor the measurements become a lot more accurate. It has two extra legs for measuring the voltage over it.

The datasheet.
http://www.produktinfo.conrad.com/datenblaetter/425000-449999/447331-da-01-en-Praezisionswiderstand_PBV.pdf

Digital write takes around 4us I imagine digital read takes a similar amount of time

Analog read can be sped up by changing the prescale values and I have measured around 17us to do an analog read when the prescale values are changed

its enough to do a fair bit within a 50Hz cycle

If thats not enough the STM32F4 is much much faster but harder to drive!

corprius:
I want to measure it in the ac line (230 volt @ 50Hz). So one sinewave is 1/50 = 20 ms. I had in mind to make 10 easurements per sinewave so I can calculate really accurate the used current. When the used current is stable for some time, it must mean that there is no music being amplified.

I did something similar last year, although I was measuring AC voltage not current. I found that I had to sample the 50Hz signal about every 500uS to get anything like consistent results, otherwise the calculated RMS value varies too much depending on how far after the zero crossing you happen to take the first sample. I used timer 2 to generate an interrupt every 500uS and kick of a new ADC conversion, and I used the ADC conversion complete interrupt to read and accumulate the values. That way, the processor can do something useful while the conversion is taking place.