EMT2461 Servo Motor Control Lab-2024 (1)

docx

School

CUNY Kingsborough Community College *

*We aren’t endorsed by this school

Course

31

Subject

Electrical Engineering

Date

Apr 3, 2024

Type

docx

Pages

13

Uploaded by MagistrateUniverseBarracuda24

Report
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Controlling a Servo Motor with a Potentiometer and the PWM Signal from Microcontroller Objectives 1. Control the position and direction of a servomotor by supplying varying resistance to the circuit using a potentiometer. 2. Build a circuit on the breadboard using electronics and motor components. 3. Measure and analyze PWM signals from the microcontroller using the oscilloscope. 4. Write the code/program for a servo motor controller that allows input commands from Arduino serial monitor to control a servo motor's direction. 5. Use CAD software to design the system schematic and construct the circuit. Required Equipment, Materials, and Software 1. Requirements for implementation using physical hardware. PC with software IDE installed. Microcontrollers such as Arduino Mega 2560 board or UNO board or other microcontrollers with USB cable PWM signal on the board Digital Multimeter Oscilloscope 10kΩ potentiometer Servo motor (SG92R or SM-S2309S or MG90S) or other standard motors 1 x 50nF Capacitor + 1 x 0.33 µF Capacitor + 1 x 0.1 µF Capacitor 1x LM78xx regulator (1x LM7805) (if 5V is required) NPN transistors (if ample power is needed) Battery Jumper wires and breadboard Tinkercad or Multisim software 2. Requirement for implementation using virtual hardware only. PC with internet access Tinkercad.com Account. The simulation will be completed using Tinkercad Circuits A Tikercad Virtual Microcontroller (Arduino UNO or another microcontroller with USB port) PWM signal on the board Virtual Digital Multimeter Virtual Oscilloscope Virtual 10kΩ potentiometer Virtual Servo motor (or any other standard motor) Virtual 1 x 50nF Capacitor + 1 x 0.33 µF Capacitor + 1 x 0.1 µF Capacitor Virtual 1x LM7805 regulator Virtual NPN transistors (if ample power is required) Virtual Battery Virtual Jumper wires and breadboard Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface 3. Built-in functions to be used. Serial.begin(speed); Serial.print(val); Serial.println(val); digitalRead(pin); digitalWrite(pin, logicValue); pinMode(pin, mode) analogRead(pin); analogWrite(pin, value); map(value, fromLow, fromHigh, toLow, toHigh); pulseIn(pin, HIGH) PART I: Background 1. Controlling a servo motor. Controlling servo motors is achieved by adjusting the signal’s duty cycle at a specific frequency. The servo motor expects a pulse every 20ms. The length or duration of the pulse determines how fast the motor turns. The shaft angle varies with an increase in pulse width or duration. The duration of that logic 1 pulse can be from 1ms to 2ms. However, some programming libraries, such as the Servo.h library, make controlling servos much easier. The Servo.h library is used to create a servo object associated with the PWM output through which the servo motor is controlled and then specify the motor's position. Most of the servo motors for hobbies operate from 4.8V to 6.5V. For MG-90S, the operating voltage is 4.8V to 6V. For SG92R, its operating voltage is 4.8 V (~5V) 2. A capacitor is needed when powering the servo motor. Some servos may draw a lot of power at startup, and this sudden high demand can be enough to drop the voltage on the microcontroller development board. This problem can be resolved by connecting a capacitor in parallel with the servo motor. 3. The three-terminal, positive regulator LM7805. IC LM7805 Voltage Regulator is a Linear Regulator IC that produces 5V (Figure 1). A regulator is mainly employed with the capacitor connected in parallel to the input terminal and the IC regulator's output terminal. LM7805 is a three-terminal device, particularly called input, output, and ground. For the input pin of the IC, positive unregulated voltage is given in regulation. For the output pin, the output of the regulated 5V volt is taken out at this pin of the IC regulator. Figure 1. The application of the fixed output regulator 4. Calculating the required resistance. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface To control the servo motor using a potentiometer, we use the voltage outputted from the potentiometer circuit, connected to an analog pin of the Arduino, as an input signal to drive the motor to move. This input voltage can be measured using a multimeter. The corresponding input resistance can be calculated using the voltage divider formula and the known source voltage, the measured input voltage, and the total potentiometer resistance. 5. Answer the following questions and complete Table 1. 1. What is the duty cycle when receiving a 1.5ms pulse every 20 ms? 2. What is the duty cycle when receiving a 1.0ms pulse every 20 ms? 3. What is the duty cycle when receiving a 2.0ms pulse every 20 ms? 4. Why would you place a capacitor in parallel with a servo motor? A capacitor is an electronic component characterized by its capacity to store an electric charge. Identify the type of capacitors in Table 1. For each capacitor, draw the corresponding symbol to indicate whether it is a polarized or a non-polarized capacitor. Table 1. The polarized capacitor and non-polarized capacitor Capacitors Polarized capacitor Non-polarized capacitor A potentiometer can be used as a voltage divider (Figure 2). If the voltage across the 10kΩ potentiometer is 5V, the voltage measured using a Multimeter is V A = 2V. What is the resistance between terminal 2 and terminal 3? Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Figure 2. A voltage divider PART II: Design a schematic using MultiSim or Tinkercad Tinkercad is open-source software that allows you to make computer-aided schematics. Tinkercad has a realistic and straightforward interface that makes designing circuits intuitive. You can draw your circuit by using this software ( https://www.tinkercad.com/ ). Figure 3 is a sample for your reference. Your schematic must use an Arduino UNO board and your lab's electronics components. Figure 3. Sample of sketch diagram PART III: Build and simulate a circuit using Tinkercad 1. Get familiar with Tinkercad. Go to Tinkercad.com and create an account or log into your existing account. Once logged in, click on the button at the right of your screen and select the option (as shown in Figure 4). You will now have blank workspace where you can build your circuit schematic. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Figure 4. Create a new circuit Figure 5. Search for components On the right, you will see a tab labeled ' Components ' (Figure 5). From this tab, drag and drop the components you need to build the circuit for this lab into the workspace. Find specific components by selecting the proper category (‘Basic’, ‘Arduino’, ‘Circuit Assemble’). You can also use the search bar to find specific components (e.g. 'Arduino', 'breadboard', 'servo', 'oscilloscope', etc.). Place the components you need in the workspace and connect them as you would on a physical breadboard. While working with wires, double-click on the points you want to create bends to route wires neatly. Wire colors can be changed by clicking on the wire and selecting the desired color from the color dropdown menu. Components with adjustable values (resistors, capacitors, sensors, etc.) can be configured by clicking on the component and entering the desired values. You can rotate a component by selecting the component and hitting 'R' on your keyboard. Complete your virtual circuit schematic as shown in Figure 6. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Figure 6. Lab schematic using virtual components Once a simulated schematic has been created, create a duplicate. Duplicates allow you to change components without losing your previous work. To create a duplicate, return to the main Circuits page, find the circuit you want to duplicate, click on the settings icon , and then select the option 'Duplicate,’ as shown in Figure 7. Figure 7. Create a duplicate of your schematic. 2. Creating the servo lab simulation To create the servo simulation, connect a servo motor, potentiometer, capacitors, and other electronic components to the correct analog and digital input and output pins of the Arduino board. Figure 8 shows an example of the lab simulation setup. Figure 8. Servo Control lab setup and simulation Fi th l idth f th i l Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Connect the potentiometer so that the Arduino can read its voltage output. To measure that voltage, connect a multimeter to the potentiometer. The servo motor has a female connector with three pins (Ground, Power, and Signal). Hover your mouse over each pin to see the proper way to connect the servo. To prevent damage to the servo from supplying too much power (for example, 9V), a 5V regulator (LM 7805) is connected between the 9V battery and the servo. As per the datasheet for the LM 7805, one capacitor is placed across the input (0.33 µF) and one across the output (0.1 µF) of the regulator (Figure 1). Connect the signal line on the servo connector to a PWM pin on the Arduino. For the simulation, PWM pin 9 is used. Understand that many servos draw lots of power, and require a separate power supply, and only the control line connects to the Arduino. Realize that you must be careful and not connect a larger servo to the Arduino for power, as that can damage your Arduino board. In addition, to see the Arduino PWM output signal that is being sent to control the servo motor, connect the signal from pin 9 to the positive pin of the oscilloscope. Control the servo motor using a potentiometer as an input. Connect the led to pin 10. You can observe the brightness of the led by changing the input voltage to the analog pin A0. 3. Programming the simulation Write your code to make your circuit control the servo using the potentiometer. Use the sample code in Part V as your starting point. Test your code by hitting the 'Start Simulation' button at the top right of the page. Click on the Serial Monitor at the bottom of the 'Code' section to see any print statements you have placed in your code. Any error messages that are generated will also be shown in the Serial Monitor area. If there are errors, correct them and restart the simulation. 4. Analyzing the simulation data Proceed to Part VI and Part VII for data analysis steps. PART IV: Build and test a circuit using physical electronic devices. This section describes how to complete the lab assignment using physical hardware. If you are working only with the virtual setup, skip this part. Connect a servo motor, potentiometer, capacitors, and other electronic components to the correct analog and digital input and output pins of the Arduino board. Figure 9 shows the example of setup, test, measurement, and data collection. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Figure 9. Hands-on setup with physical hardware, test, measurement, and data collection The servo motor has a female connector with three pins. The darkest or even black one is usually the ground. Connect this to the Arduino GND. Connect the power cable (red in this design) to the 5V battery. Connect the remaining line on the servo connector to a PWM pin on the Arduino. For this example, pin 9. Understand that many servos draw lots of power and require a separate power supply, and only the control line connects to the Arduino. Realize that you must be careful and not connect a larger servo to the Arduino for power, as that can damage your Arduino board. Control the servo motor using a potentiometer as an input. An oscilloscope is connected to the PWM output of Arduino to understand better the control signal being sent to the servo motor. PART V: Computer programming Please modify the example source code to control a servo. Open Arduino IDE, and upload the sourceCode.ino . Open the serial monitor. 1. Example 1 Controlling a servo position using a potentiometer (variable resistor). ServoControlCode1.ino #include <Servo.h> int ledDimVal = 0 ; int val = 0 , pos = 0 ; int ledPin = 10 ; int potPin = A0; int i; //used for index Servo myservo; void setup() { pinMode (ledPin,OUTPUT); pinMode (potPin, INPUT); myservo.attach( 9 ); myservo.write( 0 ); Serial.begin( 9600 ); } void loop() { val = analogRead(potPin); ledDimVal = map(val, 0 , 1023 , 0 , 255 ); analogWrite(ledPin, ledDimVal); Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface //Sweeps the shaft of a RC servo motor pos = map(val, 0 , 1023 , 0 , 180 ); myservo.write(pos); Serial.print( "Position (angle in degree) = " ); Serial.println(pos); Serial.println( "----------------------------------" ); Serial.print( "Read Value (from an analog input) = " ); Serial.println(val); delay ( 100 ); // waits for the servo to get there } 2. Example 2 Measuring the duration of a pulse of PWM from an Oscilloscope ServoControlCode2.ino #include <Servo.h> int val = 0 , pos = 0 , current_pos = 0 ; int Tw; //pulse width in us; int potPin = A0; int i; //used for index Servo myservo; void setup() { pinMode(potPin, INPUT); myservo.attach( 9 ); Serial.begin( 9600 ); } void loop() { //read a pot val= analogRead(potPin); //Sweeps the shaft of a RC servo motor pos = map(val, 0 , 1023 , 0 , 180 ); myservo.write(pos); //Read the duration of the pulse at Pin 9 Tw = pulseIn( 9 , HIGH); Serial.print( "Pulse width of PWM in microseconds = " ); Serial.println(Tw); Serial.println ("----------------------------------" ); Serial.print( "Position (angle in degree) = " ); Serial.println(pos); Serial.println( "----------------------------------" ); Serial.print( "Read Value (from an analog input) = " ); Serial.println(val); Serial.println( "----------------------------------" ) delay( 1000 ); // waits 100 us } Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface PART VI: Characterize the circuit’s behavior and present precise and complete results. Data can be collected on both the input and output of Arduino. The input voltage can be measured using a Multimeter. The PWM pulse sent to the servo motor can be observed on the oscilloscope screen. Take a screenshot of the Arduino sketch and serial monitor output. Take a picture of the circuit and the pulse from the oscilloscope display. Record the input voltage value and calculate the input resistance value. Record the analog read value and the motor angle in degrees, which can be found from the serial monitor (the source code's output). Fill in the appropriate data in Table 2. Table 2. The input voltage and resistor and the output data from the serial monitor Record No. Input voltage (measured) (Volt) Input resistance (calculated) ( Ω ) Read Value (from an analog input) (the record from the serial monitor) Position (angle in degrees) (the record from the serial monitor) 1 2 3 4 5 6 7 8 9 10 Create a graph of the input resistance vs. servo angle position (angle in degree), as shown in Figure 10. You can edit the graph below. You must analyze and discuss the results. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Figure 10. The input resistance vs. servo angle position (in degrees) The percentage of time that the PWM signal remains HIGH (on time) is called the duty cycle. The duty cycle is measured as a percentage of the on-time (Tw) over the time it takes for one cycle of the waveform to complete, the period (T). The duty cycle of the PWM signal can be calculated using an oscilloscope. If a physical oscilloscope is not available, use the virtual oscilloscope . Figure 11.1 The PWM signal period observation Figure 11.2 The PWM signal pulse width observation Figure 11. The PWM signal period and pulse width Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter 0 50 100 150 200 250 0 2000 4000 6000 8000 10000 12000 Servo Motor Position (Degrees) Input Resistance (Ohm, Ω) Input Resistance vs Servo Position
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface To measure the waveform period, first set the Time Per Division of the oscilloscope to 5ms (Figure 11) so that you can see the complete cycle of the waveform. Then set the potentiometer to its maximum value so that the pulse is at its widest and easiest to visualize. Zoom in (mouse scroll or Ctrl+) to focus on the oscilloscope image. Next, determine the period (T) of the waveform by inspection. Using the known Time Per Division, use it to find the length (time) that it takes the waveform to complete a single cycle. Record the data. Now that the period of the waveform is known, the duty cycle that corresponds to any position of the servo movement can be calculated. First, change the Time Per Division of the oscilloscope to 500 µs so that you can see a single cycle. Set the potentiometer to the voltage values in Table 3. At each value, determine the Pulse Width (Tw) length by inspection using the known Time Per Division and enter the value in the table. Using the period of the waveform that was found earlier, calculate the duty cycle using the formula given in the table. At each voltage, note the corresponding servo position and analogRead value from the Serial Monitor. Table 3. The Pulse Width, Duty Cycle, and output data from the serial monitor Potentiometer Voltage (V) (sample data) Pulse Width, Tw (ms) Period, T (ms) Duty Cycle 𝑻𝑻𝑻𝑻 = × 𝟏𝟏𝟏𝟏𝟏𝟏% 𝑻𝑻 (Calculated) Servo Position (Degrees) (record from a S erial monitor) analogRead Value (record from a S erial monitor) 4.50 V 4.20 V 3.25 V 2.50 V 1.50 V 1.00 V 500mV PART VII: Discussion and analysis Describe, with detail, how circuit analysis, electronics, basic instrumentation, and computers are used to aid the system's characterization, analysis, and troubleshooting. Discuss how the function of a map(value, fromLow, fromHigh, toLow, toHigh) should be used in the lab. Analyze the data recorded and calculated in Table 2 and Table 3. Discuss the data here. Find the relationship between the servo position and input resistance. Discuss the relationship between the duty cycle and the servo position in degrees. Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
New York City College of Technology | Computer Engineering Technology Department EMT2461: Electromechanical Systems: Software Interface Appendix A Oscilloscope Kit (Optional Components) These components are suggested for students that are taking the class remotely to replace the oscilloscope in the physical lab. Handheld Pocket Sized-Digital Oscilloscope Kit ($25- $45) Developed by Dr. Yu Wang in Fall 2017. The work is partially supported by National Science Foundation ATE (#1601522). Revised in Spring 2018 and Fall 2019, updated in Spring 2023 by Dr. Yu Wang. Acknowledgment: Thanks Dr. Benito Mendoza, Dr. Xiaohai Li, and Warren Hunter