Build Your Own Robot Arm |
However, the majority of tutorials are based on the USB model. The one I have comes with a simple switch box control, not USB.
So, time to control the motors directly. All five of them.
I read a few tutorials before getting going and soon realised that connecting the motors willy nilly to the GPIO pins was probably not a good idea. There appear to be a number of ways of connecting the motors, but which was going to be best was a bit of a mystery. I plumped for getting started with a L298N H Bridge driver for no reason other than I had seen this working in another blog post and it seemed to be safe and foolproof.
I bought the L298N driver board from ebay but it didn't come with a datasheet. It looks a bit like this one, so I used this as a basis for connecting it up.
L298N motor driver |
I hooked up the +ve and -ve leads from the robotic arm battery pack (containing 4 D cells ~ 6V) to the +12V power and power GND terminals. The +5V power terminal was left unconnected - I believe this is an output. I then connected the yellow and black leads from the jaws motor to the output A terminals.
Next, the raspberry pi was connected. One GND pin was connected to the same GND as the robotic arm battery pack. Two GPIO pins (23 & 24) were connected to the two input pins on the driver board corresponding to 'output A'.
Some simple python code was then written to enable the pins, turn the motor clockwise, anti-clockwise and stop. This verified that all connections were working ok.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# set pins as output
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
# spin motor one way for 0.2 seconds
GPIO.output(23,True)
GPIO.output(24,False)
time.sleep(0.2)
# stop the motors for 1 second
GPIO.output(23,False)
GPIO.output(24,False)
time.sleep(1)
# spin motor the other way for 0.2 seconds
GPIO.output(23,False)
GPIO.output(24,True)
time.sleep(0.2)
# stop the motors
GPIO.output(23,False)
GPIO.output(24,False)
import time
GPIO.setmode(GPIO.BCM)
# set pins as output
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
# spin motor one way for 0.2 seconds
GPIO.output(23,True)
GPIO.output(24,False)
time.sleep(0.2)
# stop the motors for 1 second
GPIO.output(23,False)
GPIO.output(24,False)
time.sleep(1)
# spin motor the other way for 0.2 seconds
GPIO.output(23,False)
GPIO.output(24,True)
time.sleep(0.2)
# stop the motors
GPIO.output(23,False)
GPIO.output(24,False)
After this success, the other four motors were connected up. One to the other side of the existing L298N driver, two to a separate L298N driver and the final one to a L293D driver. The L293D driver came as just a chip and is not mounted on a board with other components. It looks like this:
L293D motor driver |
I then wrote a python script which allowed the robotic arm to be controlled via keyboard presses (via pygame). The code is available on github robotarm.py
And here is the robotic arm in action:
Great, so I have replaced the perfectly adequate control box that came with the arm with a wiring monstrosity! But the project has lead to a better understanding of interfacing and the research required has pointed me towards many new resources to learn from.
Questions which I now wish to pursue are:
- What alternative interfaces could be used to connected a DC motor?
- What is an H Bridge?
- What are the main differences between the L298N and L293D?
- What other motor drivers are popular among robotic enthusiasts?
- What other motors are possible?
No comments:
Post a Comment