Paja

Icon

Rapid prototyping for music, art and design work

Interfacing servo motor with Arduino

The Arduino (build 0007) analogue output pins (PWM 9-11) have a frequency of approximately 30769 Hz. According to the Parallax documentation (http://www.parallax.com/dl/docs/books/edu/roboticsservomod.pdf) the low time for a pulse train can be between 20-40ms which means that the Arduino pulse is in the range. However, to be consistent with the BS board the frequency can be lowered by the following code =>

TCCR1A = 0x00; // sets timer control bits to PWM Phase and Frequency Correct mode

TCCR1B = 0x12; // sets timer control bits to Prescaler N = 8

ICR1 = 0x07d0; // Upper Timer Limit = 2000 (in hex) equals 2ms

(http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1170282999/2#2)

Having obtained a method by which to regulate the PWM frequency, it is now possible to regulate the servo motor by for instance a potentiometer.
The Arduino board hasn’t sufficient amount of power to run both the servo motor and receive potentiometer input so an extra supply, like a second Arduino board or other 5V power supply, must be added to the wiring. Both GRD cables must be connected to the board receiving the analogue input though.

Servomotor_potentiometer_circuit_diagram

The following code is for the Arduino circuit board:

int val = 0;
int outPin = 9;
int minServoVal = 400;
int maxServoVal = 2000;

void setup(void)
{
Serial.begin(9600);
TCCR1A = 0x00; // sets timer control bits to PWM Phase and Frequency Correct mode
TCCR1B = 0x12; // sets timer control bits to Prescaler N = 8
ICR1 = 0x07d0; // Upper Timer Limit = 2000 (in hex) equals 2ms
}

void loop(void) {
loopNum = loopNum+1;
val = analogRead(0); // read potentiometer, reads 0-1023,
// correct the value to the range of the servo and start with the min value of the servo
val = (val*1.5625)+ minServoVal; //–(2000-400/1024)
analogWrite(outPin,val);
delay(20);

}

Herein a computer can receive the output signal from the Arduino board via serial communication. Processing, or any other frontend, can then graph the signal. Extra signal noise has been eleminated in the following code:

/*
Datalogger
by Tom Igoe

This program takes raw bytes from the serial port at 9600 baud and graphs them.
To start/stop the graph, click the mouse.

No graphing is done when the incoming value is below a constant threshold.
You can only change the threshold in code. I haven’t made a UI for that.

Created 20 April 2005
Updated 5 July 2005
*/

import processing.serial.*;

Serial myPort; // The serial port

// initial variables:
int i = 1; // counter
int inByte = -1; // data from serial port
int inNum = -1;
int minNum = 400;
int maxNum = 2000;
int inVal = 0;
int value = 0;
float correctK = 0;
float decK = 100;
float corrC = 0;
float cVal = 0;
int r = 255;
int g = 255;
int b = 255;
color bgHex = #5A5A46;

void setup () {
size(640, 480); // window size
setFullScreen(true);
setResolution(640,480);
createFullScreenKeyBindings();

correctK = float(maxNum-minNum)*decK;
correctK = correctK/height;
correctK = correctK/decK;

// List all the available serial ports
println(Serial.list());
// I know that the third port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[2].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[4], 9600);

// set inital background:
background(bgHex);

}
void draw () {

if (myPort.available() > 0) {
//inByte = myPort.read();

inNum = int(myPort.readString());
if(inNum >= minNum && inNum = width) {
i = 0;
background(bgHex);
}
else {
i++;
}
}

Note that to achieve fullscreen presentation one must have ‘fullscreen_api.pde’ located in the same project folder. ‘Fullscreen’ can be obtained from:
http://www.superduper.org/processing/fullscreen_api/

And then the result:

(video….)

Facebook Twitter Email

Category: Snippets

Tagged:

Comments are closed.

Social links powered by Ecreative Internet Marketing