Sunday, August 16, 2015

MicroServo SG90

by J.B. Wylzan


Project 25:  Microservo SG90

This project shows how to control the microservo from 0 to 180 degrees.





Hardware:
microservo SG90
connecting wires
breadboard
Arduino R3 UNO board


Block Diagram:


NOTE: The RGB and the Push Button are not on the sketch below. However using your understanding on our previous lessons or projects, you can add them on your sketch. Good Luck ;-)

Code # 22:

/* ===============================================================
      Project # 27: Microservo SG90
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihacklab.blogspot.com
      Abstract: This project shows how to activate a servo
================================================================== */


#include <servo .h>
int x = 1000

Servo servo;                         // name the Servo

void setup()
{
   servo.attach(6);                 // servo pin to arduino pin 6
}

void loop()
{
    servo.write(90);                  // Turn servo back to 90 center position
   delay(x);                              // Wait 1 second
   servo.write(45);                   // Turn servo Left to 45 degrees
   delay(x);                              // Wait 1 second
   servo.write(90);                   // Turn servo back to center position
   delay(x);                              // Wait 1 second
   servo.write(135);                 // Turn servo Right to 135 degrees
   delay(x);                              // Wait 1 second
   servo.write(0);                     // Turn servo Left to 0 degrees
   delay(x);                             // Wait 1 second
   servo.write(180);                // Turn servo Right to 180 degrees
   delay(x);                             // Wait 1 second
}




Challenge: 

Create the robot below using the microservo with an ultrasonic sensor attached on its axle.



Actual Layout:


Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #22 above
5. Paste Code #22
6. Click File > Save
7. Click Verify
8. Click Upload
9. Servo will scan from 0 to 180 degrees.

Serial Monitor Test:

int servopin=9;
int myangle;
int pulsewidth;
int val;

void servopulse(int servopin,int myangle)
{
pulsewidth=(myangle*11)+500;          // convert angle to 500-2480 pulse width
digitalWrite(servopin,HIGH);                // set the level of servo pin as “high”
delayMicroseconds(pulsewidth);        // delay microsecond of pulse width
digitalWrite(servopin,LOW);               // set the level of servo pin as “low”
delay(20-pulsewidth/1000);
}

void setup()
{
pinMode(servopin,OUTPUT);
Serial.begin(9600);
Serial.println("servo ready" ) ;
}
void loop() {
val=Serial.read();
if(val>'0'&&val<='9')
{
val=val-'0';                                          // convert character to number
val=val*(180/9);                                // convert number to angle
Serial.print("servo angle: ");
Serial.print(val,DEC);
Serial.println();
for(int i=0;i<=50;i++) 
{
servopulse(servopin,val);
}
}

}


Project # 23:  Robotics







Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, f you are interested in using any of the projects for personal or educational purposes, please inform the author by email or cite the source as follows: The Hackspace Project, J. B. Wylzan, 1988, USA.


No comments:

Post a Comment