Internet of Things Lab Manual
ISBN 9788119221745

Highlights

Notes

  

Chapter 4: Servo Motors

Servo Motor:

A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.[1]‌ It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, often a dedicated module designed specifically for use with servomotors.

Servomotors are not a specific class of motor, although the term servomotor is often used to refer to a motor suitable for use in a closed-loop control system.

Servomotors are used in applications such as robotics, CNC machinery or automated manufacturing.

A servomotor is a closed-loop servomechanism that uses position feedback to control its motion and final position. The input to its control is a signal (either analogue or digital) representing the position commanded for the output shaft.

The motor is paired with some type of position encoder to provide position and speed feedback. In the simplest case, only the position is measured. The measured position of the output is compared to the command position, the external input to the controller. If the output position differs from that required, an error signal is generated which then causes the motor to rotate in either direction, as needed to bring the output shaft to the appropriate position. As the positions approach, the error signal reduces to zero and the motor stops.

Program 4.1: To interface servo motor with Arduino and write a program to sweep a servo back andforth through its full range of motion.

Component: Arduino Uno, Positional Micro Servo

Circuit Diagram:

Code:

#include <Servo.h>

int servoPin = 9; int servoPos = 45;

int servoInitalPos = 180;

Servo myServo;

void setup()

{

myServo.attach(servoPin);

}

void loop()

{

int pos = 0;int dtwait=15;

for(pos = 0; pos < 180; pos += 1) {myServo.write(pos);

delay(dtwait);

}

for(pos = 180; pos>=1; pos -= 1) {myServo.write(pos);

delay(dtwait);

}

}

Output:

Program 4.2: To interface servo motor, Photo resistor (LDR) with Arduino and write a program tosweep a servo back and forth through its full range of motion depending on light.

Component:

Circuit Diagram:

Code:

#include <Servo.h>

int servoPin = 6;int servoPos = 0;int red = 8;

int green = 9;int yellow = 10;Servo myServo;

void setup()

{

pinMode(red, OUTPUT);pinMode(green, OUTPUT);pinMode(yellow, OUTPUT);myServo.attach(servoPin);Serial.begin(9600);

}

void loop()

{

Serial.println(analogRead(A1));Serial.println(analogRead(A1));

if (analogRead(A1) <= 700 && analogRead(A1) >595){digitalWrite(red, HIGH);

digitalWrite(green, LOW);digitalWrite(yellow, LOW);myServo.write(180);

}

else if (analogRead(A1) <= 595 && analogRead(A1) > 200){

digitalWrite(red, LOW);digitalWrite(green, HIGH);digitalWrite(yellow, LOW);myServo.write(90);

}

else{

digitalWrite(red, LOW);digitalWrite(green, LOW);digitalWrite(yellow, HIGH);myServo.write(0);

}

}

Output:

Program 4.3: Write a program to control two DC motors with Arduino.

Component:

Theory:

DC Motor:

A direct current (DC) motor is a type of electric machine that converts electrical energy into mechanical energy. DC motors take electrical power through direct current, and convert this energy into mechanical rotation.

DC motors use magnetic fields that occur from the electrical currents generated, which powers the movement of a rotor fixed within the output shaft. The output torque and speed depend upon both the electrical input and the design of the motor.

Circuit Diagram:

Code:

int speedPin = 11;int speedPin2 = 3;int dir1= 9;

int dir2= 10;

int dir3= 5;

int dir4= 6;

void setup()

{

pinMode(speedPin, OUTPUT);pinMode(speedPin2, OUTPUT);pinMode(dir1, OUTPUT);pinMode(dir2, OUTPUT);pinMode(dir3, OUTPUT);pinMode(dir4, OUTPUT);

}

void loop()

{

digitalWrite(dir1, HIGH);digitalWrite(dir2, LOW);digitalWrite(dir3, LOW);digitalWrite(dir4, HIGH);analogWrite(speedPin,100);analogWrite(speedPin2,100);

}

Output:

Program 4.4: To interface IR remote with Arduino Write a program to create useful commands fromthe IR remote control.

Component: Arduino Uno, IR Sensor

Theory:

(InfraRed remote control) A handheld, wireless device used to operate audio, video and other electronic equipment within a room using light signals in the infrared (IR) range.

Infrared light requires line of sight to its destination. Low-end remotes use only one transmitter at the end of the unit and have to be aimed directly at the equipment.

High-quality remotes have three or four powerful IR transmitters set at different angles to shower the room with signals.

Circuit Diagram:

Code:

#include <IRremote.h>

int IRpin = A0; IRrecv IR(IRpin);

decode_results res;

void setup()

{

Serial.begin(9600);IR.enableIRIn();

IR.blink13(true);

}

void loop()

{

while(IR.decode(&res) == 0){

}

IR.resume();

if (res.value == 0xFD08F7){Serial.print(“Hex Value: ”);Serial.println(res.value,HEX);

}

else if (res.value == 0xFD8877){Serial.print(“Hex Value: ”);Serial.println(res.value,HEX);

}

else if (res.value == 0xFD48B7){Serial.print(“Hex Value: ”);Serial.println(res.value,HEX);

}

}

Output:

Program 4.5: To interface RGB led and IR remote, write a program to control RGB led with IRremote.

Component:

Circuit Diagram:

Code:

#include <IRremote.h>

int IRpin = A0;IRrecv IR(IRpin);

decode_results res;int red = 12;

int blue = 11;int green = 10;

void setup()

{

Serial.begin(9600);IR.enableIRIn();

IR.blink13(true);pinMode(red, OUTPUT);pinMode(blue, OUTPUT);pinMode(green, OUTPUT);

}

void loop()

{

while(IR.decode(&res) == 0){

}

//Serial.println(res.value);

//Serial.print(“Hex Value: ”);Serial.println(res.value,HEX);IR.resume();

if (res.value == 0xFD08F7){digitalWrite(red, HIGH);

digitalWrite(blue, LOW);digitalWrite(green, LOW);

}

else if (res.value == 0xFD8877){digitalWrite(red, LOW);digitalWrite(blue, HIGH);digitalWrite(green, LOW);

}

else if (res.value == 0xFD48B7){digitalWrite(red, LOW);digitalWrite(blue, LOW);digitalWrite(green, HIGH);

}}

Output:

Exercise: Write a program to control DC motor speed with an IR remote