• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Raspberry Pi - GamesController - DC Motor Control

Dominant1989

New Member
Good Morning Everyone,

I am new to the forum and would just like to say hello to everyone,

So i thought i would first start with something that i'm really struggling with and that would be the use of a Raspberry Pi Controller via Raspberry Pi to control these DC motor's. My aim from this project I am working on is to be able to make a RC tank that I can control with the wifi controller and have the Raspberry Pi be used as the way of outputting the signals if there are other options that might be easier to use that would be great.

This is the set up I have at the moment:
119962




Below is the code i was able to use from a simple tutorial to actually make the Motors move, which they do but i now want to go a bit further in depth with this code and take it to the next step and actually use it with the Game controller, any help, pointers in the right direction for me to do some reading would be greatly appreciated.

Something I know with this code is that once i kill the code, I then have a dc motor continue to go which is something I would also like to stop.


Python:
#DC motor control with Adafruit TB6612FNG dual h-bridge motor controller

from time import sleep  #Import sleep from time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False);

#PWM Frequency
pwmFreq = 100

delay = 2
micro_delay = 0.25

#Setup Pins for motor controller
GPIO.setup(12, GPIO.OUT) #PWMA
GPIO.setup(18, GPIO.OUT) #AIN2
GPIO.setup(16, GPIO.OUT) #AIN1
GPIO.setup(22, GPIO.OUT) #STBY
GPIO.setup(15, GPIO.OUT) #BIN1
GPIO.setup(13, GPIO.OUT) #BIN2
GPIO.setup(11, GPIO.OUT) #PWMB

pwma = GPIO.PWM(12, pwmFreq) #pin 18 to PWM
pwmb = GPIO.PWM(11, pwmFreq) #pin 13 to PWM
pwma.start(100)
pwmb.start(100)

## ---------------- FUNCTIONS --------------- ##
def forward(spd):
    runMotor(0, spd, 0)
    runMotor(1, spd, 0)

def reverse(spd):
    runMotor(0, spd, 1)
    runMotor(1, spd, 1)

def turnLeft(spd):
    runMotor(0, spd, 0)
    runMotor(1, spd, 1)

def turnRight(spd):
    runMotor(0, spd, 1)
    runMotor(1, spd, 0)

def runMotor(motor, spd, direction):
    GPIO.output(22, GPIO.HIGH);
    in1 = GPIO.HIGH
    in2 = GPIO.LOW

    if (direction ==1):
        in1 = GPIO.LOW
        in2 = GPIO.HIGH

    if (motor == 0):
        GPIO.output(16, in1)
        GPIO.output(18, in2)
        pwma.ChangeDutyCycle(spd)
    elif(motor == 1):
        GPIO.output(15, in1)
        GPIO.output(13, in2)
        pwmb.ChangeDutyCycle(spd)

def motorStop():
    GPIO.output(22, GPIO.LOW)

## MAIN

def main(args=None):
    while True:
        forward(50)
        sleep(delay)
        motorStop()
        sleep(micro_delay)

        reverse(50)
        sleep(delay)
        motorStop()
        sleep(micro_delay)

        turnLeft(50)
        sleep(delay)
        motorStop()
        sleep(micro_delay)

        turnRight(50)
        sleep(delay)
        motorStop()
        sleep(micro_delay)

if __name__ == "__main__":
    main()
Many Thanks in Advance

Aaron.
 

ronsimpson

Well-Known Member
Most Helpful Member
Welcome.
I like the clear pictures. Good information in the links. You know how to insert code. "Python" thankyou, some people never say.
What H-bridge are you using?
 

Dominant1989

New Member
"I kill the code" explain. how?
Once I close the terminal, of the code the DC motor continues to run, However I have made some progress and managed to find a golden oldy by a person called Freddy:

Youtube Video

Within his code, it allows for me to control the two dc motors I have in the sequence I was after so I thought it would be best to share as it took me two days to get anywhere near.
 

EE World Online Articles

Loading

 
Top