Friday 11 January 2019

Arduino Tutorial #7 : UnoArduSim - SERVO Motor

Arduino Tutorial #7:

UnoArduSim - SERVO Motor 

     

          Previously we have look at using UnoArduSim to control an LED. Today we will look at how we can control the servo motor by using the UnoArduSim simulation tool. But before that, let's look at the theory of servo motor. 






            Servo motor comes with 3 wires, for power, ground and signal. The rotation of the motor is decided by the signal. Without the signal, the servo will not rotate. Inside a servo motor, there is a DC motor, potentiometer and control circuit. The potentiometer reading changes as the motor rotate. The motor is attached by gears to control the wheel. The control circuit will precisely regulate how much movement there is and in which direction. This is a very brief introduction to the servo motor. In our upcoming blog, we will look into the details. For now, we will stop here on we move on to the coding. 



          Once we have known the theory, we can start to code. Below is the code which we have used.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <Servo.h>

Servo myservo;  

int potpin = 0; //assigned to A0
int val;    

void setup() {
  myservo.attach(9);  // servo signal is assigned to Pin 9
}

void loop() {
  val = analogRead(potpin);           
  val = map(val, 0, 1023, 0, 180);    
  myservo.write(val);                  
  delay(15);                           
}

You may copy and try to run the code.


           The next step is to simulate the code which we have created. The first step to do is upload the INO file into the software. The image below shows how this can be done.



                  Once the code is uploaded. the next step is to assign the pin accordingly. As per our code, we have assigned the A0 as the analogue value input and pin 9 as the signal output to the servo. The image below describes how we can achieve this.





                      That's all required for the setup. Now we are ready to run the simulation. Click the run button or use the shortcut key F9. Below is the result.




                        As we can see, the servo motor responds to the value of the analogue input.

Finally, below is the tutorial video of this blog.













No comments:

Post a Comment