# How to take inputs faster using Raspberry Pi

Status
Not open for further replies.

#### John Manuel

##### New Member
I am trying to detect different frequency signals using a Raspberry Pi and an ADC converter (PCF8591). My initial guess was the highest frequency I could detect would be limited by the speed of the I2C bus (which is about 400000 bauds). But when I run the setup, I could only get sampling speeds of around 10000 samples/sec. This value lies very close to the time taken to run an empty for loop in python (which is about 10^(-5) seconds which is equivalent to 10^(5) samples/sec).

I wanted to reach higher frequency domain (around 10 MHz) and using a better ADC won't be of much help since the limitation is set by the program itself. I wanted to know if my conclusion is correct or not. I have a feeling I am very wrong and doing something stupid. Any guidance will be much appreciated. Below is the python code I am using for signal detection.

Python:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smbus
import time
import matplotlib.pyplot as plt

start = time.time()

A0 = 0x40
A1 = 0x41
A2 = 0x42
A3 = 0x43
bus = smbus.SMBus(1)

voltage_value = [ ]
time_value = [ ]

try:
while True:
voltage_value.append(value)
time_value.append(time.time()-start)
#print("AOUT:%1.3f  " %(value*3.3/255))
#print(value)
#time.sleep(0.1)
except KeyboardInterrupt:
voltage_value = [x*3.3/255 for x in voltage_value]
plt.plot(time_value,voltage_value)
plt.ylabel('Voltage')
plt.xlabel('Time')
plt.show()

with open('output.txt', 'w') as f:
for v,t in zip(voltage_value,time_value):
f.write(str(v)+' '+str(t)+'\n')

#### ronsimpson

##### Well-Known Member
This value lies very close to the time taken to run an empty for loop in python
I think you will find C to be much faster.

#### Pommie

##### Well-Known Member
Why are you going via an ADC and then I²C? Can't you read the frequency as a square wave on a single pin?

Mike.

#### John Manuel

##### New Member
Why are you going via an ADC and then I²C? Can't you read the frequency as a square wave on a single pin?
I am trying to make a digital lock-in amplifier, so the input signal which I get will be mostly sinusoidal. Is it possible to detect such signals on a single GPIO pin?

#### dknguyen

##### Well-Known Member
I am trying to make a digital lock-in amplifier, so the input signal which I get will be mostly sinusoidal. Is it possible to detect such signals on a single GPIO pin?
Mostly or always? Because if you know it's always going to be a sinusoid than you don't actually need to read it in as a sinusoid. You could strip it down and just read in key features that would let you know the period because you already know it is a sinusoid.. You could clamp it with diodes (behind a resistor so the original signal is not clamped) and the period between rising and/or falling edges. Or use a comparator simiarly.

At 10MHz though it still might not be very accurate with a true GPIO...you'd want an input capture or something like that. Latency is not the Rapsberry Pi's strong suite so interrupts would not be ideal. Definitely not with Python.

#### ronsimpson

##### Well-Known Member
1) Using the Pi with a Real Time Operating System is something like using Windows on a PC and expecting to get 100% of the CPU. The system will go away for a while and do something on the hard drive.
2) PYTHON is a script and is slow. It is not compiled.
3) A PLL like the CD4046 can convert frequency to voltage in real time. Just read the voltage.
4) A counter like the 74HC4040 will take a frequency link 10mhz and hand you 5m, 2.5m, 1.25m, 626k, etc. Divide the 10mhz down to some frequency that the Pi can count. The 4040 can divide down to 2^12. Also look at CD4020 and 4060. The last one has some buffers on the front end that might help with "analog" to digital.

#### be80be

##### Well-Known Member
You can use ASM on the pi

Status
Not open for further replies.