Lab 5 - EENG312
Lab 5 - EENG312
Objective: The students will be introduced to motor control using servo motors. The
students learn about library usage via the servo library.
Lab Work - 1: Each group of students will edit and run the following Program in the IDE and
Tinkercad and debug to make it ready to be uploaded to Arduino Uno. The program controls
a servo motor, doing sweeps across its range of motion.
// C++ code
//using the servo library!
#include <Servo.h>
#define servo_pin 9
int pos = 0;
Servo servo_1;
void setup()
{
//defining the pin the servo will be attached to
servo_1.attach(servo_pin);
}
void loop()
{
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 180; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_1.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo_1.write(pos);
delay(15);
}
}
Lab Work - 2: Each group of students will edit and run the following Program in the IDE and
Tinkercad and debug to make it ready to be uploaded to Arduino Uno. The program controls
a servo motor, whose arm can be rotated clockwise and anticlockwise using two buttons.
#include <Servo.h>
#define servo_pin 9
#define left_button 4
#define right_button 3
Servo servo_1;
void setup()
{
servo_1.attach(servo_pin);
servo_1.write(pos); // Start servo at midpoint
pinMode(left_button, INPUT);
pinMode(right_button, INPUT);
}
void loop()
{
while (digitalRead(left_button) == HIGH)
{
servo_1.write(--pos);
delay(15);
servo_1.write(++pos);
delay(15);
}
}
HW 5. (Due Next Friday)
Write a program with the respective Tinkercad circuit design to control a turret that aims
toward movement. You will need to use multiple sensors. You could use PIR or Ultrasonic
distance sensors.
An example design would be:
3 sensors for left, middle, and right sides.
Servo arm points towards the direction of the detected movement.