I am trying to do an Internet of Things project with the help of a Raspberry Pi. While most of the project is complete, there is one small thing I am stuck at. I need to connect a solenoid valve to the Pi. The valve needs a voltage of 24v-36v to operate. I connected three 9v batteries together in series to operate it. The valve opens when the voltage is applied and closes when it not. I want to control the opening and closing of the valve by the Pi’s GPIO pins. I’m using a NPN 2N3904 transistor for that. The circuit I’m using is exactly the one given in this image. (http://www.davidhunt.ie/water-droplet-photography-with-raspberry-pi/)
However, it’s not working. The circuit works sometimes and doesn’t work other times when I manually pull and put in the GPIO pin. It never works with code. Is the 27v causing problems with the Pi? What can I do? Is there another way to control it apart from NPN transistors?
The code:
import RPi.GPIO as GPIO
import time,os
pin=18
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin,GPIO.OUT)
while(True):
GPIO.output(pin,GPIO.HIGH)
time.sleep(5)
GPIO.output(pin,GPIO.LOW)
time.sleep(5)