Servo Motor Control Using Arduino and Processing

Introduction: Servo Motor Control Using Arduino and Processing

About: An Electronics engineer and a hobbyist. I love to keep experimenting with microcontrollers.

Hello everyone, today we control servo motor using the movement of our mouse's cursor. This project will be done using processing software. I have already made few other projects using processing software so you can check that out. Now coming to this project, you can use this project to create a small robot arm whose movement can be controlled by your cursor.

So let's begin...

Step 1: Getting the Supplies

I would recommend you to buy the components from UTSource.net . They provide high quality components at affordable rates. They also provide PCB Services for PCB's from single layer to 16 layers. And all these things will be delivered at your doorstep on time. So do check them out.

Things we need to make this project -

1. Arduino Uno

2. Servo Motors x 2

3. Connecting Wires

4. Double Sided Tape

5. Popsicle Stick (or mechanix game part)

Step 2: Connecting Everything Together

I haven't included a circuit diagram here because the connections are very easy to understand.

Connect the VCC (red) and GND (brown) wires of both the servos to the 5V and GND pin of the Arduino.

Now connect the signal pin(yellow) of the first servo to digital pin 9 and the signal pin of second servo to digital pin 10.

Servo 1 >> Arduino

VCC >> 5 V

GND >> GND

SIG >> D9

Servo 2 >> Arduino

VCC >> 5 V

GND >> GND

SIG >> D10

Now place one servo on the top of the other and fix them using some double sided tape or hot glue.

Refer to the image above.

Now attach a small Popsicle stick / a mechanix game part to the shaft of the upper motor to see the movements properly.

Step 3: The Processing Code

Install the Processing Software by clicking HERE.

Now paste the code given below to the IDE.

//Processing code:
import processing.serial.*; int xpos=90; // set x servo's value to mid point (0-180); int ypos=90; // and the same here Serial port; // The serial port we will be using void setup() { size(360, 360); frameRate(100); println(Serial.list()); // List COM-ports // You will want to change the [1] to select the correct device // Remember the list starts at [0] for the first option. port = new Serial(this, Serial.list()[0], 57600); } void draw() { fill(175); rect(0,0,360,360); fill(255,0,0); //rgb value so RED rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height fill(0,255,0); // and GREEN rect(175, 180, 10, mouseY-180); update(mouseX, mouseY); } void update(int x, int y) { //Calculate servo postion from mouseX xpos= x/2; ypos = y/2; //Output the servo position ( from 0 to 180) port.write(xpos+"x"); port.write(ypos+"y"); }

This won't work until your Arduino is connected to the PC.

Step 4: The Arduino Code

Now copy the Arduino code given below and paste it in your Arduino sketch. Compile and upload the code to your Arduino Board.

#include   
char tiltChannel=0, panChannel=1; Servo servoTilt, servoPan; char serialChar=0; void setup() { servoTilt.attach(9); //The Tilt servo is attached to pin 9. servoPan.attach(10); //The Pan servo is attached to pin 10. servoTilt.write(90); //Initially put the servos both servoPan.write(90); //at 90 degress. Serial.begin(57600); //Set up a serial connection for 57600 bps. } void loop(){ while(Serial.available() <=0); //Wait for a character on the serial port. serialChar = Serial.read(); //Copy the character from the serial port to the variable if(serialChar == tiltChannel){ //Check to see if the character is the servo ID for the tilt servo while(Serial.available() <=0); //Wait for the second command byte from the serial port. servoTilt.write(Serial.read()); //Set the tilt servo position to the value of the second command byte received on the serial port } else if(serialChar == panChannel){ //Check to see if the initial serial character was the servo ID for the pan servo. while(Serial.available() <= 0); //Wait for the second command byte from the serial port. servoPan.write(Serial.read()); //Set the pan servo position to the value of the second command byte received from the serial port. } //If the character is not the pan or tilt servo ID, it is ignored. }

Now go back to processing IDE and click on the run button.

A small screen with two lines (red and green) will appear as you move your cursor over that screen.

This will send values to the Arduino Board and will rotate the servo's according to the mouse's movements.

Step 5: Working of the Project

I have attached a small video of the working of this project. You can implement this similar method to control a robot arm. If you want to see that project here then comment below. That's it for today. See you'll with another project soon. Thank you.

Attachments

Be the First to Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Automation Contest

      Automation Contest
    • Make it Glow Contest

      Make it Glow Contest

    Comments

    0
    solidangelo
    solidangelo

    Question 1 year ago on Step 5

    Hi! i just want to know how to integrate the servo code to my existing code, because i want to add some features of this lights.Hope you can help me.Thank you.
    This is my code:
    import cc.arduino.*;
    import org.firmata.*;

    import cc.arduino.*;
    import org.firmata.*;

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import processing.serial.*;
    import cc.arduino.*;

    Arduino arduino;

    Minim minim;
    AudioPlayer song;
    FFT fft;

    int redPin1 = 12;
    int greenPin1 = 11;
    int bluePin1 = 10;

    int redPin2 = 9;
    int greenPin2 = 7;
    int bluePin2 = 8;

    int redPin3 = 6;
    int greenPin3 = 4;
    int bluePin3 = 5;

    int whitePin1 = 22;
    int yellowPin1 = 24;

    int color_id = 0;

    int common_cathode = 0;

    void setup() {
    size(800, 600);

    arduino = new Arduino(this, "COM8", 57600);
    for (int i = 0; i <= 13; i++) arduino.pinMode(i, Arduino.OUTPUT);
    for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.LOW);

    minim = new Minim(this);
    song = minim.loadFile("KPOP PLAYLIST 2019 [ DANCE, GYM, PARTY ETC...].mp3");
    song.play();
    fft = new FFT(song.bufferSize(), song.sampleRate());
    }

    void draw() {
    background(#151515);

    fft.forward(song.mix);

    strokeWeight(1.3);
    stroke(#FFF700);

    // frequency
    pushMatrix();
    translate(250, 0);
    for(int i = 0; i < 0+fft.specSize(); i++) {
    line(i, height*4/5, i, height*4/5 - fft.getBand(i)*4);
    if(i%100==0) text(fft.getBand(i), i, height*4/5+20);
    if(i==200) {
    if(fft.getBand(i)>2) {
    setColor1(255,255,0);
    setColor3(255,255,0);
    }
    else if(fft.getBand(i)>1) {
    setColor1(255,0,255);
    setColor3(255,0,255);
    } else {
    setColor1(255,255,255);
    setColor3(255,255,255);
    }
    }
    if(i==50) {
    if(fft.getBand(i)>5) {
    color_id = (color_id+1)%4;
    } else if(fft.getBand(i)>3) {
    if(color_id==0) setColor2(0,255,0);
    else if(color_id==1) setColor2(0,255,255);
    else if(color_id==2) setColor2(0,0,255);
    else setColor2(255,0,0);
    }
    else {
    setColor2(255,255,255);
    }
    }
    }
    popMatrix();

    stroke(#FF0000);

    //waveform
    for(int i = 250; i < song.left.size() - 1; i++) {
    line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
    line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
    line(i, 250 + song.mix.get(i)*50, i+1, 250 + song.mix.get(i+1)*50);
    }

    noStroke();
    fill(#111111);
    rect(0, 0, 250, height);

    textSize(24);
    fill(#046700);
    text("left amplitude", 10, 50);
    text("right amplitude", 10, 150);
    text("mixed amplitude", 10, 250);
    text("frequency", 10, height*4/5);
    }

    void stop()
    {
    for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.LOW);
    song.close();
    minim.stop();
    super.stop();
    }
    void setColor1(int red, int green, int blue)
    {
    if(common_cathode==1) {
    redPin1 = 255-red;
    greenPin1 = 255-green;
    bluePin1 = 255-blue;
    }
    arduino.digitalWrite(redPin1, red);
    arduino.digitalWrite(greenPin1, green);
    arduino.digitalWrite(bluePin1, blue);
    }
    void setColor2(int red, int green, int blue)
    {
    if(common_cathode==1) {
    red = 255-red;
    green = 255-green;
    blue = 255-blue;
    }
    arduino.digitalWrite(redPin2, red);
    arduino.digitalWrite(greenPin2, green);
    arduino.digitalWrite(bluePin2, blue);
    }
    void setColor3(int red, int green, int blue)
    {
    if(common_cathode==1) {
    red = 255-red;
    green = 255-green;
    blue = 255-blue;
    }
    arduino.digitalWrite(redPin3, red);
    arduino.digitalWrite(greenPin3, green);
    arduino.digitalWrite(bluePin3, blue);
    }
    void setColor4(int white, int yellow)
    {
    if(common_cathode==1) {
    white = 255-white;
    yellow = 255-yellow;
    }
    arduino.digitalWrite(whitePin1, white);
    arduino.digitalWrite(yellowPin1, yellow);

    }