LESSON 12 - Servo Motor Control with C# and Arduino

In this lesson, I've explained servo motor control with Arduino and SerialCom V1.2. which has programed with C# 2013.
Figure 1 - Servo Motor

Servo Motor
Unlike dc motors, with servo motors you can position the motor shaft at a specific position (angle) using control signal. The motor shaft will hold at this position as long as the control signal not changed. This is very useful for controlling robot arms, unmanned airplanes control surface or any object that you want it to move
at certain angle and stay at its new position.
Servo motors may be classified according to size or torque that it can withstand into mini, standard and giant servos. Usually mini and standard size servo motors can be powered by Arduino directly with no need to external power supply or driver.

How Does the Servo Motor Works?The servo have 3 wires (Figure 2):Black wire: GND (ground)    RED wire:+5v   Colored wire: control signal (PWM)
Figure 2 - Servo Motor Wire

The third pin accept the control signal which is a pulse-width modulation (PWM) signal (Figure 3). It can be easily produced by all micro- controllers and Arduino board. This accepts the signal from your controller that tells it what angle to turn to. The control signal is fairly simple compared to that of a stepper motor. It is just a pulse of varying lengths. The length of the pulse corresponds to the angle the motor turns to. 


Figure 3 - Conrol Signal (PWM)
.  Servo Motor Control with Arduino

Figure 4 - Arduino UNO and Servo Motor

Standard servo motor control using Arduino is extremely easy. This is because the Arduino software comes with a sample servo sketch and servo library that will get you up and running quickly.

1. Connect the black wire from the servo to the Gnd pin on the Arduino
2. Connect the red wire from the servo to the +5V pin on the Arduino
3. Connect the third wire (usually orange or yellow) from the servo to a digital pin on the Arduino

Figure 5 - Arduino Servo Motor Circuit

Arduino Programı
#include <Servo.h>
Servo myservo;
int pos = 0; 
int x;
void setup()
{
     Serial.begin(9600);
     myservo.attach(9);
}
void loop() 
{
    if(Serial.available()) 
     {
         pos = Serial.read();
         if(pos>0)
          {
              x=pos;
           }
      }
  myservo.write(x); 
}

SerialCom V1.2 Program

I programmed the second version of the program named SerialCom (SerialCom V1.2) that we used in previous projects. With this program, the angle of rotation of the servomotor connected to the arduino is controlled (Figure 6).

Figure 6 - SerialCom V1.2.

The TrackBar's moving tip on the form is played and the value is sent to the Arduino via the serial port as the pwm value. The minimum value of the TrackBar must be 0 and the maximum value must be 180. These values ​​are the rotation rating of the servo motor.

C# 2013 Codes

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();//Seri Portu Oku
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port); //Seri Portu Listeye At
            }
            timer1.Enabled = true//Zamanı Görüntülemek için Timer1 Aktif Et
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string x = comboBox1.SelectedItem.ToString();
            serialPort1.PortName = x;
            serialPort1.BaudRate = 9600;
            //serialPort1.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!serialPort1.IsOpen)
                    serialPort1.Open();

            }
            catch
            {
                MessageBox.Show("Seri Port Seçin!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Close(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToLongTimeString();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                int PWM_deger = trackBar1.Value;
                textBox2.Text = trackBar1.Value.ToString();
                // int değeri byte a dönüştürme işlemi.
                byte[] b = BitConverter.GetBytes(PWM_deger);
                serialPort1.Write(b, 0, 4);
            }
            else // gerçekleşmemişse hata mesajı verir.
            {
                textBox2.Text = "Cihaz Bağlı Değil";
            }
        }
    }
}



Do not forget to type " using System.IO.Ports;" when writing the program. This library provides the serialPort object to work.

Project FilesYou can download project files by clickin the links.
Video of  Lesson

 



Hiç yorum yok:

Yorum Gönder