I am trying to create an electric tuner using a piezo (so it can be used to detect vibrations rather than a mic which is subject to ambient noise)
I have the schematic below using a voltage regulator to create 5v (5.09V is the closest I could get)
The battery then powers the ADC and the op-amp and the 10-bit data is sent to the SPI pins of the Raspberry pi.
I am attempting to create a 3.3V bias so the negative voltages generated by the piezo can be read. (3.3V is the simplest implementation as 3.3V is all ready to go)
I have recently changed the design because the 5V from the RPi was unstable and giving me erroneous result but I am still getting bad results from my current setup.
The code I use reads the pin and converts the data into it's correct voltage the data is then displayed as a graph and created as a .wav file (the .wav will be used when I can get the setup right to listen to the note played)
Here is the code:
import spidev
import time
import os
import matplotlib.pyplot as plt
import wave
import numpy as np
from scipy.fftpack import fft, ifft
from scipy.io.wavfile import write
spi = spidev.SpiDev()
spi.open(0,0)
def read_spi(channel):
spidata = spi.xfer2([96,0])
data = ((spidata[0] & 3) << 8) + spidata[1]
return data
def ConvertVolts(data):
volts = (data * 5.09) / float(1023)
return volts
ADC_channel = 0
samples = []
print "Starting detection..."
start_time = time.time()
try:
while(True):
samples.append(ConvertVolts(read_spi(ADC_channel))
except KeyboardInterrupt:
pass
readTime = time.time() - start_time
print "Detection complete."
print("%s seconds" % (readTime))
time_axis = range(1,len(samples)+1,1)
print "Generating graph..."
plt.plot(time_axis,samples)
npSamples = np.asarray(samples)
scaled = np.int16(npSamples/np.max(np.abs(npSamples))*32767)
write('audio.wav', int(len(samples)/readTime),scaled)
plt.show()
Using the code above I generate a graph which looks like this:
This is the results for just under 1 second of reading.
simulate this circuit – Schematic created using CircuitLab
Can anyone explain why I am not getting a constant 3.3V displayed on my table?
Thank you.
EDIT:
When I remove the op-amp and connect the ADC input to the positive rail (5.09V) I get a reading of 768 or 3.8 Volts. So the code must be slightly out. But I don't get why the 3.3V on the RPi is inconsistent unless I'm sinking too many milliamps and it's unstable.
EDIT2: Updated the design. I have two 9V batteries and 2 LMT317. One generating 5.09V and one with 2.46V. 2.46V is connected to the input to the ADC and 5V is powering it. I'm still getting bad results fluctuating between 768, 256, 96 and 0. (3.82V, 1.27V, 0.47V and 0V, respectively)
Really don't understand at the moment.
Also, if CH0 is unplugged entirely it fluctuates between 0 and 256.