Skip to content

Instantly share code, notes, and snippets.

@theapi
Last active November 27, 2018 01:11
Show Gist options
  • Save theapi/6543051 to your computer and use it in GitHub Desktop.
Save theapi/6543051 to your computer and use it in GitHub Desktop.
Simple demo to detect knocking from a piezo sensor on the Raspberry Pi.
#!/usr/bin/python
import time, signal, sys
from Adafruit_ADS1x15 import ADS1x15
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
#print 'Press Ctrl+C to exit'
ADS1015 = 0x00 # 12-bit ADC
ADS1115 = 0x01 # 16-bit ADC
# Initialise the ADC using the default mode (use default I2C address)
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
adc = ADS1x15(ic=ADS1015)
while 1:
# Read channel 0 in single-ended mode, +/-4.096V, 250sps
volts = adc.readADCSingleEnded(3, 1024, 860)
# To read channel 3 in single-ended mode, +/- 1.024V, 860 sps use:
# volts = adc.readADCSingleEnded(3, 1024, 860)
if volts > 0.5:
print "%.6f" % (volts)
time.sleep(0.01)
@IvanArrego
Copy link

Hi theapi,

I am having an issue with line 22:
Traceback (most recent call last):
File "vibe2.py", line 22, in
volts = adc.readADCSingleEnded(3, 1024, 860)
AttributeError: 'int' object has no attribute 'readADCSingleEnded'

I am using the ADS1015 and have changed line 17 to be: adc = ADS1015.

Would you help me understand what i am doing wrong? I am sorry but i am very new to coding and hardware so i would greatly appreciate your time and patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment