In this lesson, we will learn about setting up a servo steering. Servos are small box-shaped electro-mechanical devices that contain a DC motor, electronics to control the motor from a signal, a gear system to produce slow/strong output to a shaft. Normally, robotic servos are designed to sweep 180 degrees, but there are servos available that rotate full circle for special applications.
Servos usually come with several different arms you can attach depending on your design. Usually you use a Servo to position something, like steering direction, arm/leg movement etc. For most servos the position angle of the servo is controlled by the length of the pulse in milliseconds (usually between about 1.00-2.00ms). 1.50ms is usually Center, or Stopped (For Robot Servo).
Normally, servos have three wire connections. One (usually red) is for power supply (in this case 5V), the orange or white is usually signal wire to control servo and green or black fror ground. The signal wire here is connected to the Froboduino microcontroller pin D6 viw servo signal port. Insert servo three wires to the servo port while make sure that signal, 5V and ground connect to the right ports on froboduino.
Now, it's time for some coding. The reason why we waited for all this while for servo attachment, because it introduces a new feature in arduino coding. Froboduino requires a special library called "Servo.h". Just like other programming environments, Froboduino/Arduino also uses "libraries" to extend the functions of the program. Arduino IDE comes with a number preloaded libraries. You can see in Arduino>sketch>include library to check which libraries are already installed. Libraries can also be accessed through file explorer- documents>arduino>libraries. Other than these installed libraries, you can also add other libraries as you will learn in further lessons.
Before you attach the wheel clamp to the servo, we first need to know the middle position of the gear on servo. For marking the middle position, you should upload this code in arduino. Yoiu will notice that we have added a delay of 5 seconds in "set up" code where we declare "steeringServo.write(90);" as the middle position. Servo motor moves 0 tp 180 degrees. Mark the position and remove cable from Froboduino and mouth clamp such that front wheel is aligned with the middle position.
Its' coding time now.
// We need to include servo.h library. Servo.h library is included in all latest Arduino IDE versions as a standard Arduino Library. #include <Servo.h> //declare servo as object. Servo steeringServo; // accelerator is used to control Frobo speed. Remember PWM! const int accelerator = 5; //Moves left motor in forward direction. const int leftMotorFwd = A0; // Moves left motor is backward direction. const int leftMotorRev = A1; //Moves right motor in forward direction. const int rightMotorFwd = A2; // Moves right motor in backward direction. const int rightMotorRev = A3; //Servo motor servoPosition. Servo sweeps 0- 180 degrees. We set the servo right in the middle at start. int servoPos; //We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts. void setup() { // Define LEDs and Motors as outputs. Inputs are defined by default. pinMode(accelerator, OUTPUT); pinMode(leftMotorFwd, OUTPUT); pinMode(leftMotorRev, OUTPUT); pinMode(rightMotorFwd, OUTPUT); pinMode(rightMotorRev, OUTPUT); // Let us open the serial ports with data rate 9600 bits /second steeringServo.attach(6); Serial.begin(9600); //Declare starting position. Use this to set your wheel. steeringServo.write(90); //Use delay of 5 seconds first time so you can mark center position to mount your wheel. delay(5000); } //This loop code runs repeatedly void loop() { //This is the basic froboduino code for controlling servo to move in different directions. //This code will move steering wheel 90 degrees left and then 180 degrees right, then it will come back to middle. // Now we declare the starting servo Position on steering Servo at 90 and turn by 1 degree each to full left at 0 degree. // Read up on how to use "for" statements. These statements are used to repeat statement between two curly braces. Serial.println("turning right"); for (servoPos = 90; servoPos <= 180; servoPos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos' delay(15); // waits 15ms for the servo to reach the servo Position } delay(2000); Serial.println("turning left"); for (servoPos = 180; servoPos >= 0; servoPos -= 1) { // goes from 180 degrees to 0 degrees steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos' delay(15); // waits 15ms for the servo to reach the servoPosition } delay(2000); Serial.println("Back to the middle");
for (servoPos = 0; servoPos <= 90; servoPos += 1) { // goes from 180 degrees to 0 degrees steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos' delay(15); // waits 15ms for the servo to reach the servoPosition } delay(2000); } |
Here is the video for your reference....
Now, our Frobo is quite well equipped with hardware of diffrent kinds. It's now time to create a composite program to enable Frobo to do multiple tasks with a single program. This will require some patience. So, we will end this lesson here. The next lesson will be your graduation into the professional programming. In case of any queries, please ask through comments section.