For this week I used 'Servo motor' for the Individual assignment & Group assignment since I went to learn more about it to use it in my final project.
I connect the servo motor to my microcontroller board that I have designed.
I used Arduino IDE
to program the servo motor I program it to change the angle from 0 degree to 90 degree to 180 degree.
#include
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(D0,540,2400); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(1000); // waits 15ms for the servo to reach the position
myservo.write(90);
delay(1000); // waits 15ms for the servo to reach the position
myservo.write(180);
delay(1000);
}
• Pulse width: at 0 degree
• Pulse width: at 90 degree
• Pulse width: at 180 degree
Finaly I used 2 servo motor in my final project to track the sun light by changing the X & Y axis
Code: To control all the Output in the Final project.
#include
Servo horizontal; // horizontal servo
int servoh = 180;
int servohLimitHigh = 175;
int servohLimitLow = 5;
Servo vertical; // vertical servo
int servov = 0;
int servovLimitHigh = 60;
int servovLimitLow = 0;
// LDR pin connections
// name = analogpin;
int ldrlt = A3;
int ldrrt = A0;
int ldrld = A2;
int ldrrd = A1;
const int button1 = 7;
const int button2 = 6;
const int motorA = D8;
const int motorB = D7;
int buttonStateA;
int buttonStateB;
int pos = 0;
int pos2 = 0;
int oldvalue;
int oldvalue2;
void setup() {
horizontal.attach(3);
vertical.attach(4);
horizontal.write(180);
vertical.write(0);
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
delay(2500);
}
void loop() {
int ldrStatus = analogRead(ldrlt);
if (ldrStatus > 30) {
buttonStateA = digitalRead(button1);
if (buttonStateA == LOW) {
digitalWrite(motorA, 255); // Counter-clockwise
digitalWrite(motorB, LOW);
} else {
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
}
int lt = analogRead(ldrlt);
int rt = analogRead(ldrrt);
int ld = analogRead(ldrld);
int rd = analogRead(ldrrd);
int dtime = 10;
int tol = 90; // dtime = difference time, tol = tolerance
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the difference of up and down
int dhoriz = avl - avr; // check the difference of left and right
if (-tol > dvert || dvert > tol) {
if (avt > avd) {
servov++;
if (servov > servovLimitHigh) {
servov = servovLimitHigh;
}
} else if (avt < avd) {
servov--;
if (servov < servovLimitLow) {
servov = servovLimitLow;
}
}
vertical.write(servov);
}
if (-tol > dhoriz || dhoriz > tol) {
if (avl > avr) {
servoh--;
if (servoh < servohLimitLow) {
servoh = servohLimitLow;
}
} else if (avl < avr) {
servoh++;
if (servoh > servohLimitHigh) {
servoh = servohLimitHigh;
}
} else if (avl == avr) {
delay(10);
}
horizontal.write(servoh);
}
delay(dtime);
} else {
oldvalue = horizontal.read();
oldvalue2 = vertical.read();
for (pos = oldvalue; pos <= 180; pos += 1) {
horizontal.write(pos);
delay(15);
}
for (pos2 = oldvalue2; pos2 <= 0; pos2 += 1) {
vertical.write(pos2);
delay(15);
}
buttonStateB = digitalRead(button2);
if (buttonStateB == LOW) {
digitalWrite(motorA, LOW); // Clockwise
digitalWrite(motorB, 255);
} else {
digitalWrite(motorA, LOW);
digitalWrite(motorB, LOW);
}
}
}
I used two methods: Multimeter, oscilloscope to read the voltage and current. and to get the signal graph and to caulculat the Power.
I use servo motor
Voltage: oscilloscope reading: as we can see that each box represent 1V so the servo used 3.3 volt and that because I connect it to the 3.3V in RP 2040 microcontroller.
Current: Multimeter reading: are current reading is (0.164 A)
The equation for electrical power is P=IV: and as we know I = 0.164A and V = 3.3v
P=IV ----> P = (0.164A) x (3.3v) = 0.5412 W
So the power consumption for the servo motor is (0.5412 W)