# 接线图

# 树莓派管脚图

# 代码

# C

#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#include <math.h>
#define		PCF     120
#define		DOpin	0
void Print(int x)
{
	switch(x)
	{
		case 1:
			printf("\n***************\n"  );
			printf(  "* Not Raining *\n"  );
			printf(  "***************\n\n");
		break;
		case 0:
			printf("\n*************\n"  );
			printf(  "* Raining!! *\n"  );
			printf(  "*************\n\n");
		break;
		default:
			printf("\n**********************\n"  );
			printf(  "* Print value error. *\n"  );
			printf(  "**********************\n\n");
		break;
	}
}
int main()
{
	int analogVal;
	int tmp, status;
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1;
	}
	// Setup pcf8591 on base pin 120, and address 0x48
	pcf8591Setup(PCF, 0x48);
	pinMode(DOpin, INPUT);
	status = 0;
	while(1) // loop forever
	{
		analogVal = analogRead(PCF + 0);
		printf("%d\n", analogVal);
		tmp = digitalRead(DOpin);
		if (tmp != status)
		{
			Print(tmp);
			status = tmp;
		}
		delay (200);
	}
	return 0;
}

编译命令: gcc RainDetector.c -o RainDetector -lwiringPi

# Python

#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time
import math
DO = 17
GPIO.setmode(GPIO.BCM)
def setup():
	ADC.setup(0x48)
	GPIO.setup(DO, GPIO.IN)
def Print(x):
	if x == 1:
		print ''
		print '   ***************'
		print '   * Not raining *'
		print '   ***************'
		print ''
	if x == 0:
		print ''
		print '   *************'
		print '   * Raining!! *'
		print '   *************'
		print ''
def loop():
	status = 1
	while True:
		print ADC.read(0)
		
		tmp = GPIO.input(DO);
		if tmp != status:
			Print(tmp)
			status = tmp
		
		time.sleep(0.2)
if __name__ == '__main__':
	try:
		setup()
		loop()
	except KeyboardInterrupt: 
		pass
#!/usr/bin/env python
#------------------------------------------------------
#
#		This is a program for PCF8591 Module.
#
#		Warnng! The Analog input MUST NOT be over 3.3V!
#    
#		In this script, we use a poteniometer for analog
#   input, and a LED on AO for analog output.
#
#		you can import this script to another by:
#	import PCF8591 as ADC
#	
#	ADC.Setup(Address)  # Check it by sudo i2cdetect -y -1
#	ADC.read(channal)	# Channal range from 0 to 3
#	ADC.write(Value)	# Value range from 0 to 255		
#
#------------------------------------------------------
import smbus
import time
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)
#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
def setup(Addr):
	global address
	address = Addr
def read(chn): #channel
	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) # dummy read to start conversion
	return bus.read_byte(address)
def write(val):
	temp = val # move string value to temp
	temp = int(temp) # change string to integer
	# print temp to see on terminal else comment out
	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 # LED won't light up below 125, so convert '0-255' to '125-255'
		write(tmp)
#		time.sleep(0.3)
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

宇凌喵 微信支付

微信支付

宇凌喵 支付宝

支付宝