Go Down

Topic: Limit switch to stop DC motor (Read 482 times) 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.

Go Up