Solenoid Valve problem

Hey there, I’m trying to build a water dispenser via a Solenoid Valve for a school task. I’ve been struggling for a couple of days, and the farthest I got was letting water flow through. But normally it should only let water flow when the HC-SR04 sensor detects an object, and stop when it doesn’t detect something.

Here is my code, and some screens of my project. If anyone could help me, it would be wonderful!

#define trigger 9
#define echo 8
#define LED 13
#define MOSFET 12

float time=0,distance=0;
void setup()


void loop()



void measure_distance()

With kind regards,

Does the led work as planned, which would dis/prove the logic.

The led goes on when the sensor detects something, this works fine. But the Valve keeps on letting water through even if the led is off

Can't really tell a whole lot from those pics tbh, so a hand drawn circuit diagram and details of the components would be a big help.

Will give a Fritzing scheme if you want? Can't draw :stuck_out_tongue:

Will give a Fritzing scheme if you want? Can’t draw :stuck_out_tongue:

Nobody here is very keen on Fritzing, but probably better than the photo as long as you get the wires all nice and square not just spaghetti. Fritzing also has a schematic view, iirc.

And don’t forget to give details of the components, like a part number for the mosfte and the solenoid, and voltages etc etc.

Should look something like this, with solenoid in the place of the motor:


On it, I'm wanted to draw but because of some "limitations" I have I cant draw or write normally.

Here you go, these are the materials i used

Materials used:
Female/Male jumper wires
Plastic Water Solenoid Valve – 12V – 1/2″ Nominal
HC-SR04 sensor
Arduino/genuino 101
Large breadboard

Crucial info missing: the mosfet part number, ditto the solenoid.

And you didn’t mention a flyback diode, see pic I posted, which is very nb for inductive loads like motors and solenoids.

And read this on how to get your pix inline.

Mosfet = p645p
Solenoid valve = 997

And I'm following this guide: Automatic Water Dispenser using Arduino

They never mentioned using a diode

I'm not finding p645p mosfet on the interwebz: is that the full marking? Any other details?

And the link you gave uses an IRF540 mosfet which is less than ideal; an IRL logic level one would be better.

It is this one:

On the mosfet there's written

And qua materials, school just gave me some projects and told me what I was needed to buy.

Ok that's an IRF540, the p645p thing is probably a manufacturing batch or date code or something.

If it's letting water through that means the mosfet is closing (even though it's not logic level) but it seems it's not switching back if you say the water wont stop.

First though, have you checked that just by applying 12V to the solenoid and taking it away, the solenoid operates?

In your parts list you mention resistor singular not resistors plural. You need a 10k from the Arduino pin / mosfet gate to ground to make sure the mosfet goes off. You got one?

We really need to see the circuit schematic.

And you really need that diode, else something may get damaged. It prevents huge spikes into the mosfet / Arduino when the inductive load is turned off.

From the moment I power the Arduino with 12V the water starts running until I plug it out. Normally it should only work when the sensor detects an object.

Since I followed this guide (Automatic Water Dispenser using Arduino). I didn't use a diode.

My setup is exactly the same as

Do my eyes deceive me or do both the red and black go into that nearest power rail? And another red to Vin in the second line?

And what's that white wire from pin 13 or is it gnd connected to, ane where does that black actually go befind the mosfet.

I see only one resistor.

Please, get us a schematic of your actual circuit, drawn (or Fritzing'ed if you must) from what you see in front of you.

I already provided a scheme and the breadboard scheme in one of my previous posts, there is all the info you need i think

Ok I didn't see these before; that's why it's better if you put them in the post (read the instructions I linked to); makes them more obvious.

I really can't make those out too well, but you seem not to have realised that the channel in the middle of the breadboard is a break in connection. So with the mosfet in the bottom half, and that blue diagonal wire to the top half from around pin 13 or (ground?) that wire is not connected to anything on the mosfet.

I realised it was a break indeed. And it goes to the 12pin. But now there are coming sparkles and smoke out of the Arduino

Well unfortunately, from those pix I honestly have no idea of what's connected to what...