Go Down

Topic: how to use tower pro micro servo 9g sg90 (Read 105 times) previous topic - next topic

original CCC

how to use tower pro micro servo 9g  sg90 with arduino ?


May 15, 2017, 11:23 am Last Edit: May 15, 2017, 11:26 am by Cactusface
     It's simple! there are lots of examples on line, try Arduino playground.

Here's a bit of code from one of my robot sketches.

Code: [Select]
#include <FastIO.h>
 #include <I2CIO.h>
 #include <LiquidCrystal_I2C.h>
 #include <Wire.h>
 LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);  // Set the LCD I2C address 
 Define constants and varibles
 #include <Servo.h>
const int LMpwm=6,LMdir=8,RMdir=7,RMpwm=9;  //Motor pins
int distance,cm,FRQ,TD,A,B,MotorTime,switches,action,Lspeed,Rspeed,Tilt;
long int randNumber,motorSpeed,TwirlTime;
float unit,val,bv,factor;
boolean go;
Servo servoScan;

void setup()
// lcd.begin(16,2);
// battCheck();
// lcd.clear();
// lcd.setCursor(2,0);
// lcd.print("I am Waiting");
// lcd.setCursor(3,1);
// lcd.print("Wave me off");
pinMode(LMpwm, OUTPUT);      // sets the digital pin as output     
pinMode(LMdir, OUTPUT);      // sets the digital pin as output
pinMode(RMdir, OUTPUT);      // sets the digital pin as output     
pinMode(RMpwm, OUTPUT);      // sets the digital pin as output
go = true;

// * * * start of Main program * * * *

void loop()

So you have to #include the library <Servo.h>
Attach it to a I/O pin with servoScan.attach(A0); Any pin will do!
and tell it where to move to with servoScan.write(90); values 0-180 approx?

Just play around with that see what happens......

Hope it helps.

Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.


Look at the Servo/Knob and Servo/Sweep sketches included in IDE. They cover the basics of driving a servo and the SG90 is an ordinary servo.

Just don't try to power more than one servo from the Arduino board itself. Servos can take a lot of current so it's best to use a separate power supply for them.


Go Up