Arduino and Servo Motor 1. Basics of the Arduino Board and Arduino a. Arduino is a mini computer that can input and output data using the digital and analog pins
b. Arduino Shield: mounts on top of Arduino Uno board lining up the pins. The use of the shield is to expand the flexibility of the Arduino Uno board. 2. Link the Arduino Shield on top of the Arduino Board linking the proper pins to each port
3. Motor Movement: Servo motor movements are rated 0 to 180 degrees a. 0 = Clockwise b. 90 = Neutral position (NOT Turned OFF; Idle) to turn a motor off servo.detach (); needs to be used c. 180 = Counter Clockwise 180 90 0 One can adjust speed and turning based upon delay commands and by attaching and detaching the motor from the Arduino board Command Attached: Electricity flow to the motor Detach: Electricity flow is cut off to the motor 4. Servo Wires Wires: Use Servo Motor Wire to Female End to connect to the board Wire the Motor to the Arduino Shield in Pin Row 4 Colors Brown or Black= Ground (G) Red or Red= Voltage (V) Orange or White= Signal (S) 5. Ultrasonic Sensor Wires: Use Four (4) Female to Female wire connectors Wire the Following Follow the proper color set Ultrasonic Sensor Arduino Board Pin Row Ground (GND) G 9 Volt (VCC) V 9 Echo S 9 Trig S 10 Show Mr. Jourden to make sure all wires are connected properly before suppling power to the unit
Program: Servo Motor Sweeping Angles #include <Servo.h> int servopin = 4; //Links Pin 4 on Aruindo board. So the pin# only has to be changed in //one location Servo myservo; int angle = 0; // servo position in degrees void setup() myservo.attach(servopin); void loop() // scan from 0 to 180 degrees for(int angle = 0; angle < 180; angle++) myservo.write(angle); delay(15); // now scan back from 180 to 0 degrees for(int angle = 180; angle > 0; angle--) myservo.write(angle); delay(15); Explore: Goal: Attempt to slow down the motor speed. Experiment with the program by modifying the delays and angle measures to see how the motor reacts. (I.E servo.write (45); or for statement angle <45); To turn motor off use servo.detach (); (Where servo is the variable name of the servo motor )
Program: Motor with Ultrasonic #include <Servo.h> Servo myservo; //ultrasonic sensor attach to pins. trigpin sends single out, echopin receives single in const int trigpin = 8; const int echopin = 9; int servopin = 4; void setup() myservo.attach(servopin); //attaches servo motor to the Arduino board Serial.begin(9600); // connects serial monitor to board to output data void loop() long duration, inches; // variables that will be used to calculate //Sends out signal for ultrasonic sensor by pulsating sound waves pinmode(trigpin, OUTPUT); digitalwrite(trigpin, LOW); delaymicroseconds(2); digitalwrite(trigpin, HIGH); delaymicroseconds(10); digitalwrite(trigpin, LOW); //calculates the duration the sound wave takes to come back to sensor duration = pulsein(echopin, HIGH); // converts duration (Sound) to inches // sends data to a function (subroutine) to calculate distance inches = microsecondstoinches(duration); Serial.print ("Inches="); Serial.println (inches); //Turning the motor on/off // Writing to motor 0 = Full Speed Forward; 180 = Full Speed Reverse; 90 = Mid Point of motor (idle) if (inches >2) myservo.attach(servopin); myservo.write(0); delay(1000); else // Detach disconnects the motor from the Arduino Board; Allowing the motor to turn off myservo.detach(); delay(1000); long microsecondstoinches(long microseconds) // Function is a sub program that can be utilized in many parts of the program without rewriting the code. // The Return Function returns a value of microseconds to the main program body return microseconds / 74 / 2;