Processing Motor Control
Processing Motor Control
Servos are usually very powerful motors because of a gearbox that translates power and
speed from the motor to the output shaft. With only minor modifications, a servo can be
hacked and used as an inexpensive continuous rotation gearbox motor. See the Seattle
Robotics Societys tutorial on how to do so:
http://www.seattlerobotics.org/guide/servohack.html
A good link for buying inexpensive DC gearbox motors (cheaper than modifying a servo)
is this: http://www.solarbotics.com/motors_accessories/gear_motors/
Pages 121-123 in Sullivan and Igoes book Physical Computing also has a more in
depth discussion of servo motors.
2a) Test your servo motor
Setup the servo and upload the code from the following example:
http://arduino.cc/en/Tutorial/Sweep
Servo motor power requirements
In contrast to DC motors which have varying power requirements, a servo motor almost
always requires a voltage between 4.5 and 6VDC. If you power the servo from a power
supply of less than +5V, that you will need to put a 1kOhm resistor between the I/O line
and the servo's control line. If you plan to use the same power supply for the Arduino
and the servo, make sure that it can supply at 1000mA of current. Especially if you plan
to control more than one servo you will need an external power supply servos can be
quite power-demanding! If you are using two different power supplies, one for the
Arduino and one for the servo make sure to connect both grounds (GND) together.
Since the power supply most of you have for this class provides 9V @ 1000mA, we need
to find a way to convert the 9V into 5V to be able to power the servo, the 7805 voltage
regulator IC does exactly this.
Here is a Fritzing sketch that illustrates how you connect the servo to an Arduino board:
You could also use the Arduinos on-board voltage regulator and get the 5VDC power
for the servo from the Arduions 5V output. However, larger servos draw more current
and may overheat and damage the smaller on board voltage regulator.
2b) Processing to Servo
Now after setting up the Arduino circuit, lets control the servos rotation angle with a
slider in Processing. We will use firmata for the communication between Processing and
Arduino and have the servo library handle the servo control from the Arduino board. Just
upload the Servo firmata onto your Arduino board, in Arduino go to
File > Examples > Firmata > Servo Firmata
With this setup make sure your Arduino board is powered with the external power
supply!
Arduino code for this project: upload the SimpleAnalogFirmata example File >
Examples > Firmata > SimpleAnalogFirmata
Processing code
(allows you to control the speed of the motor with a GUI slider):
import processing.serial.*;
import cc.arduino.*;
import controlP5.*;
ControlP5 controlP5;
Arduino arduino;
Please read pp.255 -260 in OSullivan and Igoes book Physical Computing for more
details on how to use the SN754410 motor driver IC with a microcontroller. Here is a
circuit diagram for how to interface the SN754410 with the Arduino board:
And here is a picture what this should look like on your breadboard:
Processing code (make sure you have the standard firmata code uploaded onto your
Arduino board):
import processing.serial.*;
import cc.arduino.*;
import controlP5.*;
ControlP5 controlP5;
Arduino arduino;
int DC_speed = 150; // 0-255
int direction = 1; // 0: backward, 1: forward
void setup() {
size(400,400);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);
for (int i = 0; i <= 13; i++)
arduino.pinMode(i, Arduino.OUTPUT);
// pin3: PWM, pin 6: 1A, pin 7: 2A (see SN754410 datasheet)
controlP5 = new ControlP5(this);
controlP5.addSlider("DC_speed",0,255,DC_speed,20,10,255,20);
Radio r = controlP5.addRadio("radio",20,50);
//r.deactivateAll();
// use deactiveAll to NOT make the first radio button active.
r.add("forward",0);
r.add("backward",1);
}
void draw() {
arduino.analogWrite(3, DC_speed);
if (direction == 1) { // run in one direction, i.e. forward
arduino.digitalWrite(6, 1);
arduino.digitalWrite(7, 0);
}
else { // run in the opposite direction, i.e. backward
arduino.digitalWrite(6, 0);
arduino.digitalWrite(7, 1);
}
}
void radio(int theID) {
switch(theID) {
case(0):
direction = 1; // forward
break;
case(1):
direction = 0; // backward
break;
}
}
If you are interested in the kinetic and mechanic aspects of motor output, please read
pp.271-283 Basic Mechanics in OSullivan and Igoes book Physical Computing.
LEGO technic pieces are also a great way to experiment with kinetic systems and
Arduino-controlled motors. Look at these nice tutorials for a start:
http://www.clear.rice.edu/elec201/Book/legos.html
http://sariel.pl/2009/09/gears-tutorial/
http://neuron.eng.wayne.edu/LEGO.../lego_building_tutorial.pdf
You can buy inexpensive used LEGO pieces here:
http://www.bricklink.com/
Look specifically for gears and LEGO technic bricks (the ones with the holes). Also good
are the LEGO 9V motors (part#2838c01) its safe to buy them right away if you find
them in good working condition for around $5.00.