Controlling DC Motors with Blynk

I am a beginner with blynk and recently wrote some code to control two DC motors on Raspberry Pi 2. The code works perfectly fine but i can’t find a way to control them using blynk.

I control the motors with the keys “w a s d” with a l293d motor driver IC.

Here is the code:

#!/usr/local/bin/python
import RPi.GPIO as GPIO
from time import sleep
import time
import os
import sys
from msvcrt import getch
import termios
import tty

TERMIOS=termios
f_t = "MOVING FORWARDS"
b_t = "MOVING BACKWARDS"
a_t = "TURNING ANTI-CLOCKWISE"
c_t = "TURNING CLOCKWISE"

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
 
Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 19
Motor2B = 21
Motor2E = 23

delay = 0.2
 
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
 
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
                c = os.read(fd, 1)
        finally:
                termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

def forwards():
    print(f_t)
    GPIO.output(Motor1A,GPIO.HIGH)
    GPIO.output(Motor1B,GPIO.LOW)
    GPIO.output(Motor1E,GPIO.HIGH)
    
    GPIO.output(Motor2A,GPIO.LOW)
    GPIO.output(Motor2B,GPIO.HIGH)
    GPIO.output(Motor2E,GPIO.HIGH)

    time.sleep(delay)
    
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.LOW)

def backwards():
    print(b_t)
    GPIO.output(Motor1A,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.HIGH)
    GPIO.output(Motor1E,GPIO.HIGH)
    GPIO.output(Motor2A,GPIO.HIGH)
    GPIO.output(Motor2B,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.HIGH)

    time.sleep(delay)
    
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.LOW)

def t_a():
    print(a_t)
    GPIO.output(Motor2A,GPIO.HIGH)
    GPIO.output(Motor2B,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.HIGH)
    GPIO.output(Motor1A,GPIO.HIGH)
    GPIO.output(Motor1B,GPIO.LOW)
    GPIO.output(Motor1E,GPIO.HIGH)

    time.sleep(0.25)
    
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.LOW)

def t_c():
    print(c_t)
    GPIO.output(Motor2A,GPIO.LOW)
    GPIO.output(Motor2B,GPIO.HIGH)
    GPIO.output(Motor2E,GPIO.HIGH)
    GPIO.output(Motor1A,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.HIGH)
    GPIO.output(Motor1E,GPIO.HIGH)

    time.sleep(delay)
    
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.LOW)
count=0
while count < 1:
    key=getkey()
    if key=="w":
        forwards()
    elif key=="s":
        backwards()
    elif key=="a":
        t_a()
    elif key=="d":
        t_c()
    elif key=="e":
        print("EXIT")
        GPIO.output(Motor1E,GPIO.LOW)
        GPIO.output(Motor2E,GPIO.LOW)
        sys.exit(0)
    else:
        print("no key")
        GPIO.output(Motor1E,GPIO.LOW)
        GPIO.output(Motor2E,GPIO.LOW)
    count=0

I want to switch on and off the motor from the app on android.

Thanks for any help :smile:

After playing around with Blynk for a couple of weeks, I have come to realize that the true power of Blynk lies in the virtual pins.

Sure it’s great fun to just open an app and change a couple settings and be ready to flash an led, or read an input. But the direct control features are limited. Using the direct control you are also relying on a connection to the Blynk server. No connection to the server and your device looses it’s settings on power cycle.

For your application, you should take a look at the following tuttorial that @vshymanskyy posted.

Good Luck :smile:

1 Like

I read the tutorial and it was pretty good but I still could not figure out how to control the DC motor as code uses a master enable pin and two other other pins A and B that control the direction of the motor.