Go Down

Topic: Limit switch to stop DC motor (Read 1 time) previous topic - next topic


I just tried with the pull-up resistor instead of pull-down. And the same thing happens. When I put the fluke on the pins, I can see the voltage droping to around 7mV during movement, so the ball valves stops. When I start the valve back, I read around 3V until it either drops down to around 7-8 mV during travel, or until it reaches destination and I read 0 V.

Below is the code and I attached a picture of my current breadboard wiring. I will work on the CAD drawing, but now it's time to go to sleep in order to be in shape with the kids during the weekend.

Thanks for your help guys!

Code: [Select]
//This example will use a static IP to control the switching of a relay. Over LAN using a web browser.
//A lot of this code have been resued from the example on the ESP8266 Learning Webpage below.

#include <ESP8266WiFi.h>

// Below you will need to use your own WIFI informaiton.
const char* ssid = "Bacterie-verte"; //WIFI Name, WeMo will only connect to a 2.4GHz network.
const char* password = "alakazoo"; //WIFI Password

//defining the pin and setting up the "server"
int openPin = D1;
int closePin = D2;
int valveOpenedPin = D5;
int valveClosedPin = D6;
WiFiServer server(80);
IPAddress ip(192, 168, 1, 199); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network

// void setup is where we initialize variables, pin modes, start using libraries, etc.
//The setup function will only run once, after each powerup or reset of the wemos board.
void setup() {

  // Reset all the pins
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
  digitalWrite(D1, HIGH);
  digitalWrite(D1, LOW);
  digitalWrite(D2, HIGH);
  digitalWrite(D2, LOW);
  digitalWrite(D3, HIGH);
  digitalWrite(D3, LOW);
  digitalWrite(D4, HIGH);
  digitalWrite(D4, LOW);
  digitalWrite(D5, HIGH);
  digitalWrite(D5, LOW);
  digitalWrite(D6, HIGH);
  digitalWrite(D6, LOW);
  digitalWrite(D7, HIGH);
  digitalWrite(D7, LOW);
  digitalWrite(D8, HIGH);
  digitalWrite(D8, LOW);

  pinMode(openPin, OUTPUT);
  pinMode(closePin, OUTPUT);
  pinMode(valveOpenedPin, INPUT_PULLUP);
  pinMode(valveClosedPin, INPUT_PULLUP);
  digitalWrite(openPin, LOW);
  digitalWrite(closePin, LOW);

  Serial.print(F("Setting static ip to : "));

  // Connect to WiFi network
  Serial.print("Connecting to ");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  //Trying to connect it will display dots
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");

  // Start the server
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");

//void loop is where you put all your code. it is a funtion that returns nothing and will repeat over and over again
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {

  // Read the first line of the request
  String request = client.readStringUntil('\r');

  //Match the request, checking to see what the currect state is
  int value = LOW;
  if (request.indexOf("/relay=ON") != -1) {
    digitalWrite(closePin, LOW);
    digitalWrite(openPin, HIGH);
    while (digitalRead(valveOpenedPin) == HIGH) {
      digitalWrite(openPin, HIGH);
    digitalWrite(openPin, LOW);
    value = HIGH;
  if (request.indexOf("/relay=OFF") != -1) {
    digitalWrite(openPin, LOW);
    digitalWrite(closePin, HIGH);
    while (digitalRead(valveClosedPin) == HIGH) {
      digitalWrite(closePin, HIGH);
    digitalWrite(closePin, LOW);
    value = LOW;
  // Return the response, build the html page
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");

  if (digitalRead(valveOpenedPin) == HIGH) {
    client.print("Il y a de l'eau chaude.");
  else if (digitalRead(valveClosedPin) == HIGH) {
    client.print("Y A PAS D'EAU CHAUDE!!!");
  } else {
    client.print("La valve est en mouvement");
  client.println("<a href=\"/relay=ON\">Ouvrir l'eau chaude.</a> <br><br><br>");
  client.println("<a href=\"/relay=OFF\">Fermer l'eau chaude.</a><br>");

  Serial.println("Client disconnected");


only d3,d4,d8 are able to be used as input pullups according to a quick internet search of the wemos pins so you might have to move things about.

Second you will be looking for a LOW not a HIGH as the pullup is pulling the voltage to 5v with a built in resistor. Only by shorting the pin to negative via the switch (common negative) will the arduino read the pin.


Here's my CAD wiring:

I tried pins D3 and D4 at first, but it did not work at all for some reason. So I went back to D5 and D6.

My understanding is that D3 and D4 have built in resistors to be used as Pull-Up pins. If I want to use D5 and D6 as Pull-Up, I need to add my own 10k resistors. Correct me if I am wrong.

Anyway, alls seems to be working on D5 and D6, expect that I need to activate the button three times to complete the cycle. Whenever the ball valve stops, I can read a voltage drop on my Fluke. I just cannot explain it... I mean, the valve is connected directly to 3.3 V via the 10 k resistor.

Is 10k too much? I read I should use pull-up / pull-down resistors 10 times bigger than the impedance of the pins, which is around 100 Ohms from what I read.

I tried just for fun to apply 3.3 V directly on the Wemos D5/D6 pins, and the valve started to move again, without me doing anything via the IP address...


its time to add some serial prints with in the code especially in the while loop to see what the arduino is seeing.


after reading a little on the net I would be tempted to pull the cap of the top and see how the micro switchs and motor are wired.


Yes I think you're right. I measured voltage on the input pin that is looking for the closed valve signal while I opened the valve. For some reason, the voltage turns negative with real low value.

I would expect to always have 3.3 V, except when the valve is fully closed or fully opened.

Maybe it's time to begin playing with the ossilloscope I got from my father so I can measure voltage on every pin at the same time.


Where is the motor power coming from? You show an L298 driver, what is the VS (power supply) pin connected to?


Can you post a picture of your project please,so we can see your  component layout?

It sounds like you have a gnd connection missing or faulty.
The voltage on pins D5 and D6 are supposed to change only when you reach either end of the value travel.

The fact that it drops immediately you operate the motor is worrying.

What point on your project are you using for gnd reference for the DMM?

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....


Aug 13, 2017, 03:27 am Last Edit: Aug 13, 2017, 03:36 am by sTaLa
The motor power is coming from a small bread board power supply, which is connected to a 12 V Lithium battery. The power supply gives 5 V to a H-Bridge I built. There is no proper L298 board, I just did not think a schematic with my four NPN was worth it for the current situation.

When I measured with the multimeter, I used the ground coming from the 5V power supply. But, as you can see on my breadboard, all the grounds are connected together. It is the right thing to do right?

One thing I want to clarify: the voltage on the input pins D5/D6 does not drop immediatly when I operate the motor. I would say there is roughly one second delay. I read around 3V, and all of sudden it drops to around 0.007 V.

Below are more pictures of my project.
Thanks again!

I also attached a more Hi-Res picture to the post if you want to download it. As high as I could get with 1 MB.


Mhhh... I am pretty sure the issue is in my code. For some reason it gets out of the while loop I think.

Also, the IP adress does not respond after a while in the browser and I have to reset the board. All the Wi-Fi code I not understand completely as I used one from another person... I guess I will look more deeply into the code itself...


I believe that power supply can only supply around 700 mA, probably not enough to start that big valve motor.


I tried with a 5VDC 2 amp PSU and got the same result.

I looked at it again and I really think there is something in the loop that makes the board stops its process. But the board only resets when I send a command. I can see the blue LED blinks, and at that point the valve stops moving. But there is still 3.3 V on the Input pin. You can see this on the video below.

It really seems the sketch is in a loop because the valve starts to move again after a while.


This makes no sense to me. But I solved the issue by adding a delay of 10 seconds after the command is sent. I do not understand why I had to do this, but it works... And the "limit switches" action prevent the solenoid to be actionned for 10 seconds. So I guess this is viable for use...

Thanks for your help on this guys!

Code: [Select]
if (request.indexOf("/relay=ON") != -1) {
    //    digitalWrite(closePin, LOW);
    //    digitalWrite(openPin, HIGH);
    //    delay(1000);
    while (digitalRead(valveOpenedPin) == HIGH) {
      digitalWrite(openPin, HIGH);
    digitalWrite(openPin, LOW);
    value = HIGH;
  if (request.indexOf("/relay=OFF") != -1) {
    //    digitalWrite(openPin, LOW);
    //    digitalWrite(closePin, HIGH);
    //    delay(1000);
    while (digitalRead(valveClosedPin) == HIGH) {
      digitalWrite(closePin, HIGH);
    digitalWrite(closePin, LOW);
    value = LOW;


Aug 27, 2017, 03:58 am Last Edit: Aug 27, 2017, 04:01 am by TomGeorge
You have 4 transistors in your pictures, where are they on your circuit diagram?
Please include your power supply , limit switches and motor, not terminal blocks.

Thanks    Tom... :)
Everything runs on smoke, let the smoke out, it stops running....


The valve has a stroke time of about 10 seconds according to the paper work. So by adding the delay you have told the valve that open is run 10 seconds one way, closed is 10 seconds the other way.

It works but its not really correct. With out opening the valve to look at the style if inputs inside the head its hard to tell what is really going on. If this was a expensive industrial valve I would say that the cams inside the head had not been adjusted to set the open and closed points but I do not see a note on how to set the end points on this valve.



The transistors are four NPN that serve as a H-Bridge.

The power supply is wathever I find that provides 5 VDC. I ran tests with USB from PC; with the small Bread Board Power Supply connected to a 12V Lithium Milwaukee battery; and with a 2 Amp 5VDC phone charger connected to 120 VAC.

I do not understand your comment about the limit switch and the motor. They are all built in within the valve and I connect the valve directly to the terminals as you can see on the above pictures.


Yes the stroke is approximatively 10 seconds. So I need the delay to be at least 10 seconds. I tried just for fun putting 20 seconds and it worked as well.

The delay is within a while loop. So the way I read my code is:

- As long as (while) the input pin for the limit switch does not read 0 V
         - Turn the motor ON
         - Delay of 10 or 20 seconds
- Exit the loop as soon as the input pin of the limit swith reads 0V

I think this ensure that the valve does not keep trying to move while it already reached its end position. And to my ears it works. Because I can hear the solenoid being activated if I apply directly voltage. But I do not hear it with my code. Even if I put in a 20 seconds delay, the valve stops as soon as it reachs its end position.

This is not the perfect solution as if the valve takes longer than the delay to open, then the board will reset (which can be seen on the above video). I think the root cause of my issue is the board resetting.

As far as calibration, I think everything is correct internally. I have 3 wires for the limit switches acting as dry contacts. So on common, and one each for the fully opened or fully closed positions. I can confirm you those dry contacts are NO and only closed once the valve reaches its final position.

Below are pictures of the final board I did. The only negative I have is I do not have  5VDC directly to the valve. I seems the impedance of the transistors is too high. What is strange though is that I tried just for fun to apply 5 VDC to the the base of the NPNs instead of the 3.3 VDC from the D1 Wemos. And the valve went faster??? I thougth the voltage applied to the base was only to turn the NPN "on" and the the 5 VDC from the collector was supposes to go to the emitter...

Go Up