## HELP! Sampling frequency

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### HELP! Sampling frequency

Hi all.

I have a question about sampling frequency. What is the sampling frequency using this example , and explain how you found the result.

The ADC is mcp3008 the same as in the project bellow.
Im using the same code but for another project. Current meter.

Thansk alot, and sorry for my spelling im from Sweeden

Code: Select all

``````#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
return -1
GPIO.output(cspin, True)

GPIO.output(clockpin, False)  # start clock low
GPIO.output(cspin, False)     # bring CS low

commandout |= 0x18  # start bit + single-ended bit
commandout <<= 3    # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)

# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
if (GPIO.input(misopin)):

GPIO.output(cspin, True)

adcout >>= 1       # first bit is 'null' so drop it

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)

# 10k trim pot connected to adc #0

last_read = 0       # this keeps track of the last potentiometer value
tolerance = 5       # to keep from being jittery we'll only change
# volume when the pot has moved more than 5 'counts'

while True:
# we'll assume that the pot didn't move
trim_pot_changed = False

print("%s milivolt" % (trim_pot*5000/1024))
time.sleep(0.5)``````

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: HELP! Sampling frequency

2Hz. The sleep(0.5) is a dead give-away.

Do you know there is hardware SPI on the RaspPi? That would use far far less CPU than doing it by bit-banging it from Python.

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

Hi, and thanks for fast reply

I want to ask you how did you calculate 0.5 to be 2 Hz,

I used a eksample form arudinio, im not god at coding self.

MarkDaniels
Posts: 60
Joined: Sun Oct 28, 2012 2:01 pm

### Re: HELP! Sampling frequency

Because 0.5 is the period and the reciprocal of period is frequency, hence 2 Hz.

Frequency = 1/period

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

MarkDaniels wrote:Because 0.5 is the period and the reciprocal of period is frequency, hence 2 Hz.

Frequency = 1/period

So it means that , the speed of my adc is 2hz, and that is how fast the adc is reading the analog input witch is in my output of a opamp. ?

If, 2 hz is the speed, and i have 10 bits adc, is my frequency 10*2hz = 20Hz ?

How many samples do i have ? 10 ?

As you can se im bit confused , sorry, and thansk for you help.

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: HELP! Sampling frequency

You have two samples per second (2Hz), each of which is accurate to one part in 1024 (10 bits).

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

rurwin wrote:You have two samples per second (2Hz), each of which is accurate to one part in 1024 (10 bits).

Hi again,
So as i understood, i have 1024 parts = 10 bits right ? 1024*2Hz = 2408Hz

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: HELP! Sampling frequency

No. The two numbers are not related to each other. You get two samples per second. "10 bits" only tells you how accurate those two samples are.

If you want more samples per second, all you have to do is to reduce that "0.5". I don't know what you can reduce it to, because I don't know what ADC you have. But I would hazard a guess that it will handle anything you can ask it to do with that Python program.

The bit of the program that is pulling in bits one at a time is nothing to do with the sampling frequency. "Sampling Frequency" is a specific term that applies to the rate at which the analogue value is measured. You are doing that at 2Hz. That measurement is then converted by the ADC into 10 bits, and it is those ten bits that you read one by one in your program. You are doing that very fast, maybe of the order of a megahertz, for brief periods every half second. But the sampling frequency is still 2Hz, because you get two measurements per second. If you had a 24-bit ADC, you would still have a 2Hz sampling frequency, because you would still get two measurements per second. Each measurement would require you to fetch twenty four bits instead of ten, and those measurements would be accurate to one part in 16 million rather than one part in 1024, but there would still be two of them per second.

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

rurwin wrote:No. The two numbers are not related to each other. You get two samples per second. "10 bits" only tells you how accurate those two samples are.

If you want more samples per second, all you have to do is to reduce that "0.5". I don't know what you can reduce it to, because I don't know what ADC you have. But I would hazard a guess that it will handle anything you can ask it to do with that Python program.

The bit of the program that is pulling in bits one at a time is nothing to do with the sampling frequency. "Sampling Frequency" is a specific term that applies to the rate at which the analogue value is measured. You are doing that at 2Hz. That measurement is then converted by the ADC into 10 bits, and it is those ten bits that you read one by one in your program. You are doing that very fast, maybe of the order of a megahertz, for brief periods every half second. But the sampling frequency is still 2Hz, because you get two measurements per second. If you had a 24-bit ADC, you would still have a 2Hz sampling frequency, because you would still get two measurements per second. Each measurement would require you to fetch twenty four bits instead of ten, and those measurements would be accurate to one part in 16 million rather than one part in 1024, but there would still be two of them per second.
Hi Rurwin,

Oh my god, a god explanation !!! Thank you so much..
Another question is, how do i know wich sampling frequency is enough for my project, then i will adjust the sleep time.

Short eksplanation is :

My adc (mcp3008) read the output from my opamp which is 3.9V . The result need to be put on a pc terminal ( hyper terminal , putty etc. )

Im building a current meter.

rurwin
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

### Re: HELP! Sampling frequency

That depends on what your current meter needs to do.

Generally if it only has a simple numerical display, then 2Hz seems to be what the general marketplace has settled on as fast enough to be useful and slow enough to be easily readable. If you had a graphic of an analogue dial, you might want 10-20Hz so the needle moved smoothly and showed transients. If the meter had an oscilloscope/trend graph then you might want to increase the sampling frequency even further.

It is also possible to do software processing of the samples to do filtering, averaging, Fourier transforms and so forth. I don't image that you would be too interested in most of those, but maybe an true RMS reading would be useful. For that you would need a sampling frequency of more than twice the maximum signal frequency you wanted to measure. So at least 100Hz to measure mains current (120Hz in the USA). And a higher sampling frequency would allow more accuracy when working with inductive or switching loads.

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

Usefull info .. Better than our teacher .. thx.

Then i understand it clearly.
I have another issue with my adc.
The analog signal to adc is 3.9V, i supply the adc with 5V but my measurements is not accurate at high voltage .. But, if i supply it with 3v3 then it is accurate at high voltage input but is not at low.

I use RPI 3v3 and 5v supply!. Rest of the circuit ( opamp ) have 4.9 supply, it is on common ground, it reads fine and the procces is perfect, but the measurements is not accurate , depends on what supply i put into the adc.

My guess is that the adc need the same amount supply as incoming input voltage on 3.9 V..

Fast reply needed. Thanks alot again!

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

ADC digital supply is usually different pin.

Without seeing your circuit, what signal source is and knowing what the ADC is cant be much more help.

What is the op-amp circuit?

Insatbilities could be caused by all sorts of things even being made on breadboard and having lots of wires acting as aerials can cause problems.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

ADC digital supply is usually different pin.

Without seeing your circuit, what signal source is and knowing what the ADC is cant be much more help.

What is the op-amp circuit?

Insatbilities could be caused by all sorts of things even being made on breadboard and having lots of wires acting as aerials can cause problems.
My Vref is at pin 2 = 5v on RPI. and GND is to common ground as opamp.

Here is my opamp circuit, i know that nothing is wrong with it.

the MCP3008 circuit is simple, cuz im only using channel 0, rest of the channels are put to GND.
and as you cans se at the button i have output from opamp 3.909V / 1024, so at 3.9v in i need to have 3.9V out. And to get 3.9V i have 1 A across shunt resistor. The feedback is 186 times cuz the shunt is 20-22. I Have about 6-7V into the LM cuz there are 1.2-1.5V drop and the ouput is stabile 4.9 into the opamp .

my code look like this.

Code: Select all

``````#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
return -1
GPIO.output(cspin, True)

GPIO.output(clockpin, False)  # start clock low
GPIO.output(cspin, False)     # bring CS low

commandout |= 0x18  # start bit + single-ended bit
commandout <<= 3    # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)

# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
if (GPIO.input(misopin)):

GPIO.output(cspin, True)

adcout >>= 1       # first bit is 'null' so drop it

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)

# 10k trim pot connected to adc #0

last_read = 0       # this keeps track of the last potentiometer value
tolerance = 5       # to keep from being jittery we'll only change
# volume when the pot has moved more than 5 'counts'

while True:
# we'll assume that the pot didn't move
trim_pot_changed = False

print("%s milivolt" % (trim_pot*3909/1024))
time.sleep(0.2)
``````
and as you cans se at the button i have output from opamp 3.909V / 1024, so at 3.9v in i need to have 3.9V out.
I hope you understand my english isnt good. Sorry!

Thanks.

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

By the way, the adc dosnt care if i have put unused cnannels to GND or not..

But if i write approx 4450 / 1024 in my code instead of 3909/ 1024 then i get almost perfect in all positions .. Why ?

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

1/ you are missing a 186k resistor from op-amp pin 4 to gnd THAT will improve stability and accuracy.

2/ for 3.9V out of op-amp approx 21mV in which is approx 1A, BUT what voltage is the current terminals sitting at with respect to op-amp gnd?

3/ If doing low speed sampling (<100Hz) then I would put a 10nF to 100nF across the output of the op-amp to gnd.

I believe you have been told this in the past in other threads.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

siqu wrote:By the way, the adc dosnt care if i have put unused cnannels to GND or not..

But if i write approx 4450 / 1024 in my code instead of 3909/ 1024 then i get almost perfect in all positions .. Why ?

The ADC converts analog voltages between 0 and 5V to ADC count values 0 to 1023. Just because your maximum input is 3.9V then that means you will not get full range with 5V Vref. If you want to get as near as possible 0 to 1023 ADC values out for 0-1A input you need a different gain on the op-amp.
Also when doing this type of circuit I do a 2 op-amp circuit The first stage actually divides the first stage to reduce the common voltage on the current measurement.

So 1023 is 5V (and above)

Therefore volts = ADC bits * Vref / 1023

volts = ADC bits * ( 5 / 1023 )
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

techpaul wrote:1/ you are missing a 186k resistor from op-amp pin 4 to gnd THAT will improve stability and accuracy.

2/ for 3.9V out of op-amp approx 21mV in which is approx 1A, BUT what voltage is the current terminals sitting at with respect to op-amp gnd?

3/ If doing low speed sampling (<100Hz) then I would put a 10nF to 100nF across the output of the op-amp to gnd.

I believe you have been told this in the past in other threads.
1) Why do i need a 186K from pin 4 to GND when i have it from output to pin3 ( R4)
2) I have 6.5V in to the regulator, and getting 4.9V stabile out - into opamp .
3) i will try the condesators right now.

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

techpaul wrote:
siqu wrote:By the way, the adc dosnt care if i have put unused cnannels to GND or not..

But if i write approx 4450 / 1024 in my code instead of 3909/ 1024 then i get almost perfect in all positions .. Why ?

The ADC converts analog voltages between 0 and 5V to ADC count values 0 to 1023. Just because your maximum input is 3.9V then that means you will not get full range with 5V Vref. If you want to get as near as possible 0 to 1023 ADC values out for 0-1A input you need a different gain on the op-amp.
Also when doing this type of circuit I do a 2 op-amp circuit The first stage actually divides the first stage to reduce the common voltage on the current measurement.

So 1023 is 5V (and above)

Therefore volts = ADC bits * Vref / 1023

volts = ADC bits * ( 5 / 1023 )

Sorry , but im not sure that i understand this post, but i will try anyway.

My powersupply to the adc is from RPI, and 5V.
My input to channel 0 on adc is 3.9 V, ( as i understood i need to have bigger supply than input to channel 0 and i have 5V so ?)
So 1023 is 5V (and above)

Therefore volts = ADC bits * Vref / 1023

volts = ADC bits * ( 5 / 1023 )
I did not understand this part ?
Do i need to put this to my code ADC bits * ( 5 / 1023 ) ?

You really have know some stuff here. Thank you so mutch.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

siqu wrote:
techpaul wrote:1/ you are missing a 186k resistor from op-amp pin 4 to gnd THAT will improve stability and accuracy.

2/ for 3.9V out of op-amp approx 21mV in which is approx 1A, BUT what voltage is the current terminals sitting at with respect to op-amp gnd?

3/ If doing low speed sampling (<100Hz) then I would put a 10nF to 100nF across the output of the op-amp to gnd.

I believe you have been told this in the past in other threads.
1) Why do i need a 186K from pin 4 to GND when i have it from output to pin3 ( R4)
If you dont you do not have a BALANCED differential amplifier configuration look up opamp differential amplifier. and by the way those resistors better be 1% tolerance or better.
2) I have 6.5V in to the regulator, and getting 4.9V stabile out - into opamp .
The inputs of current measurement on your circuit for the shunt resistor marked "Current +" and "Current -", what voltage with respect to the op-amp GND are they at?

I am not worried what you regulator is doing, other than you really should have supplied the input voltage at 7V to get 5V out stablely. That is the normal minimum input voltage for a 7805, and probably part of your problem.

By the way the circuit works from current sense to ADC as follows

1A through shunt Resistor 0.02R give 20mV voltage difference at the op-amp input terminals, with a common mode voltage of Volts in * (186k / (1k +186k) ) or Volts in * 0.99465 only on pin 3 (-) but almost Volts in at pin 4. Without the extra resistor to match on pin 4 your measurements are wrong

The approx gain of the op-amp is 186 so the output max for 20mV in (using correct configuration) is 20mV * 186 = 3.72V you are getting 3.9V because the extra resistor is missing.

This voltage is applied to an ADC with a 5V reference, so its max voltage in is 5V.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

siqu wrote:
techpaul wrote:
siqu wrote:By the way, the adc dosnt care if i have put unused cnannels to GND or not..

But if i write approx 4450 / 1024 in my code instead of 3909/ 1024 then i get almost perfect in all positions .. Why ?

The ADC converts analog voltages between 0 and 5V to ADC count values 0 to 1023. Just because your maximum input is 3.9V then that means you will not get full range with 5V Vref. If you want to get as near as possible 0 to 1023 ADC values out for 0-1A input you need a different gain on the op-amp.
Also when doing this type of circuit I do a 2 op-amp circuit The first stage actually divides the first stage to reduce the common voltage on the current measurement.

So 1023 is 5V (and above)

Therefore volts = ADC bits * Vref / 1023

volts = ADC bits * ( 5 / 1023 )
Sorry , but im not sure that i understand this post, but i will try anyway.

My powersupply to the adc is from RPI, and 5V.
The power for the device is applied to pin 14 of the device, the Vref which is the maximum range the ADC can convert is applied to pin 13 if this is 5V the above is true, but in your case Vref is 4.9V.
My input to channel 0 on adc is 3.9 V, ( as i understood i need to have bigger supply than input to channel 0 and i have 5V so ?)
Yes BOTH Vref and Vdd must be higher for most devices.
So 1023 is 5V (and above)

Therefore volts = ADC bits * Vref / 1023

volts = ADC bits * ( 5 / 1023 )
I did not understand this part ?
When you measure things with a ruler for example a 30cm ruler, the maximum it can measure is 30cm and if you cna only measure WHOLE cms then you have measurements in range of 0 to 30.

The ADC can only give 10bit answers which is WHOLE numbers between 0 to 1023 decimal.

The minimum value (0) is what the ADC gives for 0V on the input
The maximum value (1023 decimal or binary 11 1111 1111) is what the ADC gives when input matches Vref in this case should be 5V actually 4.9V.
So an output of 1 from ADC is 5V / 1023 on the input.
So an output of 2 from ADC is 2 * (5V / 1023) on the input.
Do i need to put this to my code ADC bits * ( 5 / 1023 ) ?
YES
You really have know some stuff here. Thank you so mutch.
I have been dealing with ADCs at low speed to higher than video speed for around 30 years and have in last 10 years various pieces of equipment used in production environment with current measuring on ADCs for MAXIMUM current ranges of 100nA to 50A, some circuits can withstand 400V AC. Yours would not be good for anything above 5V.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

1) Why do i need a 186K from pin 4 to GND when i have it from output to pin3 ( R4)
If you dont you do not have a BALANCED differential amplifier configuration look up opamp differential amplifier. and by the way those resistors better be 1% tolerance or better.
Okay i will read more about the diff opamp, and put a 186K Resistor at pin 4., my resistors are 1 %.

2) I have 6.5V in to the regulator, and getting 4.9V stabile out - into opamp .[/quote]The inputs of current measurement on your circuit for the shunt resistor marked "Current +" and "Current -", what voltage with respect to the op-amp GND are they at?[/quote]

5V and i turn op and down for the A and have different result on the output of opamp.

By the way the circuit works from current sense to ADC as follows
The approx gain of the op-amp is 186 so the output max for 20mV in (using correct configuration) is 20mV * 186 = 3.72V you are getting 3.9V because the extra resistor is missing.
I will be fast and point that the R sense is 0,022R and give me reasult of 4,092 V, but i have readable result of 3.909 V.

This voltage is applied to an ADC with a 5V reference, so its max voltage in is 5V.[/quote]

What you say is if i reduce Vref to 4.2 or so, i will get better result and the differance is not so big.?

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

Do i need to put this to my code ADC bits * ( 5 / 1023 ) ?
YES

Can you write the line of code for me, cuz when i do it i dont get any result at all. Only reading of 0 V no mather what.

You really have know some stuff here. Thank you so mutch.
I have been dealing with ADCs at low speed to higher than video speed for around 30 years and have in last 10 years various pieces of equipment used in production environment with current measuring on ADCs for MAXIMUM current ranges of 100nA to 50A, some circuits can withstand 400V AC. Yours would not be good for anything above 5V.
[/quote]
, i dont know what to say dude. Heavy champion.. Am still student, so people like you , THANK YOU!!!

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

Do i need to put this to my code ADC bits * ( 5 / 1023 ) ?
When you write adc bits do you then mean 10 bits cuz that is my adc.

and when i type it in python i don get any result.

please se my code, second last line, and help me.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Contact: Website

### Re: HELP! Sampling frequency

siqu wrote:
techpaul wrote:By the way the circuit works from current sense to ADC as follows
....
The approx gain of the op-amp is 186 so the output max for 20mV in (using correct configuration) is 20mV * 186 = 3.72V you are getting 3.9V because the extra resistor is missing.
I will be fast and point that the R sense is 0,022R and give me reasult of 4,092 V, but i have readable result of 3.909 V.
because you dont have stable 5V and the missing resistor.
What you say is if i reduce Vref to 4.2 or so, i will get better result and the differance is not so big.?
If put a voltage reference that works from 5V and can give 4.096V or 4.2V then your range will be reduced but accuracy increases. You need to do circuit mods first add resistor and 7V minimum supply in.

To change Vref (not Vdd) you really need to add a STABLE Bandgap reference many exist.

Personally unless it really is necessary I would leave it as 5V being actually 5V that way if op-amp for any reason gives out 5V you wont overdrive ADC
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

siqu
Posts: 26
Joined: Thu Apr 11, 2013 1:50 pm

### Re: HELP! Sampling frequency

Hi,

It works perfekt now. Thank you so mutch for your time, i dont hope it was wasted ..

Without you i wouldnt be able to manage this.

Thanks again!
May good bless you .