# 接线图
# 树莓派管脚图
# 代码
| #include <stdio.h> |
| #include <wiringPi.h> |
| #include <pcf8591.h> |
| |
| #define PCF 120 |
| |
| int main (void) |
| { |
| int value; |
| int count = 0; |
| wiringPiSetup (); |
| |
| pcf8591Setup (PCF, 0x48); |
| while(1) |
| { |
| value = analogRead (PCF + 0); |
| |
| if (value < 50){ |
| count++; |
| printf("Voice In!! %d\n", count); |
| } |
| } |
| return 0; |
| } |
编译命令: gcc sound_sensor.c -o sound_sensor -lwiringPi
# Python
| |
| import PCF8591 as ADC |
| import RPi.GPIO as GPIO |
| import time |
| |
| GPIO.setmode(GPIO.BCM) |
| |
| def setup(): |
| ADC.setup(0x48) |
| |
| def loop(): |
| count = 0 |
| while True: |
| voiceValue = ADC.read(0) |
| if voiceValue: |
| print 'Value:', voiceValue |
| if voiceValue < 50: |
| print "Voice detected! ", count |
| count += 1 |
| time.sleep(0.2) |
| |
| if __name__ == '__main__': |
| try: |
| setup() |
| loop() |
| except KeyboardInterrupt: |
| pass |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import smbus |
| import time |
| |
| |
| bus = smbus.SMBus(1) |
| |
| |
| def setup(Addr): |
| global address |
| address = Addr |
| |
| def read(chn): |
| if chn == 0: |
| bus.write_byte(address,0x40) |
| if chn == 1: |
| bus.write_byte(address,0x41) |
| if chn == 2: |
| bus.write_byte(address,0x42) |
| if chn == 3: |
| bus.write_byte(address,0x43) |
| bus.read_byte(address) |
| return bus.read_byte(address) |
| |
| def write(val): |
| temp = val |
| temp = int(temp) |
| |
| bus.write_byte_data(address, 0x40, temp) |
| |
| if __name__ == "__main__": |
| setup(0x48) |
| while True: |
| print 'AIN0 = ', read(0) |
| print 'AIN1 = ', read(1) |
| tmp = read(0) |
| tmp = tmp*(255-125)/255+125 |
| write(tmp) |
| |