Control a DC motor with limit switch

Hi Blynkers, i am in need of some help, The objective is to start the DC motor by using APP Button and the motor stopping when limit switch is triggered.

The limit switch is NC (HIGH) and LOW when triggered

I am using the old style WEMOS D1 board. I have wired it up and although the limit switch blinks the onboard led, it does not make D6 LOW. can somebody tweek my code for it to work please?

D6 is the motor pin
D5 is the limit switch

here is my code so far:-

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char server[] = "*********";
char ssid[]   = "*********";
char pass[]   = "***********";
char auth[]   = "*************************";  

const int motorIn1 = D6; 
const int limitswitch = D5;

    

    
 void setup()
{
  pinMode(motorIn1,OUTPUT);
  digitalWrite(motorIn1, LOW); // LED OFF on bootup
  pinMode(limitswitch,INPUT);
  digitalWrite(limitswitch, HIGH);
  Serial.begin(115200);
  Blynk.begin(auth, "***************", "*********", IPAddress(***********));

 {
  if (limitswitch == LOW){
    digitalWrite(motorIn1, LOW);}
     else{
    digitalWrite(motorIn1, LOW);}  }
  
 }




void loop()
{
  Blynk.run();
}

Tweak for you, setup() is a run once at MCU bootup function.

can you extend your tweek haaaaaaaa to like maybe rewrite it how it should be.

Sure, BLYNK_WRITE().

haaaaaa @Costas some how i can’t see that will work for me

you are Costas the CODER, i am merely a poor old DOG

See the docs at http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynk_writevpin and sample sketch using BLYNK_WRITE() with an ESP at https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FVirtualPinRead

here is my attempt that will not compile?

    #define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>


    char server[] = "*********";
    char ssid[]   = "*********";
    char pass[]   = "***********";
    char auth[]   = "*************************";  

    const int motorIn1 = D6; 
    const int limitswitch = D5;

        

        
     void setup()
    {
      pinMode(motorIn1,OUTPUT);
      digitalWrite(motorIn1, LOW); // LED OFF on bootup
      pinMode(limitswitch,INPUT);
      digitalWrite(limitswitch, HIGH);
      Serial.begin(115200);
      Blynk.begin(auth, "***************", "*********", IPAddress(***********));

  Blynk.virtualWrite(V0, LOW); // button set to OFF on bootup
}

// WeMos on board LED is active LOW
BLYNK_WRITE(V0){      // Timer widget                     
  if (motorIn1 == HIGH) {
    Blynk.virtualWrite(V0, HIGH); // MOTOR OFF
    Serial.println("MOTOR ON");
  }
  else{
    Blynk.virtualWrite(V0, LOW); // button set to OFF from timer
    Serial.println("MOTOR OFF"); 
  }
 Blynk.syncVirtual(V1);      // sync virtualWrite 0 / 1 to button widget
}
 
 BLYNK_WRITE(V1){      // Timer widget                     
  if (limitswitch == LOW) {
    digitalWrite(motorIn1, LOW); // MOTOR OFF
    Serial.println("MOTOR OFF");
}
  else{
    digitalWrite(motorIn1, HIGH); 
    Serial.println("MOTOR ON"); 
  

  }
  
}

void loop()
{
  Blynk.run();
}

both if statements have the opening bracket in the wrong place.

pleaseeeeeeeee where should they be?

Like this https://www.arduino.cc/en/Reference/If

1 Like

still struggling with this code, i have corrected the opening brackets which compiles and uploads to the Board but limit switch does not stop the motor, any ideas?

After a very quick glance at your code, I do not see any timers or interrupt routines to check for the state of any physical limit switch. Thus it is being ignored until you press a button or something in the App. And if it is an App timer, how frequently does it scan?