简体   繁体   中英

how to switch on/off a circuit from python?

I has a circuit containing mcp3008 and a rain sensor module that read analog data and send digital data to raspberry pi.I what to create another circuit that has a button that can turn on and off the rain sensor circuit. the rain sensor circuit is connected to 5v pin on raspberry pi. can anyone help me on how to turn on and off the rain sensor circuit?

This is the web that I found about how to on/off another circuit from it: http://www.barryhubbard.com/raspberry-pi/howto-pn2222a-npn-gpio-controlled-simple-transistor-circuit-for-raspberry-pi/

import RPi.GPIO as GPIO #Get's GPIO module
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
#Define your MCP3008 SPI stuff here
CLK = 18 # Change these values to then pins you are using
MISO = 20
MOSI = 21
CS = 8
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
GPIO.setmode(GPIO.BCM) #sets mode
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button Pin
input_state = GPIO.input(22) #Get's button input
while True:
      if input_state == 1:
            break
      else:
          continue

And there's your code!! I also happen to use the MCP3008 module for a windsensor! Happy programming!

EDIT: To read one channel simply import Adafruit_SPI as SPI import Adafruit_MCP3008 CLK = 18 MISO = 20 MOSI = 21 CS = 8 mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) values = [0]*8 for i in range(8): values[i] = mcp.read_adc(i) CHANNEL_ZERO = '{0:>4'.format(*values)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM