Monday, November 17, 2014

Let's make the DC Spin

Hello person reading this blog.

You will learn how to set up and control a DC motor with the BeagleBone Black RevB (BBB) and L293D IC.

Assumption

  1. You have a BBB RevB (I don't know if the stuff I say or do will work on other version)
  2. You have already setup your OS on the BBB and have it configured the way you want. I have a tut for getting a TP-Link TL-WN722N USB wireless working if you want to do wireless. 
  3. You have a basic understanding of circuits. I probably won't do circuit diagrams but you never know I might get creative. 
  4. You know Python 2.X it is different than Python 3.X
  5. You know how to run Python code on the BBB
  6. You have some type of bread board and jumper wires.
  7. You have installed the Adafruit Python Library

You will need

Your Bread Board, A L293D IC, A DC motor, 5v power supply/battery pack, and Jumper wires.

Step 1 Spin that Motor

Step 1 Wiring

Overview

In Step 1 we are simply turning on the motor. Double check your wires before you supply power to the IC. IF the motor does not start spinning once you apply power from the BBB and the 5v power supply UNPLUG EVERYTHING and double check that the wires are correct and that the power from the BB and power supply are correct. We just setup the IC's inputs to send the power to the motor to cause it to spin at full speed in one direction.

Lets try something: 
Move the orange wire from power to ground leaving it connected to pin 2 of the IC and then move the purple wire from ground to power leaving it connected to pin 7. Notice that the motor starts spinning full speed in the opposite direction.

Step 2 Control the Speed of the Motor

Step 2 Wiring


Move the Blue wire from power to P9_14.

Code:

import Adafruit_BBIO.PWM as PWM

enablePin = "P9_14"

PWM.start(enablePin, 100)

delay = 60
while int(delay) <= 100 && int(delay) >= 0:
  PWM.set_duty_cycle(enablePin, float(delay))
  delay = raw_input("Speed of Motor (0-100):")

PWM.stop(enablePin)
PWM.cleanup()

Overview

In step 1 we set the EN pin on  L293D (Pin 1) to on always. In this step we are turning it on and off using the duty cycle. From 0% to 100% speed. As you put in different numbers you will find that there is a certain point, below which your motor no longer spins. For me that happened at around 53.

Step 3 Control the Direction of the Motor

Step 3 Wiring

Changes from Step 1: Move the Purple wire from ground into P9_11, and move Orange wire from power into P9_13.

Code:

import Adafruit_BBIO.GPIO as GPIO

in1Pin = "P9_11"
in2Pin = "P9_13"

GPIO.setup(in1Pin, GPIO.OUT)
GPIO.setup(in2Pin, GPIO.OUT)

GPIO.output(in1Pin, GPIO.HIGH)
GPIO.output(in2Pin, GPIO.LOW)

rev = 60
while int(rev) == 1 || int(rev) == 0:
    rev = raw_input("Direction: 0,1")
    GPIO.output(in1Pin, int(rev) == 1)
    GPIO.output(in2Pin, int(rev) != 1)

GPIO.cleanup()

Overview

In step 2 we turned the EN switch on and off, using PWM, to control the speed of the motor. In this step we control the motors direction based on the value sent to the GPIO pins. According to the L293D documentation the two GPIO pins must be opposite for the motor to run other wise the motor is set to brake mode meaning if it was running it will do a hard stop. 

Final Step Control it ALL

Final Wiring

Changes from step 1: Move the Purple wire from Ground into P9_11, Orange wire from Power into P9_13, Blue wire from Power into P9_14.

Code:

import Adafruit_BBIO.PWM as PWM
import Adafruit_BBIO.GPIO as GPIO

enablePin = "P9_14"
in1Pin = "P9_11"
in2Pin = "P9_13"

GPIO.setup(in1Pin, GPIO.OUT)
GPIO.setup(in2Pin, GPIO.OUT)

PWM.start(enablePin, 100)

GPIO.output(in1Pin, GPIO.HIGH)
GPIO.output(in2Pin, GPIO.LOW)

delay = 60
while int(delay) <= 100 && int(delay) >= 0 && (rev == 1 || rev == 0):
  PWM.set_duty_cycle(enablePin, float(delay))
  delay = raw_input("Speed of Motor (0-100):")
  if int(delay) <= 100:
    rev = raw_input("Direction: 0,1")
    GPIO.output(in1Pin, int(rev) == 1)
    GPIO.output(in2Pin, int(rev) != 1)

PWM.stop(enablePin)
PWM.cleanup()
GPIO.cleanup()

Overview

We put what we learned from the other steps to put it all together. Now go forth and control the world one motor at a time. 

No comments:

Post a Comment