Control motor with blynk but motor stops when it hits either (of the 2) push buttons


Hi guys,

I new to Arduino and Blynk and I’m trying to build something. I’m using Arduino Uno R3, L298N, ESP8266 with AI and 2 push buttons (later I want to add a camera). I want to control the motor via Blynk which will slide an item vertically. When the item hits a push button, I’d like the motor to cutoff. with the button still depressed, i’d like to be able to move the item the opposite direction.

Is this possible? Please could someone help, i think I understand the connections but have no idea about coding.

Thank you all for you responses in advance.

void loop()


Each of the physical limit switches should be wired to separate GPIO pins on the UNO, and the other side of the switches wired to ground (if the switches are Normally Open). The corresponding GPIO pins will go HIGH when the switch is closed.
If you’re using Normally Closed switches then wire the other side of the switches to the +5v pin and the corresponding pin will be pulled LOW when the switch is activated.

With the code, you have two options. I’ll assume that you’re using Normally Open limit switches:

  1. use a timer (or timers) to check the state of the GPIO pins if either of the pins go HIGH then cut the power to the motor. I assume you’ll be using a relay, connected to a different GPIO pin to control the motor, but you could be using a motor controller shield or a high power transistor/MOSFET to do this.

  2. use software interrupts on the pins that the limit switches are attached to. With a Normally Open limit switch that would be a rising interrupt. When the interrupt is triggered by the switch then a software function would be executed.

As you’re using Blynk, I’d say that option (1) would make more sense. The frequency of the timer(s) that check the limit switches will depend on how fast the mechanical device is running and how critical it is that it stops immediately after the limit switch contact is made. Try having your timer(s) run every 100 microseconds. If you’re using two timers then stagger them so that they don’t both try to run at the same time (one set to 100 microseconds and the other at 120 microseconds for example).



Hi Pete,

I’m very new to this (this being my first project), the switches i’m using are normally open. Because i want them both to operate the same, could I run them off the same pin? I have a motor control module (L298N) but not a shield (do I need one, as well?)
It’s critical that the motor stops immediately or it could end up breaking things. I see the Blynk also has the option for video so would like to add a camera which turns on when I open the Blynk project (is that possible?)

As i said i"m new to this and it’s making me feel rather daft:sweat_smile:


@von212 You issue is all about the code… Blynk would simply be a GUI, not the actual driving factor.

Another thing to consider is the type of motor you use. I made a slider prototype once using a stepper motor and HALL sensors & magnets for limit switches.

This is an older project., and a rover not a camera slider, but a motor is a motor… I don’t recall if I since implemented auto stop at connection loss, but this would be something to get you started with mixing motor controller commands and Blynk interface.

And a “shield” is just a fancy way of saying a “form fitting” module. A L298N based motor shield or standalone motor controller is the same technical thing.


I’d do the classic approach with the limit switches like this . It’s saver than doing this part in software. It will stop the motor at the (mechanical ) limit no matter what the software does.


@Gunner, Thanks, your rover is really cool.I will try what you have recommended tonight. Hopefully I manage to get it up and running. @IBK, I agree that it would be safer to rely on the mechanical side, so will try adding diodes into the circuitry.

Thank you both for your suggestions